Program to find sum of elements in a given 2D array
Given a 2D array of order M * N, the task is to find out the sum of elements of the matrix.
Examples:
Input: array[2][2] = {{1, 2}, {3, 4}};
Output: 10Input: array[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
Output: 136
Approach:
The sum of each element of the 2D array can be calculated by traversing through the matrix and adding up the elements.
Below is the implement the above approach-
C++
// C++ program to find sum of // elements in 2D array #include <iostream> using namespace std; // Get the size m and n #define M 4 #define N 4 // Function to calculate sum // of elements in 2d array int sum( int arr[M][N]) { int i, j; int sum = 0; // Finding the sum for (i = 0; i < M; ++i) { for (j = 0; j < N; ++j) { // Add the element sum = sum + arr[i][j]; } } return sum; } // Driver code int main() { int i, j; int arr[M][N]; // Get the matrix elements int x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum cout << sum(arr); return 0; } |
Java
// Java code for the above approach import java.io.*; class GFG { // Get the size m and n static int M = 4 ; static int N = 4 ; // Function to calculate sum // of elements in 2d array static int sum( int arr[][]) { int i, j; int sum = 0 ; // Finding the sum for (i = 0 ; i < M; ++i) { for (j = 0 ; j < N; ++j) { // Add the element sum = sum + arr[i][j]; } } return sum; } public static void main (String[] args) { int i, j; int arr[][]= new int [M][N]; // Get the matrix elements int x = 1 ; for (i = 0 ; i < M; i++) for (j = 0 ; j < N; j++) arr[i][j] = x++; // Get sum System.out.println(sum(arr)); } } // This code is contributed by Potta Lokesh |
Python3
# phython program to find sum of # elements in 2D array # Get the size m and n M = 4 N = 4 # Function to calculate sum # of elements in 2d array def sum (arr): sum = 0 # Finding the sum for i in range (M): for j in range (N): # Add the element sum = sum + arr[i][j] return sum # Driver code arr = [[ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ]] # Get the matrix elements x = 1 for i in range (M): for j in range (N): arr[i][j] = x x + = 1 # Get sum print ( sum (arr)) # This code is contributed by ninja_hattori |
C#
using System; class GFG { // Get the size m and n static int M = 4; static int N = 4; // Function to calculate sum // of elements in 2d array static int sum( int [,]arr) { int i, j; int sum = 0; // Finding the sum for (i = 0; i < M; ++i) { for (j = 0; j < N; ++j) { // Add the element sum = sum + arr[i,j]; } } return sum; } static public void Main (String[] args){ int i, j; int [,]arr= new int [M,N]; // Get the matrix elements int x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i,j] = x++; // Get sum Console.WriteLine(sum(arr)); // Code } } // This code is contributed by Kritima Gupta |
Javascript
<script> // JavaScript program to find sum of // elements in 2D array // Get the size m and n const M = 4; const N = 4; // Function to calculate sum // of elements in 2d array const sum = (arr) => { let i, j; let sum = 0; // Finding the sum for (i = 0; i < M; ++i) { for (j = 0; j < N; ++j) { // Add the element sum = sum + arr[i][j]; } } return sum; } // Driver code let i, j; let arr = new Array(M).fill(0).map(() => new Array(N).fill(0)); // Get the matrix elements let x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum document.write(sum(arr)); // This code is contributed by rakeshsahni. </script> |
136
Time Complexity: O(M*N)
Auxiliary Space: O(1)
Another Method: Using STL. Calling the inbuilt function for sum of elements of an array in STL. We use accumulate( first, last, sum) function to return the sum of 1D array.
Below is the implementation of idea.
C++
// C++ program to find sum of // elements in 2D array #include <iostream> #include <numeric> using namespace std; // Get the size m and n #define M 4 #define N 4 // Function to calculate sum // of elements in 2d array int sum( int arr[M][N]) { int i, j; int sum = 0; // Finding the sum for (i = 0; i < M; ++i) { int ans = 0; sum += accumulate(arr[i], arr[i] + N, ans); } return sum; } // Driver code int main() { int i, j; int arr[M][N]; // Get the matrix elements int x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum cout << sum(arr); return 0; } |
Python
# phython program to find sum of # elements in 2D array # Get the size m and n M = 4 N = 4 # Function to calculate sum # of elements in 2d array def Findsum(arr): ans = 0 # Finding the sum for i in range (M): ans + = int ( sum (arr[i])) return ans # Driver code arr = [[ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ]] # Get the matrix elements x = 1 for i in range (M): for j in range (N): arr[i][j] = x x + = 1 # Get sum print (Findsum(arr)) # This code is contributed by Sam_snehil |
Javascript
// JavaScript program to find sum of // elements in 2D array // Get the size m and n const M = 4; const N = 4; // Function to calculate sum // of elements in 2d array const sum = (arr) => { let i, j; let sum = 0; // Finding the sum for (i = 0; i < M; ++i) { sum = sum + arr[i].reduce( function (accumulator, currentValue){ return accumulator + currentValue;}, 0); } return sum; } // Driver code let i, j; let arr = new Array(M).fill(0).map(() => new Array(N).fill(0)); // Get the matrix elements let x = 1; for (i = 0; i < M; i++) for (j = 0; j < N; j++) arr[i][j] = x++; // Get sum document.write(sum(arr)); // This code is contributed by rakeshsahni. |
136
Time Complexity: O(n*m) (where n = no. of rows and m = no. of column)
Auxiliary Space: O(1)
Please Login to comment...