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
Explain the difference between strcpy() and memcpy() function?
Write a code on reverse string and its complexity.
What is the correct declaration of main?
Do you know what are the properties of union in c?
Can you please compare array with pointer?
Write a program for Overriding.
What are c preprocessors?
program for reversing a selected line word by word when multiple lines are given without using strrev
How can I delete a file?
Explain the difference between the local variable and global variable in c?
What are file streams?
Is swift based on c?
What is the value of uninitialized variable in c?
Why C language is a procedural language?
List the different types of c tokens?