write a program to print sum of each row of a 2D array.
Answer Posted / mark
/*
* C Program to find sum of each row and column of matrix
*/
#include <stdio.h>
#include <conio.h>
int main(){
int rows, cols, rowCounter, colCounter;
int inputMatrix[50][50], rowSum[50] = {0};
printf("Enter size of a matrix
");
scanf("%d %d", &rows, &cols);
printf("Enter matrix of size %dX%d
", rows, cols);
/* Input matrix */
for(rowCounter = 0; rowCounter < rows; rowCounter++){
for(colCounter = 0; colCounter < cols; colCounter++){
scanf("%d", &inputMatrix[rowCounter][colCounter]);
}
}
/* Calculate sum of each row and column */
for(rowCounter = 0; rowCounter < rows; rowCounter++){
for(colCounter = 0; colCounter < cols; colCounter++){
/* Add this element in it's row sum */
rowSum[rowCounter] += inputMatrix[rowCounter][colCounter];
}
}
/* Print rows sum */
for(rowCounter = 0; rowCounter < rows; rowCounter++){
printf("Sum of row number %d is %d
",
rowCounter, rowSum[rowCounter]);
}
getch();
return 0;
}
Source : http://www.techcrashcourse.com/2015/03/c-program-sum-each-row-and-column.html
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
i have to apply for the rbi for the post of officers. i need to know abt the entrance questions whether it may be aps or techinical....
int i=3; this declaration tells the C compiler to a) reserve space in memory to hold the integer value b) associate the name i with this memory location c) store the value 3 at this location d) all the above
How can I dynamically allocate arrays?
I just typed in this program, and it is acting strangely. Can you see anything wrong with it?
Can we declare variable anywhere in c?
write an algorithm to display a square matrix.
Here is a good puzzle: how do you write a program which produces its own source code as output?
What is the difference between strcpy() and memcpy() function in c programming?
What would happen to X in this expression: X += 15; (assuming the value of X is 5)
What are the benefits of c language?
Why & is used in c?
Is it possible to pass an entire structure to functions?
write a c program in such a way that if we enter the today date the output should be next day's date.
What are global variables?
What is extern storage class in c?