Hi im trying to calculate the average of an inputfile using a 2d array. I tried to calculate the average per row and add them all together but my code gives me an average of zero. I was wondering if anyone can see what im doing wrong or a better way to do it. Thanks in advance for your help.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
const int NROW=10;
const int NCOLS=7;
double average=0.0;
string str;
double total=0;
double average1=0,average2=0,average3=0,average4=0,average5=0,average6=0;
double average7=0,average8=0,average9=0,average10=0;
double total1=0,total2=0,total3=0,total4=0,total5=0,total6=0,total7=0;
double total8=0,total9=0,total10=0;
int array[NROW][NCOLS];
ifstream inputFile("power1.txt");
if(!inputFile){
cout << "Error the file power1.txt does not exist.\n";
exit(1);
}
while(getline(inputFile,str)){ //reading in each line from the file
for(int i=0;i<NROW; i++){
total1= total1 + array[0][i];
average1=(double) total/10;
}
for(int i=0;i<NROW; i++){
total2= total2 + array[1][i];
average2 =(double) total/10;
}
for(int i=0; i<NROW; i++){
total3=total3 + array[2][i];
average3=(double) total/10;
}
for(int i=0; i<NROW; i++){
total4=total4 +array[3][i];
average4=(double) total/10;
}
for(int i=0; i<NROW; i++){
total5=total5+array[4][i];
average5=(double) total/10;
}
for(int i=0; i<NROW; i++){
total6=total6+array[5][i];
average6=(double) total/10;
}
for(int i=0;i<NROW; i++){
total7=total7+array[6][i];
average7=(double) total/10;
}
for(int i=0; i<NROW; i++){
total8=total8+array[7][i];
average8=(double) total/10;
}
for(int i=0;i<NROW; i++){
total9=total9+array[8][i];
average9=(double) total/10;
}
for(int i=0; i<NROW; i++){
total10=total10+array[9][i];
average10=(double) total/10;
}
}
cout << endl;
average=((average1+average2+average3+average4+average5+average6+average7+average8+average9+average10))/10;
cout << "The average power output is "<< average<< endl;;
inputFile.close();
}