Program to Find the Sum of all Elements in a 2D ArrayMatrix

Python Program to Find the Sum of all Elements in a 2D Array/Matrix

In the previous article, we have discussed Python Program to Display an Upper Triangular Matrix

Given a matrix and the task is to find the sum of all elements in a 2-dimensional array of the given matrix in Python.

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

Examples:

Example1:

Input:

Given Matix : 
1 6 4 
8 1 3 
1 6 2

Output:

The sum of all elements in 2 dimensional array for a given matrix is :
32

Example2:

Input:

Given Matix : 
0 8
3 7

Output:

The sum of all elements in 2 dimensional array for a given matrix is :
18

Program to Find the Sum of all Elements in a 2D Array in Python

Below are the ways to find the sum of all elements in a 2-dimensional array of the given matrix in Python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the matrix as static input and store it in a variable.
  • Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
  • Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
  • Take a variable say rslt_sum and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] to the above-initialized rslt_sum and store it in the same variable rslt_sum.
  • Print the variable rslt_sum to get the sum of all elements in a 2-dimensional array for a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Give the matrix as static input and store it in a variable.
mtrx = [[1, 6, 4], [8, 1, 3], [1, 6, 2]]
# Calculate the number of rows of the given matrix by
# calculating the length of the nested list using the len() function
# and store it in a variable mtrxrows.
mtrxrows = len(mtrx)
# Calculate the number of columns of the given matrix by
# calculating the length of the first list in the nested list
# using the len() function and store it in a variable mtrxcols.
mtrxcols = len(mtrx[0])
print("The sum of all elements in 2 dimensional array for a given matrix is :")
# Take a variable say rslt_sum and initialize its value to 0.
rslt_sum = 0
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
        # Inside the For loop, Iterate till the given number of columns using another
        # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Add the gvnmatrix[n][m] to the above-initialized
        # rslt_sum and store it in the same variable rslt_sum.
        rslt_sum += mtrx[n][m]
# Print the variable rslt_sum to get the sum of all elements in a 2 dimensional array for
# a given matrix
print(rslt_sum)

Output:

The sum of all elements in 2 dimensional array for a given matrix is :
32

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
  • Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
  • Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
  • Loop till the given number of rows using the For loop
  • Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
  • Add the above row elements list to gvnmatrix using the append() function.
  • Take a variable say rslt_sum and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
  • Add the gvnmatrix[n][m] to the above-initialized rslt_sum and store it in the same variable rslt_sum.
  • Print the variable rslt_sum to get the sum of all elements in a 2-dimensional array for a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
    # Inside the For loop, Give all the row elements of the given Matrix as a list using
    # the list(),map(),int(),split() functions and store it in a variable.
    l = list(map(int, input(
        'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))
    # Add the above row elements list to gvnmatrix using the append() function.

    mtrx.append(l)
    
print("The sum of all elements in 2 dimensional array for a given matrix is :")
# Take a variable say rslt_sum and initialize its value to 0.
rslt_sum = 0
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
        # Inside the For loop, Iterate till the given number of columns using another
        # Nested For loop(Inner For loop).
    for m in range(mtrxcols):
        # Add the gvnmatrix[n][m] to the above-initialized
        # rslt_sum and store it in the same variable rslt_sum.
        rslt_sum += mtrx[n][m]
# Print the variable rslt_sum to get the sum of all elements in a 2 dimensional array for
# a given matrix
print(rslt_sum)

Output:

Enter some random number of rows of the matrix = 2
Enter some random number of columns of the matrix = 2
Enter {2} elements of row {1} separated by spaces = 0 8
Enter {2} elements of row {2} separated by spaces = 3 7
The sum of all elements in 2 dimensional array for a given matrix is :
18

If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners as our expert team has designed them from scratch.