Algorithm - Arrays
Through this section of the unit we will be presenting code for some algorithm examples and you will also be either editing code or creating code from the algorithms. We will be using C and later C# as our programming languages. You can use http://www.compileonline.com to code and compile, however, that only provides a limited interface to debug code. You should download the free Microsoft Visual Studio Express 2013 or use an online IDE Chpokk.
For a speedy tutorial on how to create your first program in Virtual Studio watch the video below:
Each of these algorithms should be desk checked using appropriate values to ensure that the logic is understood. It is also important for students to implement these routines into their code so they can see the resulting output and understand the processing that has occurred.
Elements in an array can be indexed from 0 or 1. For consistency, in all the following examples, processing starts with element 1.
Load an array and print its contents
An array can be loaded from data values or input from the keyboard. The following algorithm assumes that values are read from a list of data statements until a sentinel value of “xxx” is encountered.
BEGIN LoadArray
Let i = 1
Read DataValue
WHILE DataValue <> “xxx”
Let Element (i) = DataValue
i = i + 1
Read DataValue
ENDWHILE
Let NumElements = i
Display “ There are” NumElements “ items loaded into the array”
END LoadArray
Note the use of a priming read to ensure that the sentinel value is not loaded into the array. The pre-test loop ensures that if there is no data in the list of data to be loaded (other than the sentinel value), then the loop will never be entered.
To print the array, it is assumed that there is a variable that stores the number of elements in the array.
BEGIN PrintArrayContents
Let i = 1
REPEAT
Display Element (i)
i = i + 1
UNTIL i > = NumElements
END PrintArrayContents
Note that if the number of elements is not known, the sentinel value would be stored into the last element in the array, and printing would continue until the sentinel value of “xxx” is encountered.
Add the contents of an array of numbers
To add the contents of an array of numbers, it is assumed that there is a counter indicating the number of elements in the array.
BEGIN SumArrayContents
Let i = 1
Let total = 0
REPEAT
Let total = Element (i) + total
i = i + 1
UNTIL i > = NumElements
Display “ The sum of all of the elements in the array = ” total
END SumArrayContents
Desk checking
Assuming the array above has 5 elements, this is an example of the desk check:
Note that if the number of elements is unknown, the sentinel value would be stored into the last element in the array, and the total would continue to accumulate until the sentinel value is encountered. In the example below, a sentinel of 999 is used.
Note the use of the Pre-test loop to ensure that the sentinel value 999 is not included in the total.
BEGIN SumArrayContents
Let i = 1
Let total = 0
WHILE Element (i) <> 999
Let total = Element (i) + total
i = i + 1
END WHILE
Display “There are ” i “ elements”
Display “The sum of all of the elements in the array = ” total
END SumArrayContents