use 2-dimension array of integers that has 6 rows representing the lakes and 4 columns representing the fish
Apr 29, 2013 at 4:03pm UTC
OK, so I have to read integers from a file, the integers are
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 have been able to read from the file and put them in the rows and columns, but I am having some trouble getting the sum and Average of the integers. Here is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//This program uses a 2 dimension array to determine the population of 4 fish in 6 Oklahoma lakes
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream DataFileIn;
void ComputeSums(int twoarrays[][4], int sum[], double ColSums[]);
//*************************************************************************
int main()
{
DataFileIn.open("F:\\DataFile3.txt" );
int row, column;
int twoarrays[6][4];
for (row = 0; row < 6; row++) //Reads the integers from file to rows and columns
for (column = 0; column < 4; column++)
DataFileIn>>twoarrays[row][column];
cout<<setw(14)<<"Fish1" <<setw(6)<<"Fish2" <<setw(6)<<"Fish3" <<setw(6)<<"Fish4" <<setw(6)<<"Sums" <<setw(10)<<"Averages" <<endl<<endl;
for (row = 0; row < 6; row++) //Prints the rows and columns from the file to output
{
cout<<"Lake " <<row + 1<<" " ;
for (column = 0; column < 4; column++)
{
cout<<setw(6)<<twoarrays[row][column];
}
cout<<endl<<endl;
}
}
//*************************************************************************
void ComputeSums(int sum[], int twoarrays[][4])
{
DataFileIn.open("F:\\DataFile3.txt" );
int row, column;
for (row = 0; row < 6; row++) //Reads the integers from file to rows and columns
for (column = 0; column < 4; column++)
DataFileIn>>twoarrays[row][column];
cout<<setw(14)<<"Fish1" <<setw(6)<<"Fish2" <<setw(6)<<"Fish3" <<setw(6)<<"Fish4" <<setw(6)<<"Sums" <<setw(10)<<"Averages" <<endl<<endl;
for (row = 0; row < 6; row++) //Prints the rows and columns from the file to output
{
sum=0;
cout<<"Lake " <<row + 1<<" " ;
for (column = 0; column < 4; column++)
{
cout<<setw(6)<<twoarrays[row][column];
sum = sum + twoarrays[row][column];
}
cout<<setw(6)<<sum<<setw<<endl; //does not output sum to "Sums" column
cout<<endl<<endl;
}
}
I have to do the sum and average in seperarte functions and allow the data to pass to and from the function. Any advice/assistance will be appreciated
Apr 29, 2013 at 4:07pm UTC
By the way an array of 6 rows and 4 columns has 24 elements. Your file contains 25 elements.:)
Apr 29, 2013 at 4:24pm UTC
I just noticed that, but unfortunately it's reading from a file our professor gave us, so we can't edit the datafile.
Topic archived. No new replies allowed.