Program to find sum of elements in a given 2D array
Last Updated :
29 Mar, 2023
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: 10
Input: 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++
#include <iostream>
using namespace std;
#define M 4
#define N 4
int sum( int arr[M][N])
{
int i, j;
int sum = 0;
for (i = 0; i < M; ++i) {
for (j = 0; j < N; ++j) {
sum = sum + arr[i][j];
}
}
return sum;
}
int main()
{
int i, j;
int arr[M][N];
int x = 1;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
arr[i][j] = x++;
cout << sum(arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int M = 4 ;
static int N = 4 ;
static int sum( int arr[][])
{
int i, j;
int sum = 0 ;
for (i = 0 ; i < M; ++i) {
for (j = 0 ; j < N; ++j) {
sum = sum + arr[i][j];
}
}
return sum;
}
public static void main (String[] args)
{
int i, j;
int arr[][]= new int [M][N];
int x = 1 ;
for (i = 0 ; i < M; i++)
for (j = 0 ; j < N; j++)
arr[i][j] = x++;
System.out.println(sum(arr));
}
}
|
Python3
M = 4
N = 4
def sum (arr):
sum = 0
for i in range (M):
for j in range (N):
sum = sum + arr[i][j]
return sum
arr = [[ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ]]
x = 1
for i in range (M):
for j in range (N):
arr[i][j] = x
x + = 1
print ( sum (arr))
|
C#
using System;
class GFG
{
static int M = 4;
static int N = 4;
static int sum( int [,]arr)
{
int i, j;
int sum = 0;
for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
sum = sum + arr[i,j];
}
}
return sum;
}
static public void Main (String[] args){
int i, j;
int [,]arr= new int [M,N];
int x = 1;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
arr[i,j] = x++;
Console.WriteLine(sum(arr));
}
}
|
Javascript
<script>
const M = 4;
const N = 4;
const sum = (arr) => {
let i, j;
let sum = 0;
for (i = 0; i < M; ++i) {
for (j = 0; j < N; ++j) {
sum = sum + arr[i][j];
}
}
return sum;
}
let i, j;
let arr = new Array(M).fill(0).map(() => new Array(N).fill(0));
let x = 1;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
arr[i][j] = x++;
document.write(sum(arr));
</script>
|
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++
#include <iostream>
#include <numeric>
using namespace std;
#define M 4
#define N 4
int sum( int arr[M][N])
{
int i, j;
int sum = 0;
for (i = 0; i < M; ++i) {
int ans = 0;
sum += accumulate(arr[i], arr[i] + N, ans);
}
return sum;
}
int main()
{
int i, j;
int arr[M][N];
int x = 1;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
arr[i][j] = x++;
cout << sum(arr);
return 0;
}
|
Python
M = 4
N = 4
def Findsum(arr):
ans = 0
for i in range (M):
ans + = int ( sum (arr[i]))
return ans
arr = [[ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ], [ 0 , 0 , 0 , 0 ]]
x = 1
for i in range (M):
for j in range (N):
arr[i][j] = x
x + = 1
print (Findsum(arr))
|
Javascript
const M = 4;
const N = 4;
const sum = (arr) => {
let i, j;
let sum = 0;
for (i = 0; i < M; ++i) {
sum = sum + arr[i].reduce( function (accumulator, currentValue){ return accumulator + currentValue;}, 0);
}
return sum;
}
let i, j;
let arr = new Array(M).fill(0).map(() => new Array(N).fill(0));
let x = 1;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
arr[i][j] = x++;
document.write(sum(arr));
|
Java
public class SumOf2DArray {
public static int Findsum( int [][] arr) {
int ans = 0 ;
int M = arr.length;
int N = arr[ 0 ].length;
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
ans += arr[i][j];
}
}
return ans;
}
public static void main(String args[]) {
int M = 4 ;
int N = 4 ;
int x = 1 ;
int [][] arr = new int [M][N];
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
arr[i][j] = x;
x++;
}
}
System.out.println(Findsum(arr));
}
}
|
C#
using System;
public class GFG {
static int Findsum( int [, ] arr)
{
int ans = 0;
int M = arr.GetLength(0);
int N = arr.GetLength(1);
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
ans += arr[i, j];
}
}
return ans;
}
static public void Main()
{
int M = 4;
int N = 4;
int x = 1;
int [, ] arr = new int [M, N];
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
arr[i, j] = x;
x++;
}
}
Console.WriteLine(Findsum(arr));
}
}
|
Time Complexity: O(n*m) (where n = no. of rows and m = no. of column)
Auxiliary Space: O(1)
Another Approach : Using pointers
We can also use pointers to find the sum of elements in a 2D array. We can use a pointer to point to the first element of the array and iterate through each element in the array by incrementing the pointer.
Step by Step algorithm :
- Define a function named sum that takes a 2D array of integers as input and returns an integer value.
- In the sum function, declare a pointer ptr of type integer and assign it the address of the first element of the 2D array using &arr[0][0].
- Declare another pointer end of type integer and assign it the address of the element one beyond the last element of the 2D array, by adding 1 to the address of the last element of the array &arr[M-1][N-1]. This way we can make imaginary indexes.
- Declare an integer variable sum and initialize it to zero.
- Using a loop that iterates over the elements of the 2D array, add each element to sum by dereferencing the pointer p.
- Return the sum value.
C++
#include <iostream>
#include <numeric>
using namespace std;
#define M 4
#define N 4
int sum( int arr[M][N])
{
int * ptr = &arr[0][0];
int * end = &arr[M-1][N-1] + 1;
int sum = 0;
for ( int * p = ptr; p != end; p++) {
sum += *p;
}
return sum;
}
int main()
{
int i, j;
int arr[M][N];
int x = 1;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
arr[i][j] = x++;
cout << sum(arr) << endl;
return 0;
}
|
Python3
M = 4
N = 4
def sum (arr):
s = 0
for i in range (M):
for j in range (N):
s + = arr[i][j]
return s
if __name__ = = '__main__' :
arr = [[ 0 for j in range (N)] for i in range (M)]
x = 1
for i in range (M):
for j in range (N):
arr[i][j] = x
x + = 1
print ( sum (arr))
|
C#
using System;
class Program
{
const int M = 4;
const int N = 4;
static int Sum( int [,] arr)
{
int sum = 0;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
sum += arr[i, j];
}
}
return sum;
}
static void Main( string [] args)
{
int [,] arr = new int [M, N];
int x = 1;
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
arr[i, j] = x++;
}
}
Console.WriteLine(Sum(arr));
}
}
|
Javascript
let M = 4;
let N = 4;
function sum(arr){
let s = 0;
for (let i = 0; i < M; i++){
for (let j = 0; j < N; j++){
s += arr[i][j];
}
}
return s;
}
let arr = new Array(N);
for (let i = 0; i < N; i++){
arr[i] = new Array(M).fill(0);
}
let x = 1;
for (let i = 0; i < M; i++){
for (let j = 0; j < N; j++){
arr[i][j] = x;
x += 1;
}
}
console.log(sum(arr));
|
Java
import java.util.*;
public class GFG {
private static final int M = 4 ;
private static final int N = 4 ;
private static int sum( int [][] arr) {
int [] ptr = arr[ 0 ];
int [] end = arr[M - 1 ];
int sum = 0 ;
for ( int [] row : arr) {
for ( int val : row) {
sum += val;
}
}
return sum;
}
public static void main(String[] args) {
int [][] arr = new int [M][N];
int x = 1 ;
for ( int i = 0 ; i < M; i++) {
for ( int j = 0 ; j < N; j++) {
arr[i][j] = x++;
}
}
System.out.println(sum(arr));
}
}
|
Time Complexity: O(M*N)
This is because the function sum() iterates through each element of the matrix using a pointer, taking O(M*N) time.
Auxiliary Space: O(1)
This is because the function sum() uses a constant amount of extra space for the pointers and the sum variable, taking O(1) space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...