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:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
usingnamespace 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]);
}
}
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:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
usingnamespace 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?