# Sum of elements of row/column of a matrix

This is a discussion on Sum of elements of row/column of a matrix within the C Programming forums, part of the General Programming Boards category; Hello, I have the following problem: 1.Write a program which gets into an array elements of matrix A sized as ...

1. ## Sum of elements of row/column of a matrix

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?

2. Are you receiving no errors or warnings from your compiler?

3. Code:
`intsum_row[n], sum_column[k];`
Make sure you first initialise these arrays to zeros, since uninitialised, they contain random values and you use:

Code:
```sum_row[i]+=tab[i][j];
sum_column[j]+=tab[i][j];```

4. Originally Posted by Ali3nSVK
Make sure you first initialise these arrays to zeros, since uninitialised, they contain random values and you use:
Any hints how to do this?
Sorry I'm a complete newbie
As you can imagine, I only depend on copied code

5. Use a loop similar to the ones you use to print out sum_row and sum_column, but instead of a printf with the element, set the element to zero. Also, your variable names are not very readable. Something like rowSize, colSize, row, and col would be better.

6. > As you can imagine, I only depend on copied code
And that is why nobody cares whether you pass or fail.

You can't learn this stuff by osmosis you know, you have to continually try (and fail) at every little thing until you succeed.
You can be pretty damn sure that all the experts here got to where they are through effort and determination - not sitting around saying "gimmetehcodez".

>> Make sure you first initialise these arrays to zeros, since uninitialised, they contain random values and you use:
> Any hints how to do this?
Yes, you go back to your C book, you look up "initialisation" in the index and you start reading.

7. Originally Posted by Pole
Any hints how to do this?
Sorry I'm a complete newbie
As you can imagine, I only depend on copied code
Code:
`int array[20] = {0};`

8. Hi again, spent all that time on reading.
I have created brand new code, however I want to add false data rejection, eg. if sb enters char/string because now the program only checks if the int is proper.
How to do that?
Code:
```#include<stdlib.h>
#include<stdio.h>
int main()
{
int n,i,j,k;
float S;

do
{printf("How many rows?\n");
scanf("%d", &n);
}while(n<=0);

do
{printf("How many columns?\n");
scanf("%d", &k);
}while(k<=0);

printf("\n");

float tab[n][k];

for(j=0; j<n; j++)
{
for(i=0; i<k; i++)
{printf("Element [%d][%d]: ", j+1,i+1);
scanf("%f", &tab[j][i]);
}
}

printf("\n");

for(j=0; j<n; j++)
{
S=0;
for(i=0; i<k; i++)
{
S+=tab[j][i];
}
printf("The sum of the elements in row %d: %f\n", j+1, S);
}

printf("\n");

for(i=0; i<k; i++)
{
S=0;
for(j=0; j<n; j++)
{
S+=tab[j][i];
}
printf("The sum of the elements in column  %d: %f\n", i+1, S);
}

printf("\n");

system("PAUSE");
return 0;
}```

9. I've invented a way to do this, however this loop doesn't return to beginning if the condition is false
How to do that?
Code:
```printf("How many rows?\n");

if (scanf("%d", &n) != 1)
printf("You entered wrong number\n");
else if (n>=20||n<0)
printf("You entered wrong number\n");
else {
printf("You entered correct number\n");}

do
{printf("How many columns?\n");
scanf("%d", &k);
}while(k>=20||k<0);```

10. Ok... now you need to add 1 more very important feature...
Proper code indentation.

Yes it's a bit more typing but when properly indented, your code is going to be a whole lot easier to follow and debug.

Indent style - Wikipedia, the free encyclopedia

11. Ok, I'd do the indentation after it is finished.
But how to make the loop from my previous post return to the beginning if inputted letter/number is not correct

12. Originally Posted by Pole
As you can imagine, I only depend on copied code
And how's that working out for you?

Seriously... you won't ever learn programming or C if you are copying other people's code.

Just look where you are... You are on a forum begging help with simple code you don't even understand well enough to indent properly... What's that tell you about your progress in learning C?

13. Originally Posted by Pole
Ok, I'd do the indentation after it is finished.
Indentation is not just to make it look good for others, it also helps you to clarify your code structure to yourself. It's important to indent properly all the time.

14. Originally Posted by Pole
Ok, I'd do the indentation after it is finished.
Person A: What are you doing?
Person B: I'm waxing the car.
Person A: But that rag you're using is totally covered in dog poop!
Person B: I know, I'll clean the rag when I'm finished waxing the car.

15. Originally Posted by oogabooga
Indentation is not just to make it look good for others, it also helps you to clarify your code structure to yourself. It's important to indent properly all the time.
... As you enter your code.