Академический Документы
Профессиональный Документы
Культура Документы
Lab 8 : Vectors
Lab Section : B32
Lecture Section : L02
To: Joydeep Mukherjee
Instructor: Yani
Lab exercise : 2.1_2.2
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// END: Function Prototypes
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// START: Main Function
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
//Require: The Matrix from main
//Promises: The Maximum grade and return it back to main
double GetMaximumGrade (Matrix& Grade);
//Require: The Matrix from main
//Promises: Calculates the average of each student and retrun it back to main
vector<double> GetStudentAverages (Matrix& Grade);
int main( int n, char *args[] )
{
// declare a matrix (two dimensional vector) to store the grades
Matrix Grades;
// read the grades from file
ReadGrades( Grades );
// print all grades to the screen
for( int i = 0 ; i < Grades.size() ; ++i )
{
for( int j = 0 ; j < Grades.at(i).size() ; ++j )
cout << fixed << setprecision(1) << setw(7) << Grades.at(i).at(j);
cout << endl;
}
// COMPUTE THE MAXIMUM GRADE HERE AND DISPLAY TO SCREEN (TASK 2.2a)
double MaximumGrade = GetMaximumGrade(Grades);
// COMPUTE THE AVERAGE GRADE OF EACH STUDENT HERE AND (TASK 2.2b)
vector<double> Average(Grades.size());
Average = GetStudentAverages ( Grades);
// DISPLAY THEM ALL TO SCREEN
cout<<"The maximum grade is : "<<MaximumGrade<<endl;
for(int w =0; w < Average.size();w++)
{
cout<<"The average for student number "<<w+1<<" is
"<<Average.at(w)<<endl;
}
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// END: Main Function
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// START: Function Definitions
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
void ResizeMatrix( Matrix& mat, int numRows, int numCols )
{
// ADD FUNCTION DEFINITION HERE
// Once complete, 'mat' should have 'numRows' rows and 'numCols' columns.
mat.resize(numRows);
for (int i=0;i < numRows;i++)
{
mat.at(i).resize(numCols);
}
}
void ReadGrades( Matrix& mat )
{
const int NUM_STUDENTS = 10;
const int NUM_GRADES = 9;
// resize the matrix
ResizeMatrix( mat, NUM_STUDENTS, NUM_GRADES );
// check that the resizing is correct
if( mat.size() != NUM_STUDENTS )
{
cout << "The ResizeMatrix() function did not work." << endl
<< "The matrix has " << mat.size() << " rows, instead of "
<< NUM_STUDENTS << "." << endl
<< endl << "Program over." << endl;
exit(1);
}
// check that every row has the right number of columns
for( int i = 0 ; i < mat.size() ; ++i )
if( mat.at(i).size() != NUM_GRADES )
{
cout << "The ResizeMatrix() function did not work." << endl
<< "The " << i+1 << "-th row of matrix has "
<< mat.at(i).size() << " columns, instead of "
<< NUM_GRADES << "." << endl
<< endl << "Program over." << endl;
exit(1);
}
// NOTE: The following code reads data from the file named
//
"Grades.txt", which should be in the same directory as
//
this source code file. You are NOT responsible for
//
understanding the code in this lab. Reading (and writing)
//
from file will be covered in the next lab, at which point
//
you can return to this function to try to understand
//
what is happening.
return (Max2);
}
vector<double> GetStudentAverages (Matrix& Grade)
{
vector<double> average(Grade.size());
int rows = Grade.size();
int colums = Grade.at(0).size();
for (int i=0; i < rows;i++)
//Taking the numbers from each students
columns
output:
46.0
22.0
96.0
65.0
35.0
97.0
54.0
17.0
33.0
9.0
33.0
87.0
26.0
90.0
79.0
60.0
97.0
44.0
55.0
75.0
62.0
68.0
2.0
56.0
11.0
55.0
66.0
3.0
14.0
11.0
68.0
76.0
98.0
52.0
89.0
92.0
17.0
16.0
56.0
15.0
The maximum grade is : 100.0
The average for student number
The average for student number
The average for student number
The average for student number
The average for student number
The average for student number
The average for student number
The average for student number
The average for student number
The average for student number
55.0
66.0
54.0
84.0
76.0
49.0
26.0
38.0
83.0
36.0
83.0
31.0
11.0
28.0
15.0
18.0
84.0
89.0
54.0
69.0
1 is 57.2
2 is 46.8
3 is 38.6
4 is 65.3
5 is 47.9
6 is 55.3
7 is 44.2
8 is 53.0
9 is 80.2
10 is 41.0
66.0
98.0
48.0
58.0
17.0
70.0
19.0
65.0
85.0
46.0
29.0
18.0
6.0
71.0
27.0
100.0
37.0
50.0
72.0
50.0
53.0
5.0
66.0
92.0
25.0
73.0
97.0
66.0
97.0
64.0