Find the sum of all elements in a 2D Array

Problem Statement:- Program to Find the sum of all elements in a 2d array or Matrix.

Sample Input/Output:-


Sample Input First: 2 2

3 4 5 6

Sample Output First: 

18

Sample Input Second:3 3 

4 5 6 7 8 9 2 3 4

Sample Output Second: 

48


Explanation:- 


For 1st output: matrix[0][0]+matrix[0][1]+matrix[1][0]+matrix[1][1]=3+4+5+6=18


For 2nd output: matrix[0][0]+matrix[0][1]+matrix[0][2]+matrix[1][0]+matrix[1][1]+matrix[1]            [2]+matrix[2][0]+matrix[2][1]+matrix[2][2]=4+5+6+7+8+9+2+3+4=48 

Data requirement:-


  Input Data:- row_size, col_size, matrix[][]


  Output Data:-sum


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to Find the sum of all elements in a 2D Array or Matrix.


Code:


#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } //Calculate sum of given matrix Elements int sum=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sum+=matrix[i][j]; } } //Display The Sum Of Given Matrix Elements printf("Sum of the Given Matrix Elements is: %d",sum); }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 3 4 5 6 Sum of the Given Matrix Elements is: 18


Program in C++

Here is the source code of the C++ Program to Find the sum of all elements in a 2D Array or Matrix.


Code:


#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } //Calculate sum of given matrix Elements int sum=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sum+=matrix[i][j]; } } //Display The Sum Of Given Matrix Elements cout<<"Sum of the Given Matrix Elements is: "<<sum; }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 4 5 6 7 8 9 2 3 4 Sum of the Given Matrix Elements is: 48


Program in Java

Here is the source code of the Java Program to Find the sum of all elements in a 2D Array or Matrix.


Code:


import java.util.Scanner; public class SumOfMatrixElement { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } //Calculate sum of given matrix Elements int sum=0; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sum+=matrix[i][j]; } } //Display The Sum Of Given Matrix Elements System.out.print("Sum of the Given Matrix Elements is: "+sum); cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:3 Enter the Matrix Element: 4 5 6 10 2 3 Sum of the Given Matrix Elements is: 30



Program in Python

Here is the source code of the Python Program to Find the sum of all elements in a 2D Array or Matrix.


Code:


# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) # Calculate sum of given matrix Elements sum=0 for i in range(0,row_size): for j in range(0,col_size): sum+=matrix[i][j] # Display The Sum Of Given Matrix Elements print("Sum of the Given Matrix Elements is: ",sum)


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 4 5 6 7 2 3 Sum of the Given Matrix Elements is: 27 



Most Recommend Questions:-







More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question


Post a Comment

0 Comments