Answer to Question #187181 in C++ for xie

Question #187181

Write a complete c++ program code which can multiply any two matrices. The two matrices must be any size. The elements of the two matrices is read from a file ex:matrix.dat. The result will be kept in a separate file call ex:matrix.res. Your program must be able to check whether the multiplication of the matrix can be done. show how to create a matrix.dat file so that it can read the file.


1
Expert's answer
2021-04-30T23:13:55-0400
// The matrix.dat file stores matrices in the following format
// 3,3,1,2,3,0,1,0,7,0,1
// 3,3,1,2,3,4,5,6,7,8,9
// The first two digits represent the dimensions of the matrix 
// while the rest represent the elements
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Matrix{
    int rows, cols, mat[10][10];
    public:
    Matrix(){
        rows = 0;
        cols = 0;
    }
    void getdata(string s){
        int start = 0, stop, arr[s.length()];
        for(int i = 0, j = 0; i < s.length(); i++){
            if(s[i] == ',' || i == s.length() - 1){
                stop = i;
                if(i == s.length() - 1) stop = s.length();
                arr[j] = stoi(s.substr(start, stop - start));
                j++;
                start = stop + 1;
            }
        }
        rows = arr[0];
        cols = arr[1];
        for(int i = 0, k = 2; i < rows; i++)
            for(int j = 0; j < cols; j++, k++)
                mat[i][j] = arr[k];
    }
    void putdata(string filename){
        fstream matfile;
        matfile.open(filename, ios::out | ios:: trunc);
        if(rows == 0 && cols == 0)
            matfile<<endl<<"\nNo data present";
        else{
            for(int i = 0; i < cols; i++){
                for(int j = 0; j < rows; j++)
                    matfile<<mat[i][j]<<"\t";
                matfile<<endl;
            }
        }
    }
    Matrix operator*(const Matrix& M){
        Matrix result;
        result.rows = this->rows;
        result.cols = M.cols;
        if(this->cols == M.rows){
        for(int i = 0; i < this->rows; i++)
            for(int j = 0; j < M.cols; j++){
                result.mat[i][j] = 0;
                for(int k = 0; k < this->cols; k++){
                    result.mat[i][j] += this->mat[i][k] * M.mat[k][j];
                }
            }
        return result;
        }
        else{
            cout<<"Cannot multiply these matrices";
            return result;
        }
    }
};
int main(){
    Matrix A, B;
    fstream matfile;
    string s;
    matfile.open("matrix.dat", ios::in);
    if(!matfile){
        cout<<"Error opening file";
    }
    else{
        getline(matfile, s);
        A.getdata(s);
        getline(matfile, s);
        B.getdata(s);
        Matrix C = A * B;
        C.putdata("matrix.res");
        matfile.close();
    }
    return 0;
}

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