Sometime you may need to write a program that uses many variables. Perhaps the program will need ten or even ten thousand variables. Perhaps you are working with a program that reads a large quantity of data from a scientific instrument and performs statistics on this data. In any case, it would be inefficient to declare so many variables. It would be even more difficult working with these variables in the program. This is where you would use an array – a set of variables, all declared using one name. The following line declares an array of 10 integers:
int MyArray[10];
The first element, or integer, in the array is numbered 0. The last element is numbered 9. To assign the value of 42 to the first element, and the value of 61 to the second element, you would do the following assignment statements:
MyArray[0] = 42;
MyArray[1] = 61;
You can declare an array and assign values to the entire set of elements in one line by doing the following:
int MyArray[10] = {42, 30, 16, 8, 27, 38, 99, 81, 7, 61};
Since the elements are numbered, arrays are ideal for using within loops to process the data. The following for loop will print all elements of the previously declared array:
for (int X=0; X<=9; X++)
{
cout << MyArray[X] << endl;
}
The following complete program demonstrates using this array.
Program 14.1a – One Dimensional Array |
#include <iostream.h>
cout << endl; |
Output |
You can use a for loop to parse through the elements not only for printing, but also for totaling their values. The next program uses the same ten element array and calculates the average of the elements. We declare a variable named Total and assign it a value of 0. The for loop steps through the array and adds the value of each element to the variable Total. To calculate the average, simply divide Total by 10.0.
Program 14.1b – Calculating Averages Using Arrays |
#include <iostream.h>
for (int X=0; X<=9; X++)
cout << "The average of the array elements is: " |
Output |
|
Coffee |
Espresso |
Cappuccino |
Mocha |
Oct. Sales |
152 |
73 |
97 |
44 |
Nov. Sales |
166 |
76 |
123 |
49 |
Dec. Sales |
189 |
89 |
118 |
51 |
Arrays can have more than one dimension. For example, a two dimensional array can be used for storing information in a spreadsheet that contains rows and columns. Take a look at this table of sales data from a coffee shop.
We could declare a two dimensional array for storing these numbers as shown in the following line:
int Sales[3][4] = {{152, 73, 97, 44},{166, 76, 123, 49},{189, 89, 118, 51}};
The following program will calculate the total sales of beverages for December.
Program 14.2 – Two Dimensional Array |
#include <iostream.h>
for (int Y=0; Y<=3; Y++)
cout << "Total December beverage sales was " << TotalSales <<
endl; |
Output |
1. Write a C++ line to declare an array of integers named Corvettes with 5 elements and assign them the following values: 1956, 1963, 1968, 1983, 1997.
2. Write a C++ line to declare an array named Days with the following values: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
3. Given the following array, write a for loop that increments each element:
short int Scores[10] = {100,40,30,90,75,60,100,0,25,80};
4. Using the array declared in the previous exercise, write a for loop that adds all the elements and stores the sum in the variable Total.