Sum of rows and columns in a matrix

I have an excercise with the following statement: "Read through the keyboard a matrix with size 10x10, display the sum of each row and column of that matrix".

I don't really understand this statement since it would be tedious to fill manually a 10x10 matrix, 100 numbers. I want to generate a matrix with random numbers and then display the sum of each row and column. I was able to do this excercise with a matrix of 3x3 filled through keyboard, the code works but I am not able to generate a random matrix, my program simply crashes. My code attempting to do the excercise with a random matrix:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
    cout << "Exercise 6 - Two-dimensional Arrays (Matrix)" << endl;

    int r,c;
    int matrix[r][c];
    int num;
    int sum_rows,sum_col;

    srand(time(NULL));

    printf("Enter the number of rows: ");
    scanf("%d", &r);
    printf("Enter the number of columns: ");
    scanf("%d", &c);

    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            num=rand()%10;
            scanf("%d", &matrix[i][j]);
        }
    }
    //Sum of each row
    for (int i=0; i<r; i++) {
        sum_rows=0;
        for (int j=0; j<c; j++) {
            sum_rows += matrix[i][j];
        }
        printf("The result of sum of rows is: %d \n", sum_rows);
    }
    //Sum of each column
    for (int j=0; j<r; j++) {
        sum_col=0;
        for (int i=0; i<c; i++) {
            sum_col += matrix[i][j];
        }
        printf("The result of sum of columns is: %d \n", sum_col);
    }
    return 0;
}


"r" is rows and "c" is columns. The excercise asks for a 10x10 matrix so the user should enter r=10 and c=10. The part of the code that calculate and display the sum of rows and columns works well. I am not able to generate a random matrix and what frustrates me more, the program simply crash, do nothing and crash. This part of the code fails miserably:

for (int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
num=rand()%10;
scanf("%d", &matrix[i][j]);
}
}

What I am doing wrong?
You can't allocate from a variable this way:

int x;
int array[x]; //fail! This isn't allowed, x must be a compile time constant. You have to allocate pointers if you want run-time size.


to make it random, you just need a loop of

for
for
matrix[I][j] = rand() %10;
or
= num; but num isn't useful, just drop it in directly.

you don't need a scanf in the random filler.

srand(constant) is easier to debug, its the same every time. You can use time when done debugging.

I don't see why it crashed, but try that approach ....

you are mixing C and C++
use cin and cout for c++ unless you have a reason not to do so.
use <ctime> <cstdio> <cstdlib> instead for headers.
Last edited on
Thank you for your help. I think I get it. Excuse me to mix c with c++. In my course, we are programming in C compiling with C++ using CodeBlocks. I don't know the reason, we are still in the third lesson. The code works but I have a question:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
    cout << "Exercise 6 - Two-dimensional Arrays (Matrix)" << endl;

    int matrix[10][10];
    int r,c;
    int sum_rows,sum_col;

    printf("Enter the number of rows: ");
    scanf("%d", &r);
    printf("Enter the number of columns: ");
    scanf("%d", &c);

    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            matrix[i][j]=rand()%10;
        }
    }
    for (int i=0; i<r; i++) {
        for (int j=0; j<c; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    //Sum of each row
    for (int i=0; i<r; i++) {
        sum_rows=0;
        for (int j=0; j<c; j++) {
            sum_rows += matrix[i][j];
        }
        printf("The result of sum of rows is: %d \n", sum_rows);
    }
    //Sum of each column
    for (int j=0; j<r; j++) {
        sum_col=0;
        for (int i=0; i<c; i++) {
            sum_col += matrix[i][j];
        }
        printf("The result of sum of columns is: %d \n", suma_col);
    }
    return 0;
}



Exercise 6 - Two-dimensional Arrays (Matrix)
Enter the number of rows: 10
Enter the number of columns: 10
1 7 4 0 9 4 8 8 2 4
5 5 1 7 1 1 5 2 7 6
1 4 2 3 2 2 1 6 8 5
7 6 1 8 9 2 7 9 5 4
3 1 2 3 3 4 1 1 3 8
7 4 2 7 7 9 3 1 9 8
6 5 0 2 8 6 0 2 4 8
6 5 0 9 0 0 6 1 3 8
9 3 4 4 6 0 6 6 1 8
4 9 6 3 7 8 8 2 9 1

The result of sum of rows is: 47
The result of sum of rows is: 40
The result of sum of rows is: 34
The result of sum of rows is: 58
The result of sum of rows is: 29
The result of sum of rows is: 57
The result of sum of rows is: 41
The result of sum of rows is: 38
The result of sum of rows is: 47
The result of sum of rows is: 57
The result of sum of columns is: 49
The result of sum of columns is: 49
The result of sum of columns is: 22
The result of sum of columns is: 46
The result of sum of columns is: 52
The result of sum of columns is: 36
The result of sum of columns is: 45
The result of sum of columns is: 38
The result of sum of columns is: 51
The result of sum of columns is: 60

Process returned 0 (0x0)   execution time : 3.932 s
Press any key to continue.


Sorry for the delay answering, I'm spaniard and I am translating the code for your understanding. Before the question, the program was crashing because of this:

int matrix[r][c];

I forgot to declare the matrix with an initial value. I fixed this putting instead:

int matrix[10][10];

The question: Everytime I run this code, I always get the same random numbers. Is there any way to get other random numbers?
You had it to begin with.

srand(time(NULL)); //different every run

srand(any constant); //same every run.

<iostream> is a c++ only header.
namespaces are c++ unless C grew them since I last used it, so using namespace std isn't C



Last edited on
Topic archived. No new replies allowed.