1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
navik [9.2K]
4 years ago
7

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 wi

th 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.
Computers and Technology
1 answer:
victus00 [196]4 years ago
4 0

Answer:

<u><em>Code:</em></u>

    #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;

    }

<u><em></em></u>

You might be interested in
A palindrome is a string whose reversal is identical to the string. how many bit string of length 20 are palindromes and do not
zepelin [54]

There are 2n possible bit strings for any n.

<h3>What makes something a palindrome?</h3>
  • The way they read both forward and backward is the same.
  • A palindrome is a word, phrase, or sentence that reads the same both forward and backward.
  • Palindrome is a combination of the Greek words for "again" (Palin) and "to run" (drom).
  • A palindromic number, sometimes referred to as a numeral palindrome or a numeric palindrome, is one whose value does not change when its digits are flipped, for example, 16461.
  • The single palindrome date in the M/DD/YYYY date format in 2022 is February 20, 2022. (2-20-2022)

To learn more about palindrome, refer to:

brainly.com/question/28279486

#SPJ4

7 0
2 years ago
What are the three main components of an IF function?
Lesechka [4]
Test somthing such as the value in a cell
4 0
3 years ago
Suppose you are given a data set consisting of nominal attributes, such as color, which takes values such as red, blue, green et
Ratling [72]

Answer:

No, you can´t.

Explanation:

An SVM uses a binary system. In this case the data consists of nominal attributes which can´t be used directly into the SVM.

However, in order to transform this normal attribute into the binary system is necessary to order the information in two sequences, in which the colors are based into two information facts that will give the system the representation that needs.

3 0
4 years ago
Select L for the items that refer to General Lee, H for General Halleck, and M for General McClellan.
Naily [24]

The letters that represent the names of the individuals associated with the various actions are; M, L, H, M, L

<h3>What were the Activities of Railroad Presidents?</h3>

1) The person that removed from command twice for being overly cautious was General McClellan.

2) The person that felt a victory in the North would turn public opinion against the war was General Lee.

3) The person that retreated to Washington, leaving the Confederates free to attack the North was General Halleck

4) The person that stopped the Confederate Army at Antietam was General McClellan.

5) The person that was offered command of Union Army after Fort Sumter was General Lee.

Read more about history of railroad Presidents at; brainly.com/question/25997960

7 0
3 years ago
What is the average time a caller waits for an operator to answer?
kaheart [24]
3-4 minutes is about right is say
7 0
4 years ago
Other questions:
  • Leah wants to create a PowerPoint presentation for a history report about the progressive era in the 1900s. To
    6·2 answers
  • What's the purpose of maintaining a network of digital forensics specialists?
    14·1 answer
  • Little circles located in front of various website screen options, which can be selected with the mouse, are called ________.
    6·1 answer
  • What is wrong with the following code? How should it be fixed?
    12·1 answer
  • Which of the following would allow for the QUICKEST restoration of a server into a warm recovery site in a case in which server
    10·1 answer
  • As marketing manager, you need to have ( blank) skills and (blank) skills.
    11·1 answer
  • Given the code as follows: main() { int i = 3, n; float x; x = i; n = 8 % x; } What problem will occur? Group of answer choices
    12·1 answer
  • Networks, partnerships, and joint ventures are an example of
    7·1 answer
  • 1. You are designing a program that will keep track of the boxes in a doctor’s office. Each box will have three attributes: date
    10·1 answer
  • List at least three kinds of harm a company could experience from electronic espionage or unauthorized viewing of confidential c
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!