Write c++ program code that can multiply any two matrices. The matrices read from a file. The result kept in a separate file
#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();
}
Comments
Leave a comment