Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a 2-dimensional int array, and populate it with the above data (exclud

ID: 3644169 • Letter: 1

Question

1. Create a 2-dimensional int array, and populate it with the above data (excluding column names) from a text file.

2. Total the scores for each student, and place them in the total column.

3. Output each student ID and total score.

4. Output the student ID with the highest score.

5. Output the student ID with the lowest score.

6. Output the average score.

Rules:


- Write exactly 5 functions (besides the main function).

- Write a bool function to read the data into your 2D array from this text file.

text file: scores.txt


8823 100 98 97 100 96

2575 92 88 85 95 90

3498 100 100 97 99 100

6857 72 74 79 84 82

5872 88 95 94 95 93


- Save the text file in the same folder as your .cpp file to run your program from the IDE debugger.

- Optionally, copy the text file to the same folder as your .exe file (debug folder) to run your program from the command prompt.

-Your function should return true if all is OK, else return false. See Final rules below for more instructions.


Write a void function to calculate student totals and update the 2D array.


Write a void function to:

- Get the ID with the highest total score.

- Get the ID with the lowest total score.


Write an int function to return the average of the total scores.

Write a void function to output required data.

Call each of the 5 functions one time from the main function, and only from the main function.

If your first function call returns false, exit the program - do not continue.


More rules:


Declare exactly 2 (and only 2) global constants:


- const int COLUMNS = 7;

- const int TOT_COL = COLUMNS - 1;


Declare exactly 6 (and only 6) variables in the main function:


- const int ROWS = 5; - int students[ROWS][COLUMNS];

- int idHigh, idLow, average; - bool haveData;


Declare variables as needed in other functions.

Indent your code properly as shown in the book's examples.

Prototype all functions above the main function, and define all functions below the main function.

Your loops should work whether there are 5 students or 50 students, and whether there are 5 scores or 50 scores for each student.

Your constants will determine how many times your loops run.


Final rules:


Study page 527 for passing 2D arrays to functions and writing loops for 2D arrays.

Study page 155 for opening files.

Study page 164 for reading files.

(Book: C++ Programming: From Problem Analysis to Program Design, Fifth Edition D.S. Malik)


After opening your file, be sure all is OK by using the following code:


inFile.open("scores.txt");

if (!inFile)

{

cout << "Could not open file." << endl;

return false;

}

else

{

// your code here to populate your 2D array.

...

inFile.close();

}

return true;


Where inFile is the name you chose for the variable (identifier) for your file.


Output

Explanation / Answer

#include #include #include #include #include using namespace std; const int ROW=20; const int COL=20; void readLife(int bacteria[][COL]); void initLife(int bacteria[][COL]); void printLife(int bacteria[][COL]); int main() { int bacteria[ROW][COL]; readLife(bacteria); printLife(bacteria); system("pause"); return 0; } //************************************************************************* void readLife(int bacteria[][COL]) { ifstream infile; //declare a file variable int row; int col; infile.open("input.dat"); //open a file assert(infile); //make sure file opened initLife(bacteria); //initialize 2D array to 0 infile>>row>>col; while(infile) { bacteria[row][col]; infile>>row>>col; } infile.close(); } //*************************************************************************** void initLife(int bacteria[][COL]) { for(int row=0;row