• No se han encontrado resultados

Assigning and Displaying Array Values

The following program shows how to assign values to an array, one element at a time. The assignment starts with the first index, 0, and ends with the last index, 2, which is one less than the number of elements, 3. The program then outputs the array values, one at a time. #include <iostream> using namespace std; int main () { int testScore[3];

cout << "Enter test score #1: "; cin >> testScore[0];

cout << "Enter test score #2: "; cin >> testScore[1];

cout << "Enter test score #3: "; cin >> testScore[2];

cout << "Test score #1: " << testScore[0] << endl; cout << "Test score #2: " << testScore[1] << endl; cout << "Test score #3: " << testScore[2] << endl; return 0;

}

Some sample input and output could be:

Enter test score #1: 77 Enter test score #2: 91 Enter test score #3: 84 Test score #1: 77

Test score #2: 91 Test score #3: 84

However, this one-element-at-a-time approach has no advantage over the following program, which does not use an array at all, but just three separate variables:

#include <iostream> using namespace std; int main ()

{

int testScore1, testScore2, testScore3; cout << "Enter test score #1: ";

cin >> testScore1;

cout << "Enter test score #2: "; cin >> testScore2;

cout << "Enter test score #3: "; cin >> testScore3;

cout << "Test score #1: " << testScore1 << endl; cout << "Test score #2: " << testScore2 << endl; cout << "Test score #3: " << testScore3 << endl; return 0;

}

The advantage of an array over using separate variables is the ability to use a loop. This is shown by the following program:

Assigning and Displaying Array Values #include <iostream> using namespace std; int main () { int testScore[3];

for (int i = 0; i < 3; i++) {

cout << "Enter test score #" << i + 1 << ": "; cin >> testScore[i];

}

for (i = 0; i < 3; i++) {

cout << "Test score #" << i + 1 << ": " << testScore[i] << endl;

}

return 0; }

Better yet, you can use a constant instead of an integer literal for the number of array elements:

#include <iostream> using namespace std; const int MAX = 3; int main ()

{

int testScore[MAX];

for (int i = 0; i < MAX; i++) {

cout << "Enter test score #" << i + 1 << ": "; cin >> testScore[i];

}

for (i = 0; i < MAX; i++) {

cout << "Test score #" << i + 1 << ": " << testScore[i] << endl;

}

return 0; }

This example illustrates an advantage of using constants rather than literals for the size declarator. Assume I wrote this program to keep track of a student’s test grades at a time when my policy is to give three tests during a semester. However, later I change my policy to giving five tests during a semester. Since I used a constant as a size declarator, I only need to make one code change, which is to initialize the constant MAX to 5 instead of 3. In contrast, had I instead used the numeric literal 3 as the size declarator, I have to find that number each time it is referred to in the program, once in the array declaration, and once each in the two for loops. This means only three changes, but in a more

complex program the number could be much higher. Not only is this time-consuming, but the potential exists that I could miss a reference to 3 which I needed to change to 5. In this example, the constant MAX is global. However, making the constant MAX global is not contrary to my recommendation in Chapter 9 against making variables global. The primary reason for my recommendation against global variables is that a global variable may be changed from anywhere in the program, making it more difficult to trace—for example, why such a variable has an incorrect value. By contrast, the value of a constant cannot be changed at all. Consequently, the reason for the recommendation that a

Assigning and Displaying Array Values

variable should not be global simply does not apply to a constant. Therefore, global constants, as opposed to global variables, are relatively common.

However, whether you use a constant or an integer literal for the number of array elements, you must take care not to go beyond the bounds of the array. The following program demonstrates a common programming mistake.

#include <iostream> using namespace std; const int MAX = 3; int main ()

{

int testScore[MAX];

for (int i = 0; i <= MAX; i++) {

cout << "Enter test score #" << i + 1 << ":"; cin >> testScore[i];

}

for (i = 0; i <= 3; i++) {

cout << "Test score #" << i + 1 << ": " << testScore[i] << endl;

}

return 0; }

This program is the same as the previous program, expect that the relational operator in the condition of each for loop has been changed from < to <=. The result is an attempt to access index 3 of the array. The problem, of course, is that there is no such index in a three-element array; the last index is 2. The result depends on the particular compiler and operating system, varying from weird output to run-time errors to the computer locking up, but the result is never good.

Documento similar