### Manipulating 2D arrays to compute the sums and averages from an infile

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?

Below is my code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````//This program uses a 2 dimension array to determine the population of 4 fish in 6 Oklahoma lakes #include #include #include using namespace 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<

Thanks so much for your help.

Edit: Sorry forgot something, maybe it would help if I posted the Input File's contents:

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
Last edited on
Would someone please help me with this? It's been several days and I haven't gotten any replies yet. Have I placed this topic in the wrong thread?
Ill take a look at this when i get home, ive got some expirience in dealing with 2d arrays
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.
Hi Seph,

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.

 ``123456789101112131415161718192021222324252627`` ``````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<

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.
Last edited on
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.
Topic archived. No new replies allowed.