CyberiaPC.com Main Page




Learning C++

The Importance of Using Arrays

As you might already now, data types can store only one value at a time.  Therefore, you would not be able to have a data type that contains more than one slot to store more than one variable.  This is where arrays come in.  Arrays are simply data types that can store more than one variable.  Each variable is stored in an array element.  Array indexes start with 0 and end with the number set by you, the programmer.

Here is an example of an array:

     int array[5];
     array[2]=100;

The above code declares an array of integers containing five elements.  The second line sets the third element to the value 100.  Here is a visual image of the array:

0 1 2 3 4
a     88    

Note that the array starts with 0 and not 1.  Therefore, the last element comes out to be 4 and not 5.

An arrays is initialized like this:

     int array[5] = {2, 4, 6, 8, 10};

This sets each array element to one of the numbers listed inside the curly brackets.

Now that you've learned the basics you might be asking: But why do we have to know all about arrays?  The answer is that sometimes you want to repeat something on different numbers that all pertain to a certain group.  However, you don't know how many numbers are going to be used, therefore, instead of initializing 100 integers, you just initialize one as an array and give it 100 elements.

Here's an example, say your a teacher (oh my...) and you want all your students to input their grades.  Your program would then gather all of the grades, find the average, display the highest and finally display the lowest.  This would be impossible (or very difficult) to do without using arrays.  Here are some of the problems that you'll face if you don't use arrays:

1. You won't know how many students there will be, how many variables should you declare?

2. You'll make your code look really scary and very long.

3. You'll find it extremely difficult to run calculations efficiently.

In this example, all you do is declare something like the following:

     const int MAX=100;
     string name[MAX]; // Name array
     string grade[MAX];l // Age array

The above code segment declares a maximum variable as a constant (because it won't change).  This determines the size of the array.  If we ever need to change the size, all we would have to do is change one variable.  The next line declares an array of integers with 100 elements to store the grades.  After that, we declare another array of 100 elements to store the names of the students.  This is known as parallel arrays, which means that if we display the grade of a student from element number 15 in the grade array, then the name of that student should also be stored in element number 15 in the name array.

In this particular example, the rest of the code would be composed of mainly For loops.  Here is what the For loops would do with the arrays declared above:

1. Set all of the array elements to zero.

2. Ask the user for a name and grade and store them in the appropriate array elements at position zero.

3. Increment counter.  Repeat step two.  This time user input is stored in element number two of each array.

And finally, to make things easier for you, here is the code for the steps outlined above:

1. To reset the array elements to zero:

     for (int i=0; i < MAX; i++)
     {
          name[i]=0;
          grade[i]=0;
     }

2 & 3. Inputting name and grade from user and incrementing element number:

     string sentinel="end";
     int counter=0;
     
     while (sentinel!="end")
     {
          cout << "Enter name" << endl;
          cin >> name[counter];
          
          cout << "Enter grade" << endl;
          cin >> grade[counter];
          
          counter++;
          
          cout << "Press any character to continue? (end to quit) ";
          cin >> sentinel;
     }

as you can see, counter determines in which slot each input is stored, and since it is incremented after each iteration, every input is stored in a different array element.  The variable sentinel is used to determine when the user wants to stop entering values.

I think that it is also important for you to know how to duplicate an array.  Duplicating a complete array can't be done the same way normal variables are done.  For example, if you have two arrays (x and y) then you can't simple say: x = y.  Instead, you use the same technique discussed earlier:

     for (int i=0; i < 100; i++)
     {
          x[i] = y[i];
     }

This makes y a duplicate of x.  Notice that i is incremented after each iteration, therefore, each element is copied at a time from one array to the other.

{Go Back To C++ Lessons Page}