0% found this document useful (0 votes)
12 views

C++ Programs on Matrix LAB

The document provides C++ programs for various matrix operations including addition, subtraction, multiplication, and finding the transpose of matrices. Each section includes problem descriptions, solutions, source code, and runtime test cases to demonstrate the functionality. The programs handle user input for matrix dimensions and elements, and perform operations based on the specified conditions.

Uploaded by

Hrishika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C++ Programs on Matrix LAB

The document provides C++ programs for various matrix operations including addition, subtraction, multiplication, and finding the transpose of matrices. Each section includes problem descriptions, solutions, source code, and runtime test cases to demonstrate the functionality. The programs handle user input for matrix dimensions and elements, and perform operations based on the specified conditions.

Uploaded by

Hrishika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C++ Program of Matrices

C++ Program to Add Two Matrices


Problem Description
The program adds two matrices and prints the resultant output.
Problem Solution
1. The program checks the number of rows and columns of the two matrices.
2. If they are not equal, the matrices cannot be added and the program is exited.
3. Else the matrices are added using for loops.
4. The resultant matrix is then printed.
5. Exit.
C++ Program/Source code
Here is the source code of C++ Program to Add Two Matrices and Print the Output. The program
output is shown below.
#include<iostream>
using namespace std;
int main () {
int m, n, p, q, i, j, A[5][5], B[5][5], C[5][5];

cout << "Enter rows and column of matrix A : ";


cin >> m >> n;

cout << "Enter rows and column of matrix B : ";


cin >> p >> q;

if ((m != p) && (n != q)) {


cout << "Matrices cannot be added!";
exit(0); // return to operating system
}

cout << "Enter elements of matrix A : ";


for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];

cout << "Enter elements of matrix B : ";


for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
cin >> B[i][j];

for (i = 0; i < m; i++)


for (j = 0; j < n; j++)
C[i][j] = A[i][j] + B[i][j];

cout << "Sum of matrices\n";


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
cout << C[i][j] << " ";
cout << "\n";
} // outer endfor
return 0;
}

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.

Runtime Test Cases


Case 1 :
Enter rows and column of matrix A: 2 2
Enter rows and column of matrix B: 2 2
Enter elements of matrix A: 1 2 3 4
Enter elements of matrix B: 4 3 2 1
Sum of matrices
5 5
5 5

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

LAB WORK Page 2


C++ Program of Matrices

C++ Program to Subtract Two Matrices


The C++ Program which demonstrates subtraction of two matrices of same order. The program
initializes the matrices, subtracts each corresponding element of the two matrices and puts the
value in the third matrix.
Here is source code of the C++ program which demonstrates subtraction of two matrices of same
order.
/*
* C++ program to demonstrate matrix subtraction
*/
#include<iostream>
using namespace std;

int main() {
int mat1[3][3], mat2[3][3], mat3[3][3];

/* Initializing Mat1 and Mat2 */


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
mat1[i][j] = i + 1;
mat2[i][j] = j + 1;
}
}

/* Adding matrices Mat1 and Mat2 */


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
mat3[i][j] = mat2[i][j] - mat1[i][j];
}
}

cout << "First Matrix : " << endl;


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << mat1[i][j] << "\t";
}
cout << endl;
}

cout << "Second Matrix : " << endl;


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << mat2[i][j] << "\t";
}
cout << endl;
}

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:

LAB WORK Page 3


C++ Program of Matrices
1 1 1
2 2 2
3 3 3
Second Matrix:
1 2 3
1 2 3
1 2 3
Third Matrix = Second Matrix - First Matrix:
0 1 2
-1 0 1
-2 -1 0

LAB WORK Page 4


C++ Program of Matrices

Matrix Multiplication in C++


Matrix multiplication in C++ is the process of multiplying two matrices to obtain a new matrix.
It involves multiplying corresponding elements and summing them up to form the resulting
matrix.
Example:
Suppose we have two matrices:

To perform matrix multiplication, we multiply corresponding elements and sum them up:
Resultant Matrix C:

The resultant matrix C is the product of matrices A and B.


Problem Description
Write a C++ Program to Perform Matrix Multiplication.
Problem Solution
1. The program takes two matrices and multiplies them.
2. If number of columns of matrix A is not equal to number of rows of matrix B, then matrices
cannot be added.
3. The program is exited.
4. Else they are multiplied and the result is printed.
5. Exit.
C++ Program/Source code
Here is the source code of C++ Program to Multiply Two Matrices.
/*
* C++ program to perform matrix multiplication
*/

#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 : ";

for (i = 0; i < r1; i++)


for (j = 0; j < c1; j++)

LAB WORK Page 5


C++ Program of Matrices
cin >> A[i][j];

cout << "Enter elements of matrix B : ";

for (i = 0; i < r2; i++)


for (j = 0; j < c2; j++)
cin >> B[i][j];

for (i = 0; i < r1; i++) {


for (j = 0; j < c2; j++) {
C[i][j] = 0;
for (k = 0; k < r2; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Product of matrices\n";

for (i = 0; i < r1; i++) {


for (j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << "\n";
}
return 0;
}

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

LAB WORK Page 6


C++ Program of Matrices

C++ Program to Find Transpose of a Matrix


This is a C++ Program to Find the Transpose of a Matrix.
Method 1:
Problem Description
The program takes a matrix and prints the transpose of the matrix. In a transpose matrix, rows
become columns and vice versa.
Problem Solution
1. Initially, the number of rows and columns of the matrix are entered, followed by the matrix
elements.
2. The transpose of the matrix is printed.
3. Exit.
C++ Program/Source code
Here is the source code of C++ Program to Find the Transpose of a Matrix.
#include<iostream>
using namespace std;
int main () {
int A[10][10], m, n, i, j;
cout << "Enter rows and columns of matrix : ";
cin >> m >> n;

cout << "Enter elements of matrix : ";


for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];

cout << "Entered Matrix : \n ";


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
cout << A[i][j] << " ";

cout << "\n ";


}

cout << "Transpose of Matrix : \n ";


for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
cout << A[j][i] << " ";

cout << "\n ";


}
return 0;
}

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

LAB WORK Page 7


C++ Program of Matrices
Enter elements of matrix : 9 8 7 6 5 4 3 2 1
Entered Matrix :
9 8 7
6 5 4
3 2 1
Transpose of Matrix :
9 6 3
8 5 2
7 4 1

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;

cout << "Enter the order of matrix ";


cin >> x >> y; // read input

double** arr = new double*[x]; // create dynamic array

for (int i = 0; i < y; i++) {


arr[i] = new double[y];
}

for (int i = 0; i < x; i++) {


for (int j = 0; j < y; j++) {
cin >> arr[i][j]; // matrix element
}
}

for(int i = 0; i < x; i++){


for(int j = i; j < y; j++) {

LAB WORK Page 8


C++ Program of Matrices
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}

cout << "Transpose Matrix" << endl;


for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
cout << arr[i][j] << " ";
}
cout << "\n";
}

for(int i = 0 ; i < y; i++) {


delete[] arr[i];
}

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

LAB WORK Page 9


C++ Program of Matrices

C++ Program to Swap Rows of a Matrix


The C++ program interchanges the rows of a matrix. The program takes the matrix from the input
and interchanges the row numbers given from input. The changed matrix is finally printed.
/*
* C++ Program to interchange the rows of a matrix
*/
#include <iostream>
using namespace std;

int main() {
int xsize, ysize, * swap;
int row1, row2;

cout << "Enter the size of matrix: ";


cin >> ysize >> xsize;

int ** matrix = new int*[ysize]; // initializing columns for each row

for(int i = 0; i < ysize; i++) {


matrix[i] = new int[xsize];
}
// taking value corresponding to each cell from input
cout << "Enter the matrix\n";
for(int i = 0; i < ysize; i++) {
for(int j = 0; j < xsize; j++) {
cin >> matrix[i][j];
}
}

cout << "Enter the rows to be interchanged: ";


cin >> row1 >> row2;

// interchanging the rows


swap = matrix[row1 - 1];
matrix[row1 - 1] = matrix[row2 - 1];
matrix[row2 - 1] = swap;

// printing the values


cout << "New Matrix\n";
for(int i = 0; i < ysize; i++) {
for(int j = 0; j < xsize; j++) {
cout << matrix[i][j] << " ";
}
cout << "\n";
}
}

$ 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

LAB WORK Page 10

You might also like