19 20 21 22 23 24 25 26 27 28 29 30
|
int SumElements(int matrix[][10], int rows, int cols)
{
int Sum=0;
for(int i=0;i<=rows-1;i++)
{
for(int j=0;j<=cols-1;j++)
if(matrix[i][j]>0) Sum+=matrix[i][j];
std::cout<<"The sum of the positive elements in row "<<i+1<<" is : "<<Sum<<std::endl;
std::cout<<"The sum of the negative elements in row "<<i+1<<" is : "<<???-Sum<<std::endl; //Can't figure out this line :(
Sum=0; // this resets the value every time, don't do that :+)
}
}
|
Hi,
First up, the normal idiom of a for loop is this:
for(int j=0;j < cols;j++)
This will make your code easier.
So you have the positive sum correct, do something similar for the negative one - have another if statement in the for loop. I would have 2 variables, SumPositive and SumNegative, so you can keep track of them.
Your function specifies returning an int, but you don't return anything. You could make the function return type
void
and send
SumPositive
and
SumNegative
as arguments that are references, or you could have 2 very similar functions that return an
unsigned int
, but make sure you assign that value to a variable.
With the layout of the file, put your function declarations before
main()
, and the function definitions after
main()
. That way we don't have to go looking for (or wonder where ) the
main()
is.
Don't have line 3, Google to see why, and what to do instead.
Hope all is well :+)