const int MAX_STUDENTS=40; const int MAX_LABS=14; int labScores[MAX_STUDENTS][MAX_LABS];
Exercise 1:
Manipulation of a two-dimensional array requires the manipulation of two indices. When the two-dimensional array labScores is declared, enough storage is set aside to hold a table containing 40 rows and 14 columns for a total of 40 * 14 = 560 integer values. To access one particular value, we must specify the row and column. The row index ranges from 0 to MAX_STUDENTS-1 (39) and the column index ranges from 0 to MAX_LABS-1 (13). Thus the table can be visualized as:
This two-dimensional array may also be visualized as a one-dimensional array of arrays. An alternative view of this array would be:
This two-dimensional array may be viewed as a one-dimensional array having 40 elements where each element is an array of 14 values.
Accessing a Two-Dimensional Array Element
Array indices may be integer constants (as in the above example), variables,
or expressions. They should be within the bounds of the array.
(Important: Just as with one-dimensional arrays,
C++ does not do run-time bounds checking of indices.)
Two-Dimensional Array Initialization
b. What value is stored in row index 2, column index 1?
c. Give the name of the location where the value 0
is stored?
When we store or process an array row-by-row, rather than column-by-column,
we call that row-major ordering. We usually, but not always, process
two-dimensional (2-D) arrays in row-major order.
We may also fill the elements of a 2-D array by reading data.
Suppose we wish to read entries into our labScores array.
We should use two loops--one to control the
student (row) and one to control the lab (column).
For example, to read in all labs corresponding to the first student
(the data in the first row of the table),
then read in all labs corresponding to the second student (the next row),
and so on, we might use the following function:
In our labScores example, suppose we wish to indicate that the second student
(corresponding to row 1) made a 90 on lab 10 (corresponding to column 9). We
might use the statement:
We can declare and initialize an array A as follows:
//declaration
int A[3][4] = {{8, 2, 6, 5}, //row 0
{6, 3, 1 ,0}, //row 1
{8, 7, 9, 6}}; //row 2
Memory for the array may be visualized as:
Exercise 2:
a. After the final answer sheet is printed, draw an alternative visualization of memory for
the array A as we
did above for our lab scores example.
//Function: ReadScores()
//Purpose: This function inputs lab scores for
// students in a computing class.
//
void ReadScores(int labScores[MAX_STUDENTS][MAX_LABS], //OUT: student labs
int& numStudents, //OUT: actual # of students
int& numLabs, //OUT: actual # of labs
ifstream& myin) //IN: input file stream
{
//local variables
int student, lab; //index for the student and lab being processed
//first read the number of students and labs
//stored in the file.
myin >> numStudents >> numLabs;
//Outer loop controls which student (row) is being read
for (student = 0; student < numStudents; student++)
{
//Inner loop controls which lab (column) is being read
for (lab = 0; lab < numLabs; lab++)
myin >> labScores[student][lab];
}
return;
}
Exercise 3:
Suppose we wish to read in the data for our students' scores
but the file is organized differently. Instead of all of one student's labs appearing
first, the file has all grades on lab 1 first, then all grades on lab 2, etc.
(In other words, the data is organized in column-major order.)
How must the code above be changed to accommodate this new arrangement of data?
Explain the difference on the answer sheet.
void ReadScores (int labScores[][MAX_LABS], //OUT: student labs int& numStudents, //OUT: Number of students int& numLabs, //OUT: Number of labs ifstream& myin); //IN: Input file stream
This second function heading, with the empty row value in the declaration, is the preferred way of declaring a 2-D array parameter.
If one wants to print the entire set of scores, a nested loop similar to that above can be used.
Add a function to print the scores so that each student's
labs appear on a separate line of output. Include a statement in your main program
to call this function. Your output should be labeled as follows:
Student 1: 80 90 70 100 60 90 85 78 93 80 70 98 89 94 Student 2: 98 85 100 99 89 90 72 0 78 98 100 65 0 56 Student 3: 85 60 25.... . .
15 sum = 0; 16 student = 2; // 3rd student 17 for (lab = 0; lab < NUM_OF_LABS; lab++) 18 sum += labScores[student][lab]; 19 average = float(sum)/NUM_OF_LABS; 20 cout << "The average for student " << student+1 << " is " 21 << average << endl;
In general, if we wished to determine the average for the Kth student, then
K-1 would replace the value 2 in statement 16 above. If we wished to find an average
for all 40 students, we can add an outer loop to control the student index.
void StudentAvg(int labScores [][MAX_LABS], //IN: Lab scores int numStudents, //IN: # of students in the class int numLabs) //IN: # of labs recorded per student
Exercise 6:
Add a function to cla14a.cpp, called labAvg(),
that finds and prints
the average score made on each individual lab. Call this function from the
the main program. Compile and run your modified program.
Submit a script log of a listing, compile, and run.
The following UNIX commands will let you create what is required:
(Be sure to exit the script session before continuing!)
$ script lab14ex6.log
$ pr -n -t -e4 cla14a.cpp
$ c++ cla14a.cpp -o cla14a
$ cla14a
$ exit
Submit the log file you have created in Lab 14 typing
$ handin lab14log lab14ex6.logFrom the PC you are working on, you must also submit the answer sheet (AnswerSheet14.pdf) using the following directions:
Congratulations!
You have finished Lab 14.