ICT role in 21st century education and it's challenges.
SPL 12.1 | Multi Dimensional(Two) Array Practice Problems
1. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Structured Programming Language
Arrays in C (1D & 2D)
Problem 1: Write a program that will take input two matrices and check their equality.
Input:
First line represents the no of rows and columns of the first matrix.
Then take input the elements of that matrix.
Next, you will take input the no of rows and columns of the second matrix.
Then take input the elements of that second matrix.
Output:
Yes (if both matrices are same)
No (otherwise)
Sample I/O:
input:
2 3
1 2 3
4 5 6
2 3
1 2 3
4 5 6
Output:
Yes
2. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 2: Write a program that will take input a matrix and show the transpose matrix in the output.
Note: The transpose of one matrix is another matrix that is obtained by using rows from the first matrix as
columns in the second matrix.
For example, it is easy to see that the transpose of matrix A is A'. Row 1 of matrix A becomes column 1 of A'; row
2 of A becomes column 2 of A' and row 3 of A becomes column 3 of A'.
A =
111 222
333 444
555 666
A' =
111 333 555
222 444 666
Note that the order of a matrix is reversed after it has been transposed. Matrix A is a 2 x 3 matrix, but matrix A' is
a 3 x 2 matrix.
Input:
First line represents the no of rows and columns of the first matrix.
Then take input the elements of that matrix.
Output:
The transpose matrix.
Sample I/O:
input:
2 3
1 2 3
4 5 6
Output:
1 4
2 5
3 6
3. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 3: Write a program that will take input a matrix and check whether it is an Identity matrix or not.
Note: The identity matrix is a square matrix which contains ones along the main diagonal (from the top left to the
bottom right), while all its other entries are zero. Such a matrix is of the form given below:
Input:
First line represents the no of rows and columns of the first matrix.
Then take input the elements of that matrix.
Output:
Yes (if identity)
No (otherwise)
Sample I/O:
input:
3 3
1 0 0
0 1 0
0 0 1
Output:
Yes
4. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 4: Write a program to find sum of elements of each column of a matrix.
Input:
First line represents the no of rows and columns of the first matrix.
Then take input the elements of that matrix.
Output:
a list representing the sum of elements of each column
Sample I/O:
input:
2 3
1 2 3
4 5 6
Output:
5 7 9
5. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 5: Write a program that will multiply two matrices and shows the final output matrix.
Input:
first line represents the rows and columns no of the first matrix.
Next take input the elements of the first matrix
Then, take input the rows and columns no of the second matrix.
Next take input the elements of the second matrix.
Output:
Invalid Dimension (if multiplication not possible due to invalid dimension)
or, show the product matrix.
Sample I/O:
input:
2 3
1 2 3
4 5 6
3 3
1 2 3
4 5 6
7 8 9
Output:
30 36 42
66 81 96
6. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 6: Write a C program to interchange the diagonals of a matrix.
Note:
Input:
first line represents the rows and columns no of the matrix. (row number = column number)
Next take input the elements of the matrix
Output:
diagonal interchanged matrix
Sample I/O:
input:
3 3
1 2 3
4 5 6
7 8 9
Output:
3 2 1
4 5 6
9 8 7
7. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 7: Write a C program that finds the sum of lower triangular matrix.
Note:
Input:
first line represents the rows and columns no of the matrix. (row number = column number)
Next take input the elements of the matrix.
Output:
an integer representing the sum of elements of lower triangular matrix.
Sample I/O:
input:
3 3
1 2 3
4 5 6
7 8 9
Output:
34
8. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 8: [Tic-Tac-Toe Win checker] Given a (3 X 3) matrix representing tic-tac-toe game board.
Note:
Input:
first line represents the rows and columns no of the matrix.
Next take input the elements (only ‘O’ or ‘X’ or ‘.(dot) ’) of the matrix.
Output:
Win O
or, Win X
or, Tie
Sample I/O:
input:
3 3
O O O
X O X
X . .
Output:
Win O
9. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 9: Write a C program that will take input a matrix as input and check whether it is symmetric or not.
Note:
Input:
first line represents the rows and columns no of the matrix. (row number = column number)
Next take input the elements of the matrix.
Output:
Yes (if symmetric)
No (otherwise)
Sample I/O:
input:
3 3
9 5 3
5 7 4
3 4 2
Output:
Yes
10. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 10: [Scarecrow Problem, 1D array] given an 1D array of the
following format, where F represents fields and S represents scarecrows.
F F F S F S F F F S
Input: take input an integer (n) representing the no of maximum fields a
scarecrow can guard.
Output: find out the array cells that the scarecrow can guard and replace the F to G.
The output array is shown below: for n=1
F F G S G S G F G S
11. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 11: Write a program that will take n integers into an array A and m positive integers into array B. Finally
find the intersection (set operation) of array A and B.
Input: first take input the size (n) of array A and then take input all the elements of A
Then take input the size of array B and then take input all the elements of B.
Output: an array holding the elements after intersection of array A and B.
Sample input Sample output
8
7 8 1 5 2 6 4 3
6
1 3 6 0 9 2
1 2 6 3
3
1 2 3
2
4 5
Empty set
12. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 12: Write a program that will take n integers into an array A and m positive integers into array B. Finally
find the union (set operation) of array A and B.
Input: first take input the size (n) of array A and then take input all the elements of A
Then take input the size of array B and then take input all the elements of B.
Output: an array holding the elements after union of array A and B.
Sample input Sample output
8
7 8 1 5 2 6 4 3
6
1 3 6 0 9 2
7 8 1 5 2 6 4 3 0 9
3
1 2 3
2
4 5
1 2 3 4 5
13. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Problem 13: Write a program that will take n integers into an array and reverse all the integers within that array.
Finally print them all from 0 indexe to last valid index.
Input: first line of the input represents the size of array A then takes input all the elements of array A.
Sample input Sample output
5
1 2 3 4 5
5 4 3 2 1
6
2 8 3 9 0 1
1 0 9 3 8 2
Problem 14: Write a program that will take input n integers into an array A and then m integers into an array B.
Now swap all elements between array A and B. Finally show all elements of both array A and B.
Input: first line of the input represents the size of array A and then take input all the elements of array A
the next line represents the size of array B and then take input all the elements of array B.
Sample input Sample output
8
7 8 1 3 2 6 4 3
3
3 2 1
Array A : 3 2 1
Array B : 7 8 1 3 2 6 4 3