Arrays as Structure Member Variables

  1. A structure can have arrays as member variables.

  2. Suppose we want to keep track of the exam score of a class using the following struct:

    struct CourseInfo
    {
        string courseName; // Course name
        char section; // section
        string instructorName; // Instructor name
        int numberOfStudents; // Number of students in the class
        int finalScores[500]; // Final exam scores
    }

    The structure can be declared as:

    CourseInfo CS1114;

    The following specifies that there are 85 students in the course:

    CS1114.numberOfStudents = 85;

    The scores for the final exam can be entered from the keyboard as follows:

        cout < < "Enter the scores:\n";
        for (int n = 0; n < CS1114.numberOfStudents; n++)
                cin >> CS1114.finalScores[n];

  3. Suppose the CS Department wants to keep track of all its courses for the Fall 2006 semester. Assuming that the CS Department never offer more than 50 courses per semester, one can declare an array of struct of type CourseInfo as follows:

    CourseInfo CSfall2006[50]     // array of struct for up to 50 courses offered by the CS Department.

    Assuming that the appropriate information is stored in this array of struct, we may output the following results:

    cout << "The total number of students in the 3rd CS course is: "
        << CSfall2006[2].numberOfStudents << endl;

    cout << "The final exam score for the 50th student in the 3rd CS course is: "
        << CSfall2006[2].finalScores[49] << endl;