Arrays as Structure Member Variables
-
A structure can have arrays as member variables.
-
Suppose we want to keep track of the exam score of a class using the following struct:
struct ExamScores
{
char Course[51]; // Course name
char Dept[4]; // Dept abbreviation (<= 3 characters)
int ExamNumber; // Exam number
int NumberOfStudents; // Number of students
int Scores[500]; // Exam scores
}
The array Scores stores the exam scores for the course. The structure can be declared as:
ExamScores CS1114;
The following specifies that there are 85 students in the course:
CS1114.NumberOfStudents = 85;
The scores for the first exam can be entered from the keyboard as follows:
cout < < "Enter the scores:\n";
for (int n = 0; n < CS1114.NumberOfStudents; n++)
cin >> CS1114.Scores[n];