Hello everyone.
This is my first post here to this forum. I am still grasping C++ so I apologize if I come off as slow. It seems I almost have this right, but something is missing.
What I'm trying to do is compute the sums and averages of the data from the infile in separate functions that will pass to main and print on the screen. What does work is the first cout in main which prints all the 24 integers in the correct order from left to right. What I want it to do is print the calculations from the two void functions into main. I'm supposed to have the sums and averages print as new columns, then again as new rows.
Would you please look at my two ComputeSums and ComputeAverages functions and let me know if I'm going in the right direction?
Also, would you please give me some tips on how to gather the data from the ComputeSums and ComputeAverages functions into main? whatever I try seems to fail. Maybe arrays are handled differently when it comes to function calling?
//This program uses a 2 dimension array to determine the population of 4 fish in 6 Oklahoma lakes
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
ifstream DataFileIn;
void ComputeSums(int twoarrays[][4], int RowSums[], int ColSums[]);
void ComputeAverages(int RowSums[], int ColSums[], double RowAvgs[], double ColAvgs[]);
//*************************************************************************
int main()
{
DataFileIn.open("G:\\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 statement should go here to place ComputeSums and ComputeAverages after each row
cout<<endl<<endl;
}
//Where do I place the cout statement that would place the Sums and Averages of each column?
}
//*************************************************************************
void ComputeSums(int twoarrays[][4], int RowSums[], int ColSums[])
{
int row, column;
RowSums[row] = RowSums[row] + twoarrays[row][column];
ColSums[column] = ColSums[column] + twoarrays[row][column]; //Do these look correct?
}
//*************************************************************************
void ComputeAverages(int RowSums[], int ColSums[], double RowAvgs[], double ColAvgs[])
{
int row, column;
RowAvgs[row] = RowSums[row]/6.0;
ColAvgs[column] = ColSums[column]/4.0; //these should gather data from ComputeSums somehow
}
Thanks so much for your help.
Edit: Sorry forgot something, maybe it would help if I posted the Input File's contents:
okay thank you. My class is tonight around 6. I know that might not be a whole lot of time but maybe it might be enough to change a little error that would make the world of difference.
While I know it can be very tempting to put things into functions when you're learning about them, be careful to not get overexcited about using them when a few simple lines of code can accomplish the same thing. :) Wanting to show your teacher that you can do it is another matter, but for now I'll give you a quick and easy solution for what you're trying to do, without using a function.
int main()
{
DataFileIn.open("lakes.txt");
int row, column, sum;
int twoarrays[6][4];
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
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; // sets sum to 0 each time the initial for loop runs.
cout<<"Lake "<<row + 1<<" ";
for(column = 0; column < 4; column++)
{
cout<<setw(6)<<twoarrays[row][column];
sum = sum + twoarrays[row][column]; // Set sum to the sum of the old value of sum & the current value of "twoarrays".
// Can also be written as "sum += twoarrays[row][column];" if you've learned that.
}
cout<<setw(6)<<sum<<setw(9)<<sum/4.0<<endl; // Here, your sum can be found with a simple equation within the cout statement.
cout<<endl<<endl;
If you wanted an overall sum & average, that would be a bit different. But if all you need is a sum & average for each lake, and want to display your knowledge of nested for loops & multi-dimensional arrays, this would probably work just fine.
Hope that helps, and let me know if you have any more questions.
Thanks Kazekan for your help! While I would love to input this into my program, my teacher is a torturer and loves to put the most mundane things in the programs. While it would be much easier to not have to use 2 separate functions, he actually wants us to put the sums and averages of the two arrays in two separate functions. Then those sums and averages should be placed on the right hand side of the table as a new added column for the row sums and averages and another whole row added underneath the table for the column sums and averages. Hence my issue.