Question

In: Computer Science

Please give C++ code ASAP Matrix Multiplication Due Friday 30th October 2020 by 23:55. (2 marks)...

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.

Solutions

Expert Solution

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!
===========================================================================

Related Solutions

Please give Java Code ASAP Matrix Multiplication Due Friday 30th October 2020 by 23:55. (2 marks)...
Please give Java 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...
Due Friday Oct. 30th, 11:59pm (10 marks total): 1. Explain the purpose of a sequence diagram....
Due Friday Oct. 30th, 11:59pm (10 marks total): 1. Explain the purpose of a sequence diagram. . 2. Draw a sequence diagram for the online ticketing system in Assignment #3. Identify the objects, lifelines, messages, and focuses in your diagram. .
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without...
[12:18, 10/2/2020] Mohan Reddy: You are to implement a program for matrix multiplication in C++ without thread AND with thread. [12:18, 10/2/2020] Mohan Reddy: ou are to implement (M by N matrix) times (N by 1 vector) operation and see how multiple threads can speed up the computation. The resulting vector will be (M by 1 vector). See the following steps/requirements. 1. Accept M and N as keyboard input. 2. Generate a random (M by N matrix) and a random...
Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** **Please don't copy /...
Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** **Please don't copy / paste code** In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write an algorithm to calculate the recursive Matrix multiplication (or write with pseudo code) 3. Find the time complexity of your pseudo code and analyze the differences
Can someone please solve parts c, d, e, and h ASAP? This is due tomorrow 2....
Can someone please solve parts c, d, e, and h ASAP? This is due tomorrow 2. (30 pts) Consider a production function given by: Q = 12(KL)2 – L 4 . a. (5 pts) Does this production function exhibit increasing, constant, or decreasing returns to scale? b. (5 pts) Derive the equation for the average product of labor and the marginal product of labor? c. (4 pts) Let K = 2. Find the level of L at which the marginal...
What is the C programming code to find the inverse of a 2*2 matrix?
What is the C programming code to find the inverse of a 2*2 matrix?
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me know?
Due Date: 30 October 2020 Question 1 (100 marks) Note: you must use your own words...
Due Date: 30 October 2020 Question 1 Note: you must use your own words in answering the questions; those parts copied from any source without your inputs/explanation/examples or elaborations will not be given any marks. I . The Conceptual Framework for Financial Reporting sets and discusses the objective and fundamentals that serve as the basis for developing financial accounting and reporting standards in different countries. The fundamentals are the underlying concepts of financial accounting that guide the selection of transactions,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT