Hello, I have the following problem:
1.Write a program which gets into an array elements of matrix A sized as n and k, but size of matrix must agree with n, k < 20. The program also must:
a)calculate and show sum of elements of each row of a matrix
b)calculate and show sum of elements of each column of a matrix
My code is:
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int k, n, i, j;
do {
printf("Specify the size of the matrix (nxk)\n");
scanf("%dx%d", &n, &k);
} while(n>=20||n<0||k>=20||k<0);
int tab[n][k];
for (i=0; i<n; i++) {
for (j=0; j<k; j++) {
printf("Specify the element (%d,%d)", i, j);
scanf("%d", &tab[i][j]);
}
}
int sum_row[n], sum_column[k];
for (i=0; i<n; i++) {
for (j=0; j<k; j++) {
sum_row[i]+=tab[i][j];
sum_column[j]+=tab[i][j];
}
}
for (i=0; i<n; i++) {
for (j=0; j<k; j++) printf("%d ", tab[i][j]);
printf("\n");
}
for (i=0; i<n; i++) printf("sum of row %d equals: %d\n", i, sum_row[i]);
for (j=0; j<k; j++) printf("sum of column %d equals: %d\n", j, sum_column[j]);
system("PAUSE");
return 0;
However it doesn't calculate the sum properly, just some random, big numbers.
What is done wrong?
Thanks in advance