C++ Programs on Matrix LAB
C++ Programs on Matrix LAB
Program Explanation
1. The user is initially asked to enter the number of rows and columns of both the matrices.
LAB WORK Page 1
C++ Program of Matrices
2. If number of rows and columns of matrix A are not equal to number of rows and columns
of matrix B respectively, then matrices cannot be added. The program is exited.
3. Else if they are equal, the elements of both the matrices are entered and added.
4. The resultant matrix C is then printed.
Case 2:
Enter rows and column of matrix A: 2 3
Enter rows and column of matrix B: 3 2
Matrices cannot be added!
Case 3:
Enter rows and column of matrix A: 1 3
Enter rows and column of matrix B: 1 3
Enter elements of matrix A: 0 2 7
Enter elements of matrix B: 0 3 3
Sum of matrices
0 5 10
int main() {
int mat1[3][3], mat2[3][3], mat3[3][3];
cout << "Third Matrix = Second Matrix - First Matrix: " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << mat3[i][j] << "\t";
}
cout << endl;
}
}
$ g++ main.cpp
$ ./a.out
First Matrix:
To perform matrix multiplication, we multiply corresponding elements and sum them up:
Resultant Matrix C:
#include<iostream>
using namespace std;
int main () {
int r1, c1, r2, c2, i, j, k;
int A[5][5], B[5][5], C[5][5];
cout << "Enter number of rows and columns of matrix A : ";
cin >> r1 >> c1;
cout << "Enter number of rows and columns of matrix B : ";
cin >> r2 >> c2;
if (c1 != r2) {
cout << "Matrices cannot be multiplied!";
exit(0);
}
cout << "Enter elements of matrix A : ";
Program Explanation
1. The user is asked to enter number of rows and columns of matrix A and matrix B.
2. If number of columns of matrix A is not equal to number of rows of matrix B, then
matrices cannot be multiplied.
3. The program is exited using the function exit ().
4. Else if they are equal, matrices are multiplied using for loops and the product is stored in
matrix C.
5. The result is then printed.
Runtime Test Cases
Testcase 1: We enter the number of rows and columns for both matrices A and B. We
provide the elements for both matrices and check the product.
Enter number of rows and columns of matrix A : 2 2
Enter number of rows and columns of matrix B : 2 2
Enter elements of matrix A : 1 2 3 4
Enter elements of matrix B : 4 3 2 1
Product of matrices
8 5
20 13
Testcase 2: In this case, the columns of the first matrix are not equal to the rows of the second
matrix.
Enter number of rows and columns of matrix A : 1 3
Enter number of rows and columns of matrix B : 2 3
Matrices cannot be multiplied!
Testcase 3: In this case, we input the number of rows and columns for both matrices A and B.
Provide elements for Matrix A and Matrix B. Program performs multiplication and outputs the
product matrix.
Enter number of rows and columns of matrix A : 1 1
Enter number of rows and columns of matrix B : 1 3
Enter elements of matrix A : 3
Enter elements of matrix B : 1 2 3
Product of matrices
3 6 9
Program Explanation
1. The user is asked to enter the number of rows and columns of the matrix.
2. The elements of the matrix are asked to enter and stored in the matrix ‘A’.
3. The transpose is found by exchanging the rows with columns and columns with rows.
4. The original matrix and the transpose are both printed.
Runtime Test Cases
Case 1 :
Enter rows and columns of matrix : 3 3
Case 2 :
Enter rows and columns of matrix : 2 2
Enter elements of matrix : 1 2 3 4
Entered Matrix :
1 2
3 4
Transpose of Matrix :
1 3
2 4
Case 3 :
Enter rows and columns of matrix : 2 3
Enter elements of matrix : 0 2 4 6 8 10
Entered Matrix :
0 2 4
6 8 10
Transpose of Matrix :
0 6
2 8
4 10
Method 2:
The C++ program generates a transpose of a given matrix of any order. It takes the size of the
array from standard input and allocates memory for it using new[]. The elements are then
stored into the multidimensional array and the transpose is obtained using a conventional
method. The transpose is printed and the memory is finally deallocated using delete[].
/*
* C++ Program to Generate a Transpose of a Given Matrix of any order
*/
#include <iostream>
int main() {
int x, y;
delete[] arr;
}
$ a.out
Enter the order of matrix 3 3
1 2 3
4 5 6
7 8 9
Transpose Matrix
1 4 7
2 5 8
3 6 9
int main() {
int xsize, ysize, * swap;
int row1, row2;
$ gcc test.cpp
$ a.out
Enter the size of matrix: 3 3
Enter the matrix
1 2 3
4 5 6
7 8 9
Enter the rows to be interchanged: 1 2
New Matrix
4 5 6
1 2 3
7 8 9