In: Computer Science
Please give C++ code ASAP
Matrix Multiplication Due Friday 30th October 2020 by 23:55.
For this exercise, you are to find the optimal order for multiplying a sequence of matrices. Note: you do not actually have to perform any matrix multiplications. As usual, your program will prompt for the name of an input file and the read and process the data contained in this file.
The file contains the following data. N, the number of matrices to be multiplied together N pairs of integers which are the row and column dimensions of each matrix. E.g.
The following input
3
3 4
4 2
2 5
Defines a problem in which we are to multiply three matrices, say M[0], M[1] and M[2], where:
M[0] has 3 rows and 4 columns; M[1] has 4 rows and 2 columns; M[2] has 2 rows and 5 columns.
Output for the program is the value of best(0,N), the minimum number of multiplications required to compute the matrix R = M[0]xM[1]x…xM[N‐1].
You may leave your solution as a memoized, recursive formulation if you have problems formulating the looped iterative scheme.
As usual, do not use classes or STL. Submit ex10.ext via moodle as usual where ext is one of c, cpp, java or py.
Source Code: in C++
#include <iostream>
#include <fstream>
#include <array>
#include <sstream>
#include <string>
#include <stdio.h>
#include <algorithm>
int row=0;
int col=0;
int i,j,k;
using namespace std;
int main()
{
string line;
ifstream myfile;
int x;
int arrayA[100][100] = {{0}};
int arrayB[100][100] = {{0}};
int arrayD[100][100] = {{0}};
myfile.open("numeric.txt");
if(myfile.fail())
{
cerr<<"file does not exist";
}
cout<<"\n"<<endl;
while(myfile.good())
{
while(getline(myfile,line))
{
istringstream stream(line);
col=0;
while(stream >> x)
{
arrayA[row][col]=x;
col ++;
}
row ++;
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<arrayA[i][j]<< " " ;
}
cout<<endl;
}
cout<<"\n"<<endl;
}
cout<<"first Matrix"<<endl;
for(i=1;i<=3;i++)
{
for(j=0;j<3;j++)
{
cout<<arrayA[i][j]<<" ";
}
cout<<endl;
}
cout<<"Second Matrix"<<endl;
for(i=2;i<5;i++)
{
for(j=2;j<5;j++)
{
arrayB[i][j] = arrayA[i][j];
cout<<arrayB[i][j]<<" ";
}
cout<<endl;
}
cout<<"Multiplication Result"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
int sum = 0;
for(k=0;k<3;k++)
{
sum=sum+arrayA[i][k]*arrayB[k][j];
}
arrayD[i][j]=sum;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<arrayD[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================