Find sum of all Matrix elements
Given a matrix mat[][], the task is to find the sum of all the elements of the matrix.
Examples:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}}
Output: 21
Explanation: Here sum of all element = 1 + 2 + 3 + 4 + 5 + 6 = 21
Input: mat[][] = {{4, 5, 3, 2}, {9, 5, 6, 2}, {1, 5, 3, 5}}
Output: 50
Explanation: Here sum of all element = 4 + 5 + 3 + 2 + 9 + 5 + 6 + 2 + 1 + 5 + 3 + 5 = 50
Approach: This can be solved using the following idea:
Traverse through the whole matrix and add the value of the element with the result.
Follow the steps mentioned below to solve the problem:
- Initialize the variable sum = 0 to store the sum of the matrix.
- Run a loop to traverse each row of the matrix.
- Use a nested loop to traverse the columns of the matrix for each row.
- Add each element to the variable sum.
- Return the sum as the required answer.
Pseudo Code:
sum = 0
for i = 0 to N-1:
for j = 0 to M-1:
sum = sum + mat[i][j]
return sum
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfMatrix( int N, int M, vector<vector< int > > mat)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
sum += mat[i][j];
}
}
return sum;
}
int main()
{
vector<vector< int > > mat = { { 4, 5, 3, 2 },
{ 9, 5, 6, 2 },
{ 1, 5, 3, 5 } };
int N = mat.size();
int M = mat[0].size();
cout << sumOfMatrix(N, M, mat) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int sumOfMatrix( int N, int M, int mat[][])
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
sum += mat[i][j];
}
}
return sum;
}
public static void main(String[] args) {
int [][] mat = { { 4 , 5 , 3 , 2 },
{ 9 , 5 , 6 , 2 },
{ 1 , 5 , 3 , 5 }
};
int N = mat.length;
int M = mat[ 0 ].length;
System.out.println(sumOfMatrix(N, M, mat));
}
}
|
Python3
def sumOfMatrix(N, M, mat):
Sum = 0
for i in range (N):
for j in range (M):
Sum + = mat[i][j]
return Sum
mat = [[ 4 , 5 , 3 , 2 ],
[ 9 , 5 , 6 , 2 ],
[ 1 , 5 , 3 , 5 ]]
N = len (mat)
M = len (mat[ 0 ])
print (sumOfMatrix(N, M, mat))
|
C#
using System;
class GFG {
static int sumOfMatrix( int N, int M, int [, ] mat)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
sum += mat[i, j];
}
}
return sum;
}
public static void Main()
{
int [, ] mat = { { 4, 5, 3, 2 },
{ 9, 5, 6, 2 },
{ 1, 5, 3, 5 } };
int N = mat.GetLength(0);
int M = mat.GetLength(1);
Console.WriteLine(sumOfMatrix(N, M, mat));
}
}
|
Javascript
function sumOfMatrix( N, M, mat)
{
let sum = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
sum += mat[i][j];
}
}
return sum;
}
let mat = [ [ 4, 5, 3, 2 ],
[ 9, 5, 6, 2 ],
[ 1, 5, 3, 5 ] ];
let N = mat.length;
let M = mat[0].length;
console.log(sumOfMatrix(N, M, mat));
|
Time Complexity: O(N * M), where N is the number of rows and M is the number of columns in the given matrix
Auxiliary Space: O(1)
Last Updated :
14 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...