Write a C++ program using the concept of OOP for the subtraction of two Matrices.
Here is the algorithm for subtracting two matrices:
Start the program by defining the class for the matrix, including the data members for the rows, columns, and elements of the matrix.
Create two constructors, one for initializing the matrix with a specific number of rows and columns, and another for initializing the matrix with a set of values.
Create a member function for subtracting two matrices. This function should take two matrices as arguments and subtract them element by element, storing the result in a new matrix.
Create a member function for displaying the matrix, which will display the rows, columns, and all the elements of the matrix.
In the main function, create two matrices by using the class constructors and initialize them with specific values.
Call the subtraction function, passing the two matrices as arguments and storing the result in a new matrix.
Finally, call the display function to output the result matrix.
Here is an example implementation of the algorithm in C++:
#include <iostream>
using namespace std;
class Matrix {
private:
int rows;
int columns;
int** elements;
public:
Matrix(int rows, int columns) {
this->rows = rows;
this->columns = columns;
elements = new int*[rows];
for (int i = 0; i < rows; i++) {
elements[i] = new int[columns];
}
}
Matrix(int rows, int columns, int** elements) {
this->rows = rows;
this->columns = columns;
this->elements = elements;
}
Matrix operator-(Matrix const &matrix) {
if (rows != matrix.rows || columns != matrix.columns) {
cout << "Error: Cannot subtract matrices of different sizes." << endl;
exit(1);
}
int** newElements = new int*[rows];
for (int i = 0; i < rows; i++) {
newElements[i] = new int[columns];
for (int j = 0; j < columns; j++) {
newElements[i][j] = elements[i][j] - matrix.elements[i][j];
}
}
return Matrix(rows, columns, newElements);
}
void displayMatrix() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << elements[i][j] << " ";
}
cout << endl;
}
}
};
int main() {
int rows, columns;
cout << "Enter number of rows and columns for both matrices:" << endl;
cin >> rows >> columns;
cout << "Enter elements of first matrix:" << endl;
int** matrix1Elements = new int*[rows];
for (int i = 0; i < rows; i++) {
matrix1Elements[i] = new int[columns];
for (int j = 0; j < columns; j++) {
cin >> matrix1Elements[i][j];
The input of the above code is two integers, representing the number of rows and columns for both matrices. Then it takes elements of first matrix, followed by elements of the second matrix by using nested for loop.
The output of the above code is the result matrix obtained by subtracting the elements of second matrix from the first matrix. The resulting matrix will be displayed row by row and element by element on the console. Also, if the matrices have different number of rows or columns the program will print an error message "Error: Cannot subtract matrices of different sizes." and exit with status code 1. It does not return anything.*/ ======Input==== Enter number of rows and columns for both matrices: 3 3 Enter elements of first matrix: 1 2 3 4 5 6 7 8 9 Enter elements of second matrix: 9 8 7 6 5 4 3 2 1 =========Output======== -8 -6 -4 -2 0 2 4 6 8 */Explanation: The program takes two integers as input, which are the number of rows and columns for the matrices. In this example, the matrices are both 3x3. Then the program prompts for the elements of the first matrix and the second matrix. The first matrix is [1 2 3; 4 5 6; 7 8 9] and the second matrix is [9 8 7; 6 5 4; 3 2 1]. The program then subtracts the elements of the second matrix from the first matrix, element by element and stores the result in a new matrix. The result matrix is [-8 -6 -4; -2 0 2; 4 6 8], which the program then displays row by row and element by element on the console.
No comments:
Post a Comment