Arrays are special variables, that can keep series of values of the same type. Just like every variable it has a name, a type and a size. The new feature here is the index. It is a positive integer number. The number of items is specified once, when the variable is created and cannot be changed after that.
Example: we have saved a sequence of integers in our new variable "numbers". It can hold 5 values. To visualize it we can use a table:
.. simply write the name, followed by the size in brackets:
In this case the brackets after the name contain the number of elements that we can keep. This is used only once – when the variable is created.
The purpose of
the index is to give us access to the corresponding element. Remember that it always
starts from 0 and therefore its biggest value is the number of
elements minus one (n – 1). If you work with contracts (sequence of 100 contracts), the index will vary from 0 to 99. In the example above(numbers) the index can take values
between 0 and 4(the first row of the table).
The second row of the table contains the
values of the elements. This is the actual data that we are interested in. As I said in the beginning it is of the
same type – in this case integer numbers.
To access the data on a given position, write the name of the array, followed by the index in brackets:
For example, to access the first cell in the example we write numbers, the third is - numbers. Do you get it why? - the first is located on index 0, the second on 1, the third on 2 and so on.. I repeat this several times, because beginners seem to forget this in the first lessons. It is not a big deal, after few examples it becomes natural ;)
Now that you know how to access any element, you will be happy to know that we can perform any action that we can do with a normal variable – read the value, change it, compare it and so on. For example, to output the fourth we use the block:
Change the last value to 0:
Since we use series of values, accessed by an index, which takes consecutive values, it is very convenient to use a loop when working with arrays. Often we will define a counter variable in the loop and we will use it as an index to access the elements.
Create an integer array with 100 elements and initialize all its elements with 0.
Probably you noticed – the last value of the index that will enter the body of the loop is 99. If we try to access an element with bigger index the program will crash, because of “index out of boundaries exception”.
Example: Create an algorithm which allows you to save 10 names.
Example: Input 10 characters, save them and output them in a reversed order.
In this next example we will check if the values are symmetric. First, take a look at several symmetric array:
So a symmetry means that all element couples with index (0, n-1), (1, n-2), (2, n-3) etc.. have the same value.
Here is the solution:
Note: Often we will use “N” as a variable, that holds the
number of elements.
This is how the algorithm works:
First we initialize the index variables with the first and the last positions. We use "i" to take the elements from the left half and "j" for the values from the right. Then we begin to compare each couple – the value of the first element to the value of the last element. If they are the same we continue with the next couple by incrementing and decrementing the two indexes. If we find that a given pair of elements is not symmetric, we can stop the comparisons and output that the array is not symmetric.
If all pairs are symmetric, we will compare them all. When do
we know that we have checked every single pair?
Just like with most tasks, this one could be solved with another approach – by using only one index variable. That variation requires a little bit more complicated arithmetic. You can try to figure it out.
Previous: Nested Loops
Next: Multidimensional Arrays
2)Software Development Process
4)Flow Chart Symbols
6)What is a variable