Can't figure out the negative sum

Hi guys, can you help me out telling me what do I need to write in order to get the sum of the negative elements in every row ? I figured out how to do the sum of the positive elements but I can't figure out the sum of the negative ones.Thank you in advance.

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>
#include<iomanip>
using namespace std;
void GenMatrix(int matrix[][10], int rows, int cols)
{
     for(int i=0;i<=rows-1;i++)
     for(int j=0;j<=cols-1;j++)
     matrix[i][j]=rand()%101-50;
}
void OutputMatrix(int matrix[][10], int rows, int cols)
{
     for(int i=0;i<=rows-1;i++)
     {
             for(int j=0;j<=cols-1;j++)
             cout<<setw(4)<<matrix[i][j];
             cout<<endl;
     }
}
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];
            cout<<"The sum of the positive elements in row "<<i+1<<" is : "<<Sum<<endl;
            cout<<"The sum of the negative elements in row "<<i+1<<" is : "<<???-Sum<<endl; //Can't figure out this line :(
            Sum=0;
    }
}
int main()
{
    int a[10][10];
    int n,m;
    cout<<"Input the number of rows : ";
    cin>>n;
    cout<<"Input the number of cols : ";
    cin>>m;
    srand(time(0));
    GenMatrix(a,n,m);
    OutputMatrix(a,n,m);
    SumElements(a,n,m);
    system("pause");
    return 0;
}
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 :+)
Last edited on
I actually need the Sum=0; because I need the value reset, after all I need to see the Sum on each row .. not the total sum after each row.When I add a second "if" I get an error about name lookup of "j" ..
1
2
3
4
5
6
7
8
9
10
11
12
13
int SumElements(int matrix[][10], int rows, int cols)
{
    int SumPos=0,SumNeg=0;
    for(int i=0;i<rows;i++)
    {
            for(int j=0;j<cols;j++)
            if(matrix[i][j]>0) SumPos+=matrix[i][j];
            if(matrix[i][j]<0) SumNeg+=matrix[i][j];
            cout<<"The sum of the positive elements in row "<<i+1<<" is : "<<SumPos<<endl;
            cout<<"The sum of the negative elements in row "<<i+1<<" is : "<<SumNeg<<endl;
            SumPos=0,SumNeg=0;
    }
}
I actually need the Sum=0;


Ok, no worries :+)

When I add a second "if" I get an error about name lookup of "j" ..


Can you post the error message exactly as it shown?

Just noticed, you are missing braces for the inner for loop - always use braces, even if there is only 1 LOC. And put the std::cout statements and line 11 outside the inner for loop.
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
#include<iostream>
#include<iomanip>
using namespace std;
void GenMatrix(int matrix[][10], int rows, int cols)
{
     for(int i=0;i<rows;i++)
     for(int j=0;j<cols;j++)
     matrix[i][j]=rand()%101-50;
}
void OutputMatrix(int matrix[][10], int rows, int cols)
{
     for(int i=0;i<rows;i++)
     {
             for(int j=0;j<cols;j++)
             cout<<setw(4)<<matrix[i][j];
             cout<<endl;
     }
}
int SumElements(int matrix[][10], int rows, int cols)
{
    int SumPos=0,SumNeg=0;
    for(int i=0;i<rows;i++)
    {
            for(int j=0;j<cols;j++)
            {
            if(matrix[i][j]>0) SumPos+=matrix[i][j];
            if(matrix[i][j]<0) SumNeg+=matrix[i][j];
            }
            cout<<"The sum of the positive elements in row "<<i+1<<" is : "<<SumPos<<endl;
            cout<<"The sum of the negative elements in row "<<i+1<<" is : "<<SumNeg<<endl;
            SumPos=0,SumNeg=0;
    }
}
int main()
{
    int a[10][10];
    int n,m;
    cout<<"Input the number of rows : ";
    cin>>n;
    cout<<"Input the number of cols : ";
    cin>>m;
    srand(time(0));
    GenMatrix(a,n,m);
    OutputMatrix(a,n,m);
    SumElements(a,n,m);
    system("pause");
    return 0;
}


Everything works now and shows the correct sum of the negative and the positive elements in each row.Thanks.
Cool ....

Now it would make my day if you implemented some of the other suggestions I made :+D

Just another thing that might be a trap in the future:

j goes out of scope after line 28, which was the problem before. If you need to use a variable after the end of a loop, then declare it before both of the loops, rather than in the for loop initialisation expression.

I am not a fan of using i and j as variable names - they look similar, and can cause some hard to spot errors if one gets them mixed up. And they are near to each other on the keyboard. Better to use Row and Col instead.

Hope all goes well, I look forward to your updated code :+D
Topic archived. No new replies allowed.