Arrays

Reading: Section 8.1

The for loop (page 30)

The for loop is another loop construct, slightly different from the while loop and more convenient in some situations.

for(<initAssign>; <condition>; <updateAssign>) {
    <toDoWhileTrue>
}
This type of loop is useful when you want to do something for every value in a set (especially when you want to do something for every integer in a range).

The following program for computing factorials is an example of the for loop at work.

#include <iostream>
#include <string>

using namespace std;

int main() {
    int num;
    cout << "Whose factorial do you want? ";
    cin >> num;

    int fact = 1;
    int i;
    for(i = 1; i <= num; i = i + 1) {
        fact = fact * i; // multiply i into fact
    }
    cout << num << "! =" << fact << endl;
    return 0;
}

Arrays (page 39)

An array is a more complicated data type for holding several related pieces of data of the same type. For example, you might use an array to hold all the test scores of a class, or the x-coordinates of data points in a graphing program.

To declare an array variable, you do something like this.

vector<<eltType>> <varName>(<arrayLen>);
For example,
vector<double> score(3);
creates an array called score that contains three doubles.

Within a program, we can refer to each individual element placing the element's array index in brackets following the array name (score[2], for example). between 0 and one less than the array length. (Notice that score[3] is therefore invalid!)

Here's a simple example program...

// average of three, page 40
#include <iostream>
#include <string>

using namespace std;

int main() {
    // read the three scores
    vector score(3);
    cout << "Type hree scores." << endl;
    cin >> score[0] >> score[1] >> score[2];

    // compute and print the average
    double avg = (score[0] + score[1] + score[2]) / 3;
    cout << "Average is " << avg << endl;
    return 0;
}

Combining loops and arrays

Loops (especially for loops) often come in handy for arrays, since so often we want to do something for every value in the array. Here's a lengthy program to compute the most-frequently-occurring value in a sequence of integer scores the user types.

// mode, inspired by page 40
#include <iostream>
#include <string>

using namespace std; // introduces namespace std

int main() {
    // initialize all tallies to 0
    vector<int> tally(11);
    int i;
    for(i = 0; i <= 10; i = i + 1) {
        tally[i] = 0;
    }

    // tally up scores the user types
    cout << "Type some scores between 0 and 10." << endl;
    cout << "Type -1 when done." << endl;
    int newnum;
    cin >> newnum;
    while(newnum >= 0 && newnum <= 10) {
        tally[newnum] = tally[newnum] + 1;
        cin >> newnum;
    }

    // find which is largest; this is the mode
    int mode = 0;
    for(i = 1; i <= 10; i = i + 1) {
        if(tally[i] > tally[mode]) {
            mode = i;
        }
    }
    cout << "The mode is " << mode << endl;
    return 0;
}