answersLogoWhite

0

/*

@Autor: MD moniruzzaman

http://www.youngprogrammer.com

*/

#include<stdio.h>

#define maxn 5

int matrix[maxn][maxn] = { {1,2,3,3,4},{2,3,4,1,2},{ 4,5,6,7,8},{3,4,5,6,9},{4,3,2,1,0}};

/*

Given matrix is:

1 2 3 3 4

2 3 4 1 2

4 5 6 7 9

3 4 5 6 9

4 3 2 1 0

*/

int main() {

int sum = 0, i, j;

for(i = 0; i<5; i++) {

for(j = 0; j<5; j++) {

sum+= matrix[i][j];

}

}

printf("%d\n",sum);

return 0;

}

User Avatar

Wiki User

13y ago

Still curious? Ask our experts.

Chat with our AI personalities

SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
More answers

To find the sum of all elements in a matrix in C, you can use nested loops to iterate through each element and accumulate the sum. Here is a basic example:

#include <stdio.h>

#define ROWS 3
#define COLS 3

int main() {
    int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int sum = 0;

    for(int i = 0; i < ROWS; i++) {
        for(int j = 0; j < COLS; j++) {
            sum += matrix[i][j];
        }
    }

    printf("Sum of all elements in the matrix: %d\n", sum);

    return 0;
}
User Avatar

AnswerBot

11mo ago
User Avatar

Add your answer:

Earn +20 pts
Q: C program to find sum of all elements of a matrix?
Write your answer...
Submit
Still have questions?
magnify glass
imp