Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program name: Matrix Multiply (please use ony c++ programming methods) Given two

ID: 3764346 • Letter: P

Question

Program name: Matrix Multiply (please use ony c++ programming methods)

Given two data files containing different matrices, write a program to read the matrix contents from the data files into arrays A and B in the program and multiply them together. One input file will go into array A, and the other input file will go into array B. All program working arrays, including arrays A and B, will be declared in main as size 10x10. Matrices in the input data files can be variable size up to the 10x10 maximum, and have different names.

This program must not limit the number or names of files available to be multiplied. It must work with any data files given by the user, and be capable of multiplying arrays having varying numbers of rows and columns up to the maximum size of 10x10.

Arrays in data files will be read into program arrays A and B in main, then the input data files will be closed. Multiplication will be carried out based on array data in the program arrays. If the input file dimensions are less than 10x10, the input array will occupy the left and upper most corner of the program array, i.e. the first element in the input array will occupy the first element in the program array. The number of rows and columns of the resulting sub array must follow the rules of matrix multiplication.

Arrays given in the data files to be multiplied are not extracted from other data arrays, but are intended to be used directly. These will be read into the program arrays in main as sub arrays. Sample arrays are provided in the topic module.

Assignment Procedure:

Find the product AB

Find the product BA

If the product is incompatible, provide an output statement stating as such.

Write the product array to a data file.

Report your findings. For compatible arrays, is AB=BA? Give the reasons for your conclusion. Include the conclusion in the file as a multiline comment.

Program structural requirements are as follows:

Your program must check to see if the multiplication is possible before proceeding with the operation. That is, see if the numbers rows and columns are compatible for the operation.

There must be one function to perform the actual array multiplication, but do nothing more. Necessary information will be passed to the function as arguments.

Each array multiplication operation must call the function. No multiplication will be done in main.

Input matrix data files may be variable from 1 to 10 rows and columns. As an example, the data file may hold a 5x5 matrix, but it will be read into a 10x10 matrix in main.

Matrices A and B can hold data files to be multiplied with different dimensions, e.g. different number of rows and columns.

The names of the data files to be read into program arrays will be entered from the keyboard, e.g. dat, matrix1.dat, etc.

The number of rows and columns of the array in the input data file will be input from the keyboard.

Capability of being able to multiply different sizes of matrices will be checked when graded. Use the sample data files to verify this capability before submitting the program.

All input and output data files, plus the .cpp file, must be attached to the assignment submission.

All matrices must be written to the screen.

The final program must be checked for compliance with these instructions before submitting.

(I am wondering if anyone would be able to help me with this program and I will appraciate it if explained. Thanks)

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;


int** multiple(int** first,int m,int n,int** second,int p,int q,string s){
   int** answer;
   answer = new int*[m];
   for (int i = 0; i < m; i++){
       answer = new int[q];
       for (int j = 0; j < q; j++)
           answer[i][j] = 0;
   }
   if (n != p){
       cout << "MULTIPLICATION NOT POSSIBLE" << endl;
       return answer;
   }
   for (int c = 0; c < m; c++) {
      for (int d = 0; d < q; d++) {
        for (int k = 0; k < p; k++) {
          sum = sum + first[c][k]*second[k][d];
        }
        answer[c][d] = sum;
        sum = 0;
      }
    }
    ofstream outfile;
   outfile.open(s.c_str());
   outfile << m << " " << q << endl;
   for (int i = 0; i < m; i++){
       for (int j = 0; j < q; j++)
           outfile << answer[i][j] << " ";
       outfile << endl;
   }
}

int main(){
   ifstream infile_1,infile_2;
   int m,n;
   infile_1.open("mat1.txt");
   intfile_1 >> m >> n;
   int** mat_1 = new int*[m];
   for (int i = 0; i < m; i++)
       mat_1[i] = new int[n];
   for (int i = 0; i < m; i++){
       for(int j = 0; j < n; j++){
           infile_1 >> mat_1[i][j];
       }
   }
   int x,y;
   infile_2.open("mat2.txt");
   intfile_2 >> x >> y;
   int** mat_2 = new int*[x];
   for (int i = 0; i < x; i++)
       mat_2[i] = new int[y];
   for (int i = 0; i < x; i++){
       for(int j = 0; j < y; j++){
           infile_2 >> mat_2[i][j];
       }
   }
  
   int** ans_1 = multiple(mat_1,m,n,mat_2,x,y,"mat1.txt");
  
   int** ans_2 = multiple(mat_2,x,y,mat_1,m,n,"mat2.txt");
   outfile.open("res2.txt");
  
   return 0;
}