Example: C++ array

// Student poll program:
// Forty students were asked to rate the quality of the food in the student
// cafeteria on a scale of 1 to 10 (1 meaning awful and 10 meaning
// excellent). Place the 40 responses in an integer array and summarize
// the results of the poll.
// Note: Think about how this would be accomplished w/o arrays.

#include<iostream>

using namespace std;

int main()
{
    // Will be using constants to set array dimensions
    const int responseSize = 40, frequencySize = 11;

    // This index variable may come in handy
    int freqIndex;

    // Exactly 40 elements
    int response[ responseSize ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,
        10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7,
        5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

    // Quick way to initialize all array elements to 0
    int frequency[ frequencySize ] = { 0 };

    for ( int answer = 0; answer < responseSize; answer++ ) {
        freqIndex = response[ answer ];
        frequency[ freqIndex ] = frequency[ freqIndex ] + 1;
    }

    cout << "\n Rating" << "\t\t" << "Frequency" << endl;

    for ( int rating = 1; rating < frequencySize; rating++ )
        cout << " " << rating << "\t\t" << frequency[ rating ] << endl;

    return 0;
}