So I have calculated the total of students on all of their 6 grades but now I want to be able to calculate the average of all the grades and list the averages next to the total column.
Any help would be greatly appreciated!
//preprocessor directive
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>
using namespace std;
//global variables/constants, function prototypes
void headerfn();
ifstream fin;
//function definitions
int main(){
system("color f0");
headerfn(); //function call
fin.open("source.txt");
if (!fin){
cout<<"Input failure";
system("pause");
return 1;
}
int scores[7][6];
//read the data
for(int row=0;row<7;row++){
for(int col=0;col<6;col++)
fin>>scores[row][col];
}//end of outer for
int rowtotals[7];
int coltotals[6];
for(int row=0;row<7;row++){
rowtotals[row]=0;
for(int col=0;col<6;col++)
rowtotals[row]=rowtotals[row]+scores[row][col];
}//end of outer for
for(int col=0;col<6;col++){
coltotals[col]=0;
for(int row=0;row<7;row++)
coltotals[col]+= scores[row][col];
}//end of outer for
for(int row=0;row<7;row++){
cout<<setw(5)<<rowtotals[row]<<endl;
}//end of outer for
/*
system("pause");
return 0;
}//end of main
//***********************************************
//void function without parameters
void headerfn(){
cout<<"******************************************"<<endl;
cout<<"*Arrays calculation*"<<endl;
cout<<"******************************************"<<endl;
}//end of headerfn
Source text file (source.txt) can contain some random data:
54 34 65 34 43 23
76 54 34 54 23 87
87 67 45 65 87 67
45 76 87 65 45 65
34 65 76 87 56 87
76 56 78 98 78 67
87 67 76 87 78 67