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:

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
//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 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:

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.

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
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.
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.