//Row Reduced Echelon Form
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
class matrixOperation
{
private:
int row, col, i, j, k;
int m1[10][10], m2[10][10], ans[10][10];
float A[10][10];
int operation;
void read()
{
cout << "\n\nEnter the number of Rows and Columns of matrix : ";
cin >> row >> col;
cout << "\nEnter the " << row * col << " elements of first matrix : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cin >> m1[i][j];
}
}
cout << "\nEnter the " << row * col << " elements of second matrix : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cin >> m2[i][j];
}
}
}
void show()
{
cout << "\n\nThe Answer matrix is : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << ans[i][j] << " ";
}
cout << endl;
}
}
void add()
{
cout<<"\t\tYou Choose Matrix Addition\t\t";
read();
//calculating the sum matrix
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
ans[i][j] = m1[i][j] + m2[i][j];
}
}
show();
}
void subract()
{
cout<<"\t\tYou Choose Matrix Subtraction\t\t";
read();
//calculating the subtraction matrix
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
ans[i][j] = m1[i][j] - m2[i][j];
}
}
show();
}
void echelon()
{
cout<<"Enter the number of rows and column: ";
cin>>row>>col;
cout<<"Enter the value of matrix: \n";
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>A[i][j];
int lead = 0;
while (lead < row) {
float d, m;
for (int r = 0; r < row; r++) { // for each row ...
/* calculate divisor and multiplier */
d = A[lead][lead];
m = A[r][lead] / A[lead][lead];
for (int c = 0; c < col; c++) { // for each column ...
if (r == lead)
A[r][c] /= d; // make pivot = 1
else
A[r][c] -= A[lead][c] * m; // make other = 0
}
}
lead++;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout << setw(7) << setprecision(4) << A[i][j] << " ";
cout<<"\n";
}
cout<<"\n";
}
}
public:
void option()
{
cout<<"\t\tChoice a Matrix Operation\t\t\n\n";
cout<<"[1]-Matrix addition: \n" ;
cout<<"[2]-Matrix Subtraction: \n";
cout<<"[3]-Row Reduced Echelon Form: \n";
cin >> operation;
switch(operation)
{
case 1:
add();
break;
case 2:
subract();
break;
case 3:
echelon();
break;
default:
cout << "Error! The operation is not correct";
break;
}
}
};
int main()
{
matrixOperation operation;
operation.option();
return 0;
}