Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this lab, you should read a files contents into a two-dimensional (2D) array. The file input.txt is provided alongwith the Lab instructions document on

For this lab, you should read a files contents into a two-dimensional (2D) array. The file input.txt is provided alongwith the Lab instructions document on Blackboard > Weekly Labs tab > Lab 12 (download the file and save in the same folder as your working program). This file contains 5 quiz grades each for 3 students, i.e., the file contains 15 lines where the first 5 lines correspond to Student 1s grades for 5 quizzes and so on. A 2D array of size 3x5 must be created and the grades from the file should to be read into the array appropriately. You should then compute the total sum of the grades all quizzes for each student and the average of grades of all students for each quiz.

I've already started the lab and done parts 2 and 3. I'm struggling on how to make a nested for loop for part 4 and 5.

Step 2: Declare constants, variables, and a two-dimensional array

2.1 Declare two constant global integer variables STUDENTS and QUIZZES and initialize them to 3 and 5 respectively. These constant variables represent the dimensions of the 2D array and should be used everywhere applicable, further in the program.

2.2 Declare a 2D array grades of integer type with dimensions 3x5 i.e., 3 rows and 5 columns.

2.3 Declare integers to be used as loop variables, as required.

Step 3: Read input.txt line by line into the 2D array

3.1 Include the required header file to perform operations using files. Initialize an object of ifstream class to access the contents of input.txt.

3.2 Similar to how one loop was required to store elements in a one-dimensional array, implement two for loops that are nested to store elements into a 2D array. In each iteration of the inner for loop, read a new line from the file and store it as one element of the 2D array.

3.3 Use i and j as the loop variables. Use i to keep track of the number of quizzes and j to keep track of the number of students. This way, each row of the array represents the grades of all quizzes of one student and each column represents grades of all the students for each quiz. In other words, the array is accessed in a row-first manner.

3.4 Make sure that the indices of both dimensions (rows and columns) of the array do not go out of bounds i.e., use the right for loop conditions to make sure that the size of the 2D array is strictly 3x5, both in read and write operations of the 2D array. Refer to Section 7.3 of the text book for more insight into array bounds checking.

3.5 Close the file input.txt after the 2D array had been read by invoking the appropriate function.

Step 4: Compute and print total sum of all quizzes for each student

4.1 Similar to Step 3, run a nested for loop.

4.2 Use a temporary integer variable sum to keep track of the sum of all quizzes for each student. In the innermost loop, update the sum variable accordingly.

4.3 After sum has been computed for each student, print it along with the student number whose sum has been computed.

Step 5: Compute and print average of all quizzes

5.1 Similar to Step 3, run a nested for loop. This time however, the way the 2D array gets accessed is different i.e., the array is accessed in a column-first manner.

5.2 Use temporary integer variables sum and average to keep track of the sum of grades of all students for each quiz and the average of all quizzes.

5.3 The outer loop will represent the 5 quizzes and the inner loop will represent the grades of 3 students of each quiz. This implies that, you will have to compute the sum of students grades in the inner loop and then the average of them. This average will represent the average of one quiz.

5.4 After sum and average have been computed, print just the average along with the quiz number.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

Students also viewed these Databases questions