Arrays

Definition

    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.

Advertise on this site. I promise you will like the rates :)

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:

numbers[5]
index 0 1 2 3 4
numbers[index] -6 3 17 0 -7

Creating an array

.. simply write the name, followed by the size in brackets:

numbers[5];

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 index

    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[100] (sequence of 100 contracts), the index will vary from 0 to 99. In the example above(numbers[5]) the index can take values between 0 and 4(the first row of the table).

The elements

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:

numbers[i]

For example, to access the first cell in the example we write numbers[0], the third is - numbers[2]. 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.

Examples

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:

3 6 8 6 3

-4 0 5 -20 -20 5 0 -4

'a' 'b' 'c' 'd' 'd' 'c' 'b' 'a'

true false true

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?

  • Normally "i" has smaller value than "j". If they are equal, they point to the same item, which is located in the middle. In this case no further comparisons are need.
  • If "i" is greater than "j", we know that all pairs have been compared and that's it :)

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.

Homework

  1. What is the purpose of the index? What are its min. and max. values?
  2. Write an algorithm which creates one array with 20 elements and initializes them with the value of their index.
  3. Given is intValues[100]. Find the sum of the items, located on odd position (1, 3, 5..)
  4. Given is employees[50]. Make an algorithm, taking a name(string) input from user and search for that name in “employees”. If the name is found – output its index location, otherwise output -1.
  5. Create a flow chart that will copy all contents from names[100] to newNames[100]
  6. *Create an algorithm that will merge names[100] and names2[100] into allNames[200]. Check for duplicates, so that you don't save one name twice.
  7. ** prices[100] is filled with the prices of different goods. Sort them in descending order.

Previous: Nested Loops

Next: Multidimensional Arrays

Tutorial Contents:

1)Learn Computer Programming
2)Software Development Process
3)Flow Chart
4)Flow Chart Symbols
5)Data Type
6)What is a variable
7)Math Operators
8)Logical Operators
9)Loops

10)Nested Loops
11)Arrays
12)Multidimensional arrays
13)Programming Questions

   Search this site: