Write a C program to multiply matrix A and matrix B of variable. For both A and B, the size of the matrix will be given along with the entries of the matrix in two input files, inA.txt and inB.txt. The first line of the input file will contain the number of rows followed by the number of columns of the matrix. The entries of the matrix are listed on the next line in row-major order. Print the output matrix C to outC.txt in the same format as input files. Be sure to include comments in your code.

Respuesta :

Answer:

Code:

    #include <iostream>

    #include <fstream>

    #include <cctype>

    #include <cmath>

    #include <vector>

    #include <float.h>

   

    using namespace std;

   

    typedef vector<vector<double> > C2DArray;

   

    void Matrix_Mult(bool* error_flag, const int mRows, const int nCols, const int pCols, const C2DArray A_Matrix, const C2DArray B_Matrix, C2DArray& C_Matrix) {      

     int i = A_Matrix.size(), j = A_Matrix[0].size(), k = B_Matrix[0].size();

     double dummy;

     if ((j != B_Matrix.size()) || (i != mRows) || (j != nCols) || (k != pCols))  *error_flag = true;

   

     if ((C_Matrix.size() != i) || (k != C_Matrix[0].size()))  *error_flag = true;

   

     if (!(*error_flag)){

     for (k = 0; k < mRows; k++) { // Do each row of A with each column of B

      for (i = 0; i < pCols; i++) {

       dummy = 0.0;

       for (j = 0; j < nCols; j++) {

        dummy += A_Matrix[k][j]*B_Matrix[j][i];

          } // End for j

          C_Matrix[k][i] = dummy;

      } // End for i

     } // End for k

     }

   

        return;

    } // End Matrix_Mult

   

    int main()

    {char rflag = 0; //Readiness flag  

   

    cout << "                 Mat_Mul1 (7 June 2013)\n";

    cout << "=========================================================================== \n";

    cout << "This program multiplies two matrices, [A] and [B]: [A][B] = [C].\n";

    cout << "[A] is M X N, [B] is N X P, and [C] is M X P\n";

    cout << "The dimensions of the [A] and [B] matrices, M, N, and P, should \n";

    cout << "have been saved beforehand in a file named mulmatdat.\n";

    cout << "mulmatdat should be in the same folder as the Mat_Mul1 executable.\n";

    cout << "The next values of mulmatdat should be the entries for matrix [A],\n";

    cout << "with data for row 1 first, then row 2, then row 3, etc.\n";

    cout << "The entries for the [B] matrix should follow, in the same order.\n";

    cout << "\nThe data is assumed to be of type double. Variables used within this program\n";

    cout << "are type double.\n";

    cout << "\nThe output is written to the file mulmatout.txt.\n";

   

    cout << "\nIs everything ready (are you ready to continue?)? If yes, Enter y. \n";

    cout << "Otherwise Enter any other key. \n";

    cin >> rflag;

   

    if (toupper(rflag) == 'Y') {

    C2DArray A, B, C; // A, B, and C Matrices

    int i, j, mDim, nDim, pDim; // Array index variables and matrix dimensions

    bool erFlag = false;  // Error flag

   

    ifstream in("mulmatdat.txt", ios::in);

   

    if (!in) {

            cout << "\nCannot open the input file.\n";

         cout << "\nEnter any key to continue. \n";

         cin >> rflag;

            return 0;

    }

   

    in >> mDim >> nDim >> pDim; //Input the Matrices' dimensions from the file

     

    if ((mDim < 1) || (nDim < 1) || (pDim < 1)){

            cout << "\nInvalid dimension entered. Program terminated. \n";

         cout << "\nEnter any key to continue. \n";

         cin >> rflag;

            in.close(); //Close the input file before terminating

            return 0;

    }

   

    ofstream out("mulmatout.txt", ios::out);

    if (!out) {

            cout << "\nCannot open the output file. Program terminated.\n";

         cout << "\nEnter any key to continue. \n";

         cin >> rflag;

            in.close(); //Close the input file before terminating

            return 0;

    }

   

    // Beginning of try block, if vector re-sizing unsuccessful

    try {

   

           // Resize the arrays to the appropriate sizes

            A.resize(mDim); // M rows

         B.resize(nDim); // N rows

         C.resize(mDim); // M rows

            for (i = 0; i < mDim; i++){

               A[i].resize(nDim); // M columns

            C[i].resize(pDim); // P columns

            } // End for i

   

         for (i = 0; i < nDim; i++){

               B[i].resize(pDim); // P columns

            } // End for i

   

    } // End of try block

   

    catch (bad_alloc& xa) { // Catch block, for exceptions

   

            in.close();

            out.close();

            cerr << "\nIn catch block, so an exception occurred: " << xa.what() << "\n";

            cout << "\nEnter any key to continue. \n";

            cin >> rflag;

            return 0;

   

    } // End of catch block

   

    for (i = 0; i < mDim; i++){ //Input the A Matrix from the file

            for (j = 0; j < nDim; j++){

              in >> A[i][j];

            }//End for j

    }//End for i

   

    for (i = 0; i < nDim; i++){ //Input the B Matrix from the file

            for (j = 0; j < pDim; j++){

             in >> B[i][j];

            }//End for j

    }//End for i

   

    in.close(); //Close the input file

   

    Matrix_Mult(&erFlag, mDim, nDim, pDim, A, B, C);

   

    if (erFlag){

      cout << "Inconsistent data sent to Matrix_Mult routine. Matrix multiplication NOT performed. Check data before running again.\n";

    }

    else {

   

     out.precision(DBL_DIG);

   

     out << "\nThe A matrix follows:\n";

     out << "\n";

     for (i = 0; i < mDim; i++){

      for (j = 0; j < nDim; j++){

     out << A[i][j] << " ";

      } // End for j

      out << "\n";

     }//End for i

   

     out << "\nThe B matrix follows:\n";

     out << "\n";

     for (i = 0; i < nDim; i++){

      for (j = 0; j < pDim; j++){

     out << B[i][j] << " ";

      } // End for j

      out << "\n";

    }//End for i

   

     out << "\nThe product matrix, C, follows:\n";

     out << "\n";

     for (i = 0; i < mDim; i++){

      for (j = 0; j < pDim; j++){

       out << C[i][j] << " ";

      } // End for j

      out << "\n";

     }//End for i

   

     cout << "\nDone! The solution is in the text file mulmatout.txt \n";

    } // End else !erFlag

    out.close();

    }

    else cout << "\nNot ready. Try again when ready with information. \n";

    cout << "\nEnter any key to continue. \n";

    cin >> rflag;

    return 0;

    }