Вы находитесь на странице: 1из 5

Thien Vu Nguyen

Lab 8 : Vectors
Lab Section : B32
Lecture Section : L02
To: Joydeep Mukherjee
Instructor: Yani
Lab exercise : 2.1_2.2

Exercise 2 Two Dimensional Arrays


Source Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <stdlib.h>
using namespace std;
// Define a new type called "Matrix" which is a vector of vector of doubles
typedef vector< vector<double> > Matrix;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// START: Function Prototypes
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// Function to resize the 'mat' argument to have 'numRows' rows
// and 'numCols' columns.
//
// REQUIRES: - The 'numRows' and 'numCols' be initialized to position values.
//
// PROMISES: - The 'mat' argument will be resized to have 'numRows' rows and
//
'numCols' columns.
void ResizeMatrix( Matrix& mat, int numRows, int numCols );
// Function to
//
// REQUIRES: //
// PROMISES: rows
//
//
//
//
//
//
//

read student grades from a file called "Grades.txt".


Nothing.
The 'mat' argument will be resized to have the proper number of
and columns for the data stored in "Grades.txt". All of the data
from the "Grades.txt" file will be read into the 'mat' argument.
If the "Grades.txt" file cannot be opened, a message explaining the
problem will be printed to screen and the program will terminate.
If the resizing fails (because the ResizeMatrix function to defined
does not work), a message explaining the problem will be printed to
screen and the program will terminate.

void ReadGrades( Matrix& mat );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// 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.

// open the file to read from


ifstream inFile( "Grades.txt" );
if( !inFile.is_open() )
{
cout << "The Grades.txt file cannot be opened." << endl
<< "Check that the file is in the proper directory and try again." <<
endl
<< endl << "Program over." << endl;
exit(1);
}
// read the data from the file
for( int i = 0 ; i < mat.size() ; ++i )
for( int j = 0 ; j < mat.at(i).size() ; ++j )
inFile >> mat.at(i).at(j);
// close the file
inFile.close();
// end of function
return;
}
double GetMaximumGrade (Matrix& Grade)
{
int rows = Grade.size();
//This is the number of rows
int colums = Grade.at(0).size();
//This is the number of columns
double Max1;
double Max2= 0.0;
for(int i=0;i < rows;i++)
//Checking row by row
{
double maxperow;
for(int j=0; j < (colums-1);j++)
//Checking column by column
{
maxperow = Grade.at(i).at(j);
if(maxperow < Grade.at(i).at(j+1))
{
maxperow= Grade.at(i).at(j+1);
}
Max1 = maxperow;
}
if(Max2 < Max1)
{
Max2 = Max1;
}
}

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

double averagenum= 0.0;


for(int j= 0; j < colums;j++)

//Adding the numbers from each

averagenum = averagenum + Grade.at(i).at(j);


}
average.at(i)=averagenum /colums;
//Find the average
}
return(average);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
// END: Function Definitions

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

Вам также может понравиться