How to Array the Arguments for Arrays in C++ - dummies

How to Array the Arguments for Arrays in C++

By Stephen R. Davis

An array is a sequence of variables in C++ that shares the same name and that is referenced using an index. Arrays are useful little critters that allow you to store a large number of values of the same type that are related in some way — for example, the batting averages of all the players on the same team might be a good candidate for storage within an array.

Arrays can be multidimensional, too, allowing you, for example, to store an array of batting averages within an array of months, which allows you to work with the batting averages of the team as they occur by month.

Consider the following problem. You need a program that can read a sequence of numbers from the keyboard and display their sum. You guessed it — the program stops reading in numbers as soon as you enter a negative number. This program will output all the numbers entered before displaying the average.

You could try to store numbers in a set of independent variables, as in

cin >> value1;
if (value1 >= 0)
    cin >> value2;
    if (value2 >= 0)

You can see that this approach can’t handle sequences involving more than just a few numbers. Besides, it’s ugly. What you need is some type of structure that has a name like a variable but that can store more than one value.

An array solves the problem of sequences nicely. For example, the following snippet declares an array valueArray that has storage for up to 128 int values. It then populates the array with numbers entered from the keyboard:

int nValue;
// declare an array capable of holding up to 128 ints
int nValueArray[128];
// define an index used to access subsequent members of
// of the array; don't exceed the 128 int limit
for (int i = 0; i < 128; i++)
    cin >> nValue;
    // exit the loop when the user enters a negative
    // number
    if (nValue < 0)
    nValueArray[i] = nValue;

The second line of this snippet declares an array nValueArray. Array declarations begin with the type of the array members: in this case, int. This is followed by the name of the array. The last elements of an array declaration are open and closed brackets containing the maximum number of elements that the array can hold. In this code snippet, nValueArray can store up to 128 integers.

The size of an array must be a constant expression — this means an expression that C++ can calculate when it does the build.

The 2014 standards allows the program to declare the size of an array with any expression as long as its value is known when the declaration is encountered. However, once declared, the size of the array is fixed.

This snippet reads a number from the keyboard and stores it into each subsequent member of the array nValueArray. You access an individual element of an array by providing the name of the array followed by brackets containing the index. The first integer in the array is nValueArray[0], the second is nValueArray[1], and so on.

In use, nValueArray[i] represents the ith element in the array. The index variable i must be a counting variable — that is, i must be a char, an int, or a long. If nValueArray is an array of ints, nValueArray[i] is an int.