Click here to Skip to main content
15,910,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#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 << " \n Matrix A: \n " << endl;
  for(i = 0; i < row1; ++i)
  {

  for(j = 0; j < co11; ++j)
    cout << A[i][j] << " ";
    cout << endl;
    
    
    myfile << A[i][j] << " ";
    myfile << endl;
    

}
  
  cout << "\n Matrix B: \n " <<endl;
  for(i = 0; i < row2; ++i)
  {
  for(j = 0; j < co12; ++j)
  cout << B[i][j] << " ";
  
  myfile << B[i][j] << " ";
  myfile << endl;
  
}

return 0;

}

}


What I have tried:

I have tried to fix it so many times, but didn't why it wouldn't show the matrix shape when I open it from the data file.
Posted
Updated 24-Apr-22 19:44pm
Comments
Sandeep Mewara 24-Apr-22 23:26pm    
What exactly are you trying to do? What are you expecting back from above code and what is the gap in that?

Seems you are setting up for matrix multiplication and current code so far is around taking the two matrix as input and displaying it back.
Patrice T 25-Apr-22 1:39am    
And you think it is possible to show expected and actual output ?

1 solution

Start by sorting out your indentation - that may help you work out what the problem is.
When I see code like this:
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; }
And this:
for(i = 0; i < row1; ++i)
for(j = 0; j < co11; ++j)
{
  cout << "Enter element A" << i + 1 << j + 1 << " : ";
  cin >> A[i][j];

}
And this:
  for(i = 0; i < row1; ++i)
  {

  for(j = 0; j < co11; ++j)
    cout << A[i][j] << " ";
    cout << endl;
    
    
    myfile << A[i][j] << " ";
    myfile << endl;
    

}
And this:
  for(i = 0; i < row2; ++i)
  {
  for(j = 0; j < co12; ++j)
  cout << B[i][j] << " ";
  
  myfile << B[i][j] << " ";
  myfile << endl;
  
}
It's pretty obvious that the code wasn't thought about too hard before hitting the keyboard, and that it's not obvious that what you are actually doing is right.
Sort out your indentation, add "{" and "}" even for single line blocks and then look at your code carefully to see what you end up with.
For example, this code:
  for(i = 0; i < row2; ++i)
  {
  for(j = 0; j < co12; ++j)
  cout << B[i][j] << " ";
  
  myfile << B[i][j] << " ";
  myfile << endl;
  
}
Is actually this:
for(i = 0; i < row2; ++i)
  {
  for(j = 0; j < co12; ++j)
      {
      cout << B[i][j] << " ";
      }
  myfile << B[i][j] << " ";
  myfile << endl;
  }
But that's not obvious from the original, and probably isn't what you wanted!
 
Share this answer
 
Comments
sugeous 25-Apr-22 1:56am    
already fix it, thank you so much for helping me out ✨
OriginalGriff 25-Apr-22 2:33am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900