Section 2 - An Array Example
8A.2.1 An Array of Strings
We will create an array of 20 strings representing student names.
string student[20];
We then want to place a recognizable initial value into each of the student strings and we do this with a for loop:
// initialize the array for (int k = 0; k < 20; k++) student[k] = "undefined";
Notice two things about this loop:
- It uses the size of the array, 20, to control the upper limit of the loop.
- It uses a less than, <, relational, not a <= operator.
The second observation is very important. There is no array element student[20] in an array of 20 students. The last member of the array is student[19]. Don't ever forget this.
Because it is very bad to use constants throughout the program (like 20, 19 or 100), we can avoid the problem of remembering what the size of the array is by using macro constant to define the length. A macro constant looks like this:
#define MAX_SIZE 20
This is usually done at the top of the file after which time you use the MAX_SIZE constant rather than the number 20. If you need a 19, for instance as the last array element, you would use the expression MAX_SIZE-1, not the literal 19.
There is no semicolon at the end of this line: it is not an actual statement but a compiler directive. Here is the preferred technique:
// constants #define MAX_SIZE 20 int main() { string student[MAX_SIZE]; // initialize the array for (int k = 0; k < MAX_SIZE; k++) student[k] = "undefined"; return 0; }
Sometimes we can't easily say what the size of the array is in a #defined constant like MAX_SIZE above. All it takes to understand this dilemma is to look at the example:
double myArray[] = {10.2, 56.9, -33, 12, 0, 2, 4.8, 199.9, 73, -91.2, 35, 91, 0.002};
In this case we can use an expression that uses the built-in sizeof() method to determine the size of the array. It goes like this:
// compute the size of the array short arraySize = sizeof(myArray)/sizeof(myArray[0]);
This strange expression can be explained. sizeof() returns the number of bytes of its argument. If myArray had 10 elements in it, and each was a four-byte double, then the value it returns would be 40, not 10. We have to divide by 4, the size of an individual element, to get the 10. It could be any element we take the size of, but we always have at least element number 0, so why not use that one.
8A.2.2 The Example
We will go into a loop that asks the user what student he wants to edit (by selecting a number from 0 to 19), and then, displays the current string value (name) for that student and request a new name. Here is a sample output:
Choose a student to edit from 0 to 19 ('q' to end program): 5 Editing student 5: 'undefined' Enter New Name: Sir Ben Kingsley Choose a student to edit from 0 to 19 ('q' to end program): 5 Editing student 5: 'Sir Ben Kingsley' Enter New Name: Sir Sean Connery Choose a student to edit from 0 to 19 ('q' to end program): 4 Editing student 4: 'undefined' Enter New Name: Helen Merrin Choose a student to edit from 0 to 19 ('q' to end program): q Press any key to continue . . .
We are ready to try out our first example of arrays.
#include <string> #include <iostream> #include <sstream> using namespace std; // constants #define MAX_SIZE 20 int main() { int value = 0; string newName, strValue; string student[MAX_SIZE]; // initialize the array for (int k = 0; k < MAX_SIZE; k++) student[k] = "undefined"; // infinite loop until user enters q or cancels while (true) { // get string from user cout << "\n\nChoose a student to edit\n" "from 0 to " << MAX_SIZE-1 << "\n" " ('q' to end program): "; getline(cin, strValue); // see if user wanted to exit if (strValue[0] == 'q') return 0; // convert to number istringstream(strValue) >> value; // week 4 lesson A.2 if (value < 0 || value > MAX_SIZE-1) continue; // get new name for this student cout << "\n\nEditing student " << value << ": '" << student[value] << "'\n"; cout << "Enter New Name: ", getline(cin, newName); student[value] = newName; } return 0; }