Arrays

Overview

Arrays provide a way to store multiple variables under a single variable name. This is very convenient for sets of variables that need to be accessed sequentially.

The following is an example of how to initialize an array. Similar to initializing a variable, you need to give the array a type. In the example below the array type is int. Also, as with variables, you also need to give the array a name. In the case below, the name of the array is example, but just like variables, you can give it any name you want. The number in the square braces determines the size of the array or how many items it can hold. The array below can hold three items or more specifically three ints. Inside the curly braces are listed the items in the array.

int example[3] = {2, 5, 44};  //Initializes an array with three values

The following example shows how to retrive an item from an array. In this case the second item (item 1 counting from 0), is placed into the variable x.

x = example[1];  //Sets x to value of the second item in the array or 5

Exercise:

  1. Write a program that includes an array with three numbers. Print the first element in the array.

  2. Write a program that includes an array with four numbers. Print the last element in the array.

  3. Write a program that includes an array with four numbers. Print each element of the array in order. Each number should be printed on a separate line.

  4. Write a program that includes and array that holds four different numbers. Print each element of the array in order using a ‘for loop’.

    TEACHER CHECK ____

  5. Write a program that includes two different arrays with three numbers each. Write program to compare the numbers in both arrays. i.e. compare the first number in one array with the first number in the other array and so on. If any of the pairs are equivalent, print “equal”. If they are not equal print “not equal.”

    TEACHER CHECK ____

  6. Write a program that includes two arrays with three elements each. Compare the two arrays to determine if they are identical (i.e. they have the same elements in the same order). If they are equal, print “equal”. If they are not equal print “not equal.”

    TEACHER CHECK ____

  7. Repeat the exercise above, but use a for loop to compare the three elements of each array.

    TEACHER CHECK ____