MultiDimensional arrays

Write a function that returns the sum of all the elements in a function using the following header
1
2
 const int SIZE = 4;
double sumColumn (const double m[SIZE], int rowsize, int columnindex);


write a test program that reads a 3-by-4 matrix and displays the sum of each column. here is a sample run:

1
2
3
4
5
6
7
8
9
10
11
 
 Enter a 3-by-4 matrix row by row:
1.5 2 3 4
5.5 6 7 8
9.5 1 3 1
sum of the elements at column 0 is 16.5
sum of the elements at column 1 is 9
sum of the elements at column 2 is 13
sum of the elements at column 3 is 13


So I can not get this to work right. I at first had it set up what I thought seemed to be properly but I did not know how to do the column index. The code I used below just shows the same total (the one for the first column) for each column. I think it has something to do with the way I have the columnindex setup but I have no clue how to do it the right way.







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
59
60
61
62
63
64
65
66
  #include <iostream> 
using namespace std;




const int columnsize = 4;
double sumColumn(const double m[][columnsize], int rowSize, int columnindex)
{
	
for (int column = 0; column <columnsize; column++)
{
	double total = 0;
	for(int row = 0; row < rowSize; row++)
	{
		total+= m[row][column];
    }
	

	return total;
}

}
int main()
{


	const int rowsize = 3;
	double m[rowsize][columnsize];
	cout <<"Enter a " << rowsize << "- by -" << columnsize << "matrix row by row:" << " " << endl;

	

	for ( int i = 0; i < rowsize; i++)
	
	
		for ( int j = 0; j < columnsize; j++) 
		
		
		
			cin >> m[i][j];
	for (int index = 0; index < columnsize; index++)
	{
		int columnindex = index;
	
	
	
	
		
	
		cout << "\nSum of the elements at column" << " " << columnindex << " " << "is" << " " << sumColumn(m,rowsize,columnindex) << endl;  }
	

		
	
	



		



	system("pause");
	return 0;
}
Last edited on
You are passing columnindex to the function but never using it. Figure out how you would use this and your problem is solved.

By the way, why not just pass index to the function? It would give the same results. No need to make a new variable called columnindex.
Last edited on
Well I tried using columnindex in the function, but the problem I have now is it only displays the sum of the first column. The problem is that in the function I declared columnindex = 0; than in than in the same array proccessed in the first for loop I increment it by one, such as this
1
2
3
4
5
6
7
8
9
10
11
 
const int columnsize = 4;

double sumColumn(const double m[][columnsize], int rowSize, int columnindex)
{
	columnindex = 0; 
	
for (int column = 0; column <columnsize; column++)
{
	columnindex++;
	double total = 0;


Than in the main function I set up a variable named int index = 0; I pass it to the function as the columnindex but I use it in my output before I use the function so I think it is not going through each column since it is reading it before it is passed to the function. This is how I have the main function set up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	

	const int rowsize = 3;
	double m[rowsize][columnsize];
	cout <<"Enter a " << rowsize << "- by -" << columnsize << "matrix row by row:" << " " << endl;

	int index = 0;

	for ( int i = 0; i < rowsize; i++)
	
	
		for ( int j = 0; j < columnsize; j++) 
		
		
		
			cin >> m[i][j];

	

	cout << "\nSum of the elements at column" << " " << index << " " << "is" << " " << sumColumn(m,rowsize,index) << endl;  


Also using it in the function wouldn't I have to return the columnindex along with the total? I have tried using a for loop in both the main function and also in the function at the same time that increments the column but all it did was display the same result for every column and everything else I try only goes through the first column before displaying the sum.
All you have to do is make a small change to your original code. Take a look at your ORIGINAL code again. Try tracing it with a pen and paper.

Take a close look at the condition of the outer for-loop in the sumColumn function. This is what you have:

for (int column = 0; column <columnsize; column++)

The way you have the nested loop in the sumColumn function, column will never be incremented beyond 0. The inner for-loop simply adds up the numbers in the 0th column and then returns, never giving the outer for-loop and chance to increment. It will do this every time you call the function. This is why you always got the sum of the 0th column for all 4 columns.

Thats why im saying, you need to use columnindex variable to solve this issue. You need to make sure that every time you call the function, the outer for-loop begins at the NEXT column number, not at 0 every time.

First call to sumColumn: column should begin at 0
Second call to sumColumn: column should begin at 1
..

and so on...

Index is being incremented for you in main function, so simply pass that to the sumColumn function (where it will be called columnindex) and do a slight modification in the header of your outer for-loop (I hope you have figured out by now what this modification will be), and problem solved.

Happy coding.
Last edited on
OMG Finally that has had me stumped for like the past two days. I am really hating myself for this one, such a simple fix and I was making it so much harder than what it should have been. After re-reading your comment a few times it finally hit me what I should have done. Thank you so much for the help.
Topic archived. No new replies allowed.