You are on page 1of 3

QUESTION 26:

Write a program to add two matrices.

ALGORITHM:

1. Declare and initialize 2 two-dimensional arrays a and b.


2. Calculate the number of rows and columns present in the array a (as dimensions of both the arrays
are same) and store it in variables rows and cols respectively.
3. Declare another array sum with the similar dimensions.
4. Loop through the arrays a and b, add the corresponding elements.
5. Display the elements of array sum.

PYTHON CODE:
QUESTION 27:

Write a program to find the transpose of a matrix.

ALGORITHM:

1. Declare and initialize a two-dimensional array a.


2. Calculate the number of rows and columns present in the matrix and store it variables rows and cols
respectively.
3. Declare another array t with reversed dimensions i.e. [cols][rows]. Array t will be used to store the
elements of the transposed matrix.
4. Loop through the array a and convert its rows into columns of matrix using [ i ][ j ] = a[ j ][ i ];
5. Finally, display the elements of matrix t.

PYTHON CODE:
QUESTION 28:

Write a program to find the diagonal elements of a matrices.

ALGORITHM:

1. Define the principal diagonal elements of the matrix a.


2. Primary /principal diagonal elements formed is by the elements a11, a22, a33, a44.

3. Calculate the number of rows and columns present in the matrix and store it variables rows and cols
respectively.
4. Check if the principal diagonal element satisfies the condition that row=column.
5. Print the matrix first and then print the diagonal elements.

PYTHON CODE:

You might also like