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
A collection of functions,calls,subroutines or other data a) library b) header files c) set of files d) textfiles
Explain what is the difference between #include and #include 'file' ?
What is wrong in this statement? scanf(“%d”,whatnumber);
Can one function call another?
What is the difference between a string and an array?
Simplify the program segment if X = B then C ← true else C ← false
What is sizeof in c?
Can you explain what keyboard debouncing is, and where and why we us it? please give some examples
What are the types of variables in c?
Write a program to check armstrong number in c?
What is a global variable in c?
what is the role you expect in software industry?
What will the code below print when it is executed? int x = 3, y = 4; if (x = 4) y = 5; else y = 2; printf ("x=%d, y=%d ",x,y);
What is the size of a union variable?
What are the various types of control structures in programming?