Answer:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile("Matrix.dat", ios::app);
int A[100][100], B[100][100], row1, co11, row2, co12, i, j, k;
cout << "Enter no. of rows for matrix A: ";
cin >> row1;
cout << "Enter no. of columns for matrix A: ";
cin >> co11;
cout << "Enter no. columns for matrix B: ";
cin >> row2;
cout << "Enter no. of rows for matrix B: ";
cin >> co12;
myfile << row1 << endl;
myfile << co11 << endl;
myfile << row2 << endl;
myfile << co12 << endl;
myfile << endl;
while (co11 != row2)
{
cout << "Sorry! Column of matric A is not equal to row of matrix B.";
cout << "Please enter rows and columns for matrix A: ";
cin >> row1 >> co11;
cout << "Please enter rows and column for the maxtrix B: ";
cin >> row2 >> co12;
}
//read matrices
//Storing elements of matrix A
cout << endl << "Enter elements of matrix A:" << endl;
for (i = 0; i < row1; ++i)
for (j = 0; j < co11; ++j)
{
cout << "Enter element A" << i + 1 << j + 1 << " : ";
cin >> A[i][j];
}
//Storing elements of matrix B
cout << endl << "Enter elements of matrix B:" << endl;
for (i = 0; i < row2; ++i)
for (j = 0; j < co12; ++j)
{
cout << "Enter element B" << i + 1 << j + 1 << " : ";
cin >> B[i][j];
}
//Displaying output of two matrix
{
cout << " \\ Matrix A: \\ " << endl;
for (i = 0; i < row1; ++i)
{
for (j = 0; j < co11; ++j)
{
cout << A[i][j] << " ";
myfile << A[i][j] << " ";
}
myfile << endl;
cout << endl;
}
myfile << endl;
cout << "\\ Matrix B: \\ " << endl;
for (i = 0; i < row2; ++i)
{
for (j = 0; j < co12; ++j)
{
cout << B[i][j] << " ";
myfile << B[i][j] << " ";
}
myfile << endl;
cout << endl;
}
return 0;
}
}
Step-by-step explanation:
I believe the issue was just a matter of when << endl; was being used.
My results:
2
2
2
2
1 2
3 4
5 6
7 8
I'm not exactly how this is supposed to look, but based on the image, this seems like the target output format.
Please test this code. If it is not what you were looking for, then tell me what's wrong with the output.