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 rowdef row_sum() :sum = 0print("\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 columndef column_sum() :sum = 0print("\nColumn:\n")for j in range(Column) :for i in range(Row) :sum += matrix[i][j]print("Sum of the Column",j,"=",sum)sum = 0row_sum()column_sum()

Output:

Number of Rows : 3Number of Columns : 4Input the vaues rowwise for matrix324512325467****The Matrix is****3 2 4 51 2 3 25 4 6 7RowSum of the row 0 = 14Sum of the row 1 = 8Sum of the row 2 = 22ColumnSum of the column 0 = 9Sum of the column 1 = 8Sum of the column 2 = 13Sum 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 = 100def DiagonalSum(mat, n):Left = 0Right = 0for 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: 7Right Diagonal: 17