Calculate the sum of the numbers in each row in a matrix

Calculate the sum of the numbers in each row and print it.

This is my code, however it doesn't do what it's supposed to do, can someone help me and tell me where is the problem?

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
 #include <iostream>
 
using namespace std;
 
int main()
{
    int array2[15], i, j ,n,s;
    cout<<"Enter the number of rows and columns for the matrix" <<endl;
    cin>>n;
    int array[n][n];
 
    for(i=0;i<n;i++)
    {
        for(j=0; j<n; j++)
        {
            cout<<"Enter the elements of the matrix" <<endl;
            cin>>array[n][n];
        }
    }
    s=0;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            s=s+array[i][j];
            cout <<"The sum of the elements on the " <<i << "row is:" <<s<<endl;
           s=0;
        }
    }
 
    return 0;
1
2
cin>>n;
int array[n][n];
Illegal C++. Array size should be compile-time constant.

cin>>array[n][n]; Didn't you mean [i][j]?
Hey, MiiNiPaa thanks for the help. I did fix that but also had some other problems so I worked on them and here's the working solution if anyone else needs help with this:

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
#include <iostream>

using namespace std;

int main()
{
    int array2[15], i, j ,n,s;
    cout<<"Enter the number of rows and columns for the matrix" <<endl;
    cin>>n;
    int array[n][n];

    for(i=0;i<n;i++)
    {
        for(j=0; j<n; j++)
        {
            cout<<"Enter the elements of the matrix" <<endl;
            cin>>array[i][j];
        }
    }
int br=0;
    for(i=0; i<n; i++)
    {
         br++;
         s=0;
        for(j=0; j<n; j++)
        {

            s=s+array[i][j];
        }
                    cout <<"The sum of the elements on the " <<br<< "row is:" <<s<<endl;
    }

    return 0;
}
Topic archived. No new replies allowed.