-
Array trouble
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.
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;
}
These are the numbers (not all are used, only 21).
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.
-
>> reading the numbers from a file
You are reading in the numbers during the Print function, which isn't called until after the sums are calculated.
-
aww, i didn't even realize that. Thank you.
-
-
Quote:
> void main()
*faints*
http://faq.cprogramming.com/cgi-bin/...&id=1043284376
Was this intentional?
Code:
RowSums[row] = RowSums[column] + Array1[row][column];
To avoid that kind of problem/ambiguity, you can write it like this:
Code:
RowSums[row] += Array1[row][column];
You can do this at the variable's initialization:
Code:
int Colsums[3];
for(column = 0; column < 3; column++)
Colsums[column] = 0;
->
Code:
int Colsums[3] = {0, 0, 0};
If you leave off elements, 0 is implied, so you could use
Code:
int Colsums[3] = {0};
Or you could use memset (in <cstring>) instead of a for loop.
I don't think you use anything from <string>, nor <cmath> for that matter.