show the marks for one student 1 show the student name COMPLETE 2 show the

Show the marks for one student 1 show the student

This preview shows page 22 - 28 out of 28 pages.

//show the marks for one student //1. show the student name _____________________________________ COMPLETE //2. show the student exam marks cout << "Marks: "; for (colIndex = 0; colIndex < cols; colIndex++) _____________________________________ COMPLETE cout << endl << endl; } }
Image of page 22
Program example (4b) void showExamMarks(string names[ ], int exams[ ][COLS], int rows, int cols) { int rowIndex, colIndex, mark; string name; cout << "Marks for students: " << endl << endl; for (rowIndex = 0; rowIndex < rows; rowIndex++) { //show the marks for one student //show the student name cout << "Name: " << names[rowIndex] << endl; //show the student exam marks cout << "Marks: "; for (colIndex = 0; colIndex < cols; colIndex++) cout << exams[rowIndex][colIndex] << '\t'; cout << endl << endl; } }
Image of page 23
Program example (5) void computeStudentAvg(string names[ ], int exams[ ][COLS] , int rows, int cols) { int rowIndex, colIndex; string name; float average, sum; for (rowIndex = 0; rowIndex < rows; rowIndex++) { //compute student average exam mark for each student (row average) sum = 0.0; for (colIndex = 0; colIndex < cols; colIndex++) sum += exams[ rowIndex ][ colIndex ]; average = sum / cols; //show the student name and average cout << "Name: " << names[rowIndex] << endl; cout << "Average: " << average << endl; cout << endl << endl; } }
Image of page 24
Program example (6) : CLASS EXERCISE The average for one exam is computed by summing the marks in the column and then dividing by the number of students. Complete the code for the computeExamAvg function. void computeExamAvg(int exams[ ][COLS] , int rows, int cols) { int rowIndex, colIndex; float average, sum; for (______________________________________) { //compute average mark for each exam sum = 0.0; for (____________________________________) sum += _______________________ average = ______________________________ //show the average for the exam cout << "Exam in column: " << colIndex << endl; cout << "Average: " << average << endl; cout << endl << endl; } } exams[0][0] exams[0][1] exams[1][0] exams[1][1] exams[2][0] exams[2][1]
Image of page 25
Program example (7) void computeExamAvg(int exams[ ][COLS] , int rows, int cols) { int rowIndex, colIndex; float average, sum; for (colIndex = 0; colIndex < cols; colIndex++) { //compute average mark for each exam sum = 0.0; for (rowIndex = 0; rowIndex < rows; rowIndex++) sum += exams[rowIndex][colIndex]; average = sum / rows; //show the average for the exam cout << "Exam in column: " << colIndex << endl; cout << "Average: " << average << endl; cout << endl << endl; } } exams[0][0] exams[0][1] exams[1][0] exams[1][1] exams[2][0] exams[2][1]
Image of page 26
Summing All the Elements in a 2D Array (1) Given the following definitions: const int NUM_ROWS = 5; // Number of rows const int NUM_COLS = 5; // Number of columns int total = 0; // Accumulator int numbers[NUM_ROWS][NUM_COLS] = { {2, 7, 9, 6, 4}, {6, 1, 8, 9, 4}, {4, 3, 7, 2, 9}, {9, 9, 0, 3, 1}, {6, 2, 7, 4, 1}};
Image of page 27
Summing All the Elements in a 2D Array (2) // Sum the array elements. for (int row = 0; row < NUM_ROWS; row++) { for (int col = 0; col < NUM_COLS; col++) total += numbers[row][col]; } // Display the sum. cout << "The total is " << total << endl;
Image of page 28

You've reached the end of your free preview.

Want to read all 28 pages?

  • Spring '16
  • Array

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture