Ok,
I have a 2d array, and am reading the numbers from a file. I have to display them, and display the average of the sum of the columns and rows.
I can display them, but i can't get the sum correct. It comes up with a wierd number.
The part in bold is where i compute the sums of each row and column.
These are the numbers (not all are used, only 21).Code:#include <iostream> #include <string> #include <cmath> #include <iomanip> #include <fstream> using namespace std; void Compute_Sums(int Array1[][3], int RowSums[], int Colsums[]); void Compute_Avg(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[]); void Print(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[]); void main() { ifstream Array_Info; Array_Info.open("A:IntFile.DAT"); int Array1[7][3], RowSums[7], Colsums[3]; float RowAvg[7], ColAvg[3]; Compute_Sums(Array1, RowSums, Colsums); Compute_Avg(RowSums, Colsums, RowAvg, ColAvg); Print(RowSums, Colsums, RowAvg, ColAvg); } void Compute_Sums(int Array1[][3], int RowSums[], int Colsums[]) { int row; int column; row = 0; column = 0; for(column = 0; column < 3; column++) Colsums[column] = 0; for(column = 0; column < 3; column++) for(row = 0; row < 7; row++) Colsums[column] = Colsums[column] + Array1[row][column]; for(row = 0; row < 7; row++) RowSums[row] = 0; for(row = 0; row < 7; row++) for(column = 0; column < 3; column++) RowSums[row] = RowSums[column] + Array1[row][column]; } void Compute_Avg(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[]) { int row; int column; row = 0; column = 0; for(row=0; row<7; row++) RowAvg[row]=RowSums[row]/3.0; for(column=0; column<3; column++) ColAvg[column]=Colsums[column]/7.0; } void Print(int RowSums[], int Colsums[], float RowAvg[], float ColAvg[]) { ifstream Array_Info; Array_Info.open("A:IntFile.DAT"); int Array1[7][3]; cout<<" "<<"Fish1"<<" "<<"Fish2"<<" "<<"Fish3"<<endl<<endl; for(int row=0; row<7; row++) { cout<<"lake "<<row+1; for(int column=0; column<3; column++) { Array_Info>>Array1[row][column]; cout<<setw(6)<<Array1[row][column]<<' '; } cout<<endl; } cout<<endl<<Colsums[1]<<endl; }
8 27 33 14 81 146 305
249 412 71 226 4 144 55
94 493 133 265 788 240
380 117 88 25 60
I output "Colsums[1]" to test to see if the sum was correct. It comes up with a wierd number.



LinkBack URL
About LinkBacks


