Sum column on c++

Write your question here.
Hey guys, im having problems with my code. I wrote the sumcolumn function so that it will sum specific columns of an array, but i am getting a hexadecimal. I dont know why. also what would be the correct cout statetement so that it will sum an specific column

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
#include <iostream>
using namespace std;

const int SIZE = 4;

double sumColumn(const double m[][SIZE], int rowSize, int columnIndex) //Function to sum columns 
{
	double totalSum = 0;
	for (int row = 0; row < rowSize; row++)
	{
		totalSum = totalSum + m[row][columnIndex];
	}
	return totalSum;
}



int main()
{
	const int columnSize = 4;
	const int rowSize = 3;
	
	double myArr[rowSize][columnSize] = { 0 }; // declare array
	// Ask values to the user

	cout << "Enter a 3-by-4 matrix row by row: " << endl; // user input values
	for (int Row = 0; Row < rowSize; Row++) {
		for (int col = 0; col < columnSize; col++)
		{
			cin >> myArr[Row][col];
		}
	}

	cout << "Sum of the second column is equal to: " << (myArr[1], rowSize) << sumColumn; // I believe here is the root of my problem. 
	                                                                                      //i dont understand why im getting an hexadecimal
	                                                                                      //im unsure if this is the correct statement to sum column 2
	cout << endl;





	return 0;

}
What are you trying to do with this: (myArr[1], rowSize) << sumColumn?
Try << sumColumn(myArr, rowSize, columnIndex)..., and use whatever index you need for the columnIndex parameter to sum a certain column.

Edit: You can tack cout << endl; to the end of the previous statement:
cout << ... << sumColumn(myArr, rowSize, columnIndex) << endl;
If it's too long to read in one line, you can split it across as many lines as you need. C++ will ignore the whitespace.
Last edited on
Hey, thanks for the help. Though , just something else, columnIndex is undefined within main. Im trying to define it to something that will efficeiently be equal to columns. what would an efficient declaration of column index be ?
thanks this was the final code and it worked
#include <iostream>
using namespace std;

const int SIZE = 4;

double sumColumn(const double m[][SIZE], int rowSize, int columnIndex) //Function to sum columns
{
double totalSum = 0;
for (int row = 0; row < rowSize; row++)
{
totalSum = totalSum + m[row][columnIndex];
}
return totalSum;
}



int main()
{
const int columnSize = 4;
const int rowSize = 3;
int columnIndex = 0;

double myArr[rowSize][columnSize] = { 0 }; // declare array
// Ask values to the user

cout << "Enter a 3-by-4 matrix row by row: " << endl; // user input values
for (int Row = 0; Row < rowSize; Row++) {
for (int col = 0; col < columnSize; col++)
{
columnIndex = myArr[Row][col];
cin >> myArr[Row][col];

}//sumColumn(myArr, rowSize, columnIndex
}

cout << "Sum of the elements at column 0 is equal to: " <<sumColumn(myArr,rowSize,columnIndex) << endl; // I believe here is the root of my problem.
cout << "Sum of the elements at column 1 is equal to: " << sumColumn(myArr, rowSize, columnIndex+1) << endl;
cout << "Sum of the elements at column 2 is equal to: " << sumColumn(myArr, rowSize, columnIndex+2) <<endl;
cout << "Sum of the elements at column 3 is equal to: " << sumColumn(myArr, rowSize, columnIndex+3) << endl; //i dont understand why im getting an hexadecimal
//im unsure if this is the correct statement to sum column 2

cout << endl;





return 0;

}
Topic archived. No new replies allowed.