Answer to Question #187078 in C++ for Allan

Question #187078

Write c++ program code that can multiply any two matrices. The matrices read from a file. The result kept in a separate file


1
Expert's answer
2021-04-29T05:41:33-0400
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib>

int main()
{
    int mul[3][3];
   ifstream indata; // data is like cin
   int num[3][3]; // variable for input value
   indata.open("example.dat"); // opens the file
   if(!indata) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
indata >> num[][];
indata.close();
   ifstream indata1; // indata is like cin
   int num1[3][3]; // variable for input value
   indata.open("example1.dat"); // opens the file read secong file
   if(!indata1) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
indata1 >> num1[][];
indata1.close();

for(i=0;i<3;i++){    
  for(j=0;j<3;j++){    
      mul[i][j]=0;    
      for(k=0;k<3;k++){    
        mul[i][j]+=num[i][k]*num1[k][j];    
      }    
    }   
 }    

ofstream MyFile("filename.txt");   // create a file to save product of matrix
MyFile << mul[][];
MyFile.close();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS