Write a function that will accept 2D array and its row column

A multi-dimensional array is one that has more than one dimension. 2-D and 3-D arrays are the most often utilised multi-dimensional arrays. Any higher-dimensional array can be thought of as an array of arrays. Chess Board is a classic example of a 2D Array. A chessboard is made up of a grid of 64 1×1 square boxes. A 2D array can be visualised in the same way. Every element in a 2D array has a row and column number assigned to it. Any element of the 2D array can be accessed in the same way as a record in an Excel file can be accessed using both the row and column numbers. When creating a Tic-Tac-Toe game, Chess, or simply storing image pixels, 2D arrays come in handy. We will discuss some basic operation of array by creating matrix using PYTHON. like Write A Function That Will Accept A 2-D Array And Its Row And Column using this we can form various problems as.

Q. Write a function that will accept a 2-D array and its row and column size as argument and find sum of rows and columns.

Solution.
#PYTHON
Row = int(input("Number of Rows : "))

Column = int(input("Number of Columns : "))
matrix = []
print("Input the vaues rowwise for matrix") 

for i in range(Row):        
   x = [] 
   for j in range(Column):     
        x.append(int(input())) 
   matrix.append(x) 
print("****The Matrix is****")  

for i in range(Row):
   for j in range(Column): 
       print(matrix[i][j], end = " ") 
    print(" ") 

#To calculate sum of each row
def row_sum() :
   sum = 0 
   print("\nRow\n") 
   for i in range(Row) : 
       for j in range(Column) : 
           sum += matrix[i][j] 
       print("Sum of the Row",i,"=",sum) 
        sum = 0 

#To calculate sum of each column 
def column_sum() :
   sum = 0 
   print("\nColumn:\n") 
   for j in range(Column) : 
       for i in range(Row) : 
           sum += matrix[i][j] 
       print("Sum of the Column",j,"=",sum) 
       sum = 0 

row_sum()
column_sum()

Output:

Number of Rows : 3
Number of Columns : 4

Input the vaues rowwise for matrix
3
2
4
5
1
2
3
2
5
4
6
7

****The Matrix is****
3 2 4 5 
1 2 3 2 
5 4 6 7  

Row
Sum of the row 0 = 14
Sum of the row 1 = 8
Sum of the row 2 = 22

Column
Sum of the column 0 = 9
Sum of the column 1 = 8
Sum of the column 2 = 13
Sum of the column 3 = 14

Q. Write a function in python to find the sum of both left and right diagonal elements from a two dimensional array (matrix).

Solution.

MAX = 100 

def DiagonalSum(mat, n):
   Left = 0 
   Right = 0 
   for i in range(0, n): 
       for j in range(0, n): 
           if (i == j): 
               Left += mat[i][j] 
           if ((i + j) == (n - 1)): 
                Right += mat[i][j] 

   print("Left Diagonal:", Left) 
    print("Right Diagonal:", Right) 

a = [[ 2, 4, 3, 3 ],
    [ 1, 2, 8, 2 ], 
    [ 1, 4, 1, 4 ], 
     [ 2, 3, 7, 2 ]] 

DiagonalSum(a, 4)

Output

Left Diagonal: 7
Right Diagonal: 17

Leave a Comment

Your email address will not be published. Required fields are marked *