Question

In: Computer Science

<Using C++ programming.> <Prof said that you use the example given below.> Write a program to...

<Using C++ programming.>

<Prof said that you use the example given below.>


Write a program to find (A*B)*10 + (2*C+D) using class Matrix.

For member functions, use add() and mul().


Add()

-a,b,c are all m*n matrices.

-Add(a,b,c,m,n): cij = aij + bij

-Add(a,m,n,num): cij = aij + num


Mul()

-a is an m*n matrix, b is an n*p matrix, and c is an m*p matrix.

-mul(a,b,c,m,n): (Matrix Multiplication)

-mul(a,m,n,num): aij = aij * num

Solutions

Expert Solution

code in c++

(code to copy)

#include<bits/stdc++.h>
using namespace std;
//matrix class
class Matrix{
        public:
        int rowLength;
        int colLength;
        vector<vector<int>> arr2d;
        Matrix(int rowLength, int colLength):rowLength(rowLength), colLength(colLength){
                vector<vector<int>> arr(rowLength, vector<int>(colLength));
                arr2d=arr;
        }
        //fills the matrix with random values between start and end
        void fillRand(int start, int end){
                for(int i=0;i<arr2d.size();i++){
                        for(int j=0;j<arr2d[i].size();j++){
                                arr2d[i][j]=rand()%(end-start+1) + start;
                        }
                }
        }

        //this is to change value of matrix
        void put(int i, int j, int value){
                arr2d[i][j]=value;
        }

        //this is to retrieve value from matrix
        int get(int i, int j){
                return arr2d[i][j];
        }


        //this adds two matrix
        static Matrix add(Matrix A, Matrix B){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for(int i=0;i<A.rowLength;i++){
                        for(int j=0;j<A.colLength;j++){
                                C.put(i, j, A.get(i, j)+B.get(i, j));
                        }
                }
                return C;
        }

        //this adds constant to a  matrix
        static Matrix add(Matrix A, int num){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for(int i=0;i<A.rowLength;i++){
                        for(int j=0;j<A.colLength;j++){
                                C.put(i, j, A.get(i, j)+num);
                        }
                }
                return C;
        }

        //this multiplies two matrix
        static Matrix mul(Matrix A, Matrix B){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for (int i = 0; i < A.rowLength; i++)
                {
                        for (int j = 0; j < B.colLength; j++)
                        {
                                C.put(i,j,0);
                                for (int k = 0; k < A.colLength; k++)
                                {
                                        C.put(i,j,C.get(i,j)+(A.get(i,k)*B.get(k,j)));
                                }
                        }
                }
                return C;
        }
        
        //this multiplies constant to a  matrix
        static Matrix mul(Matrix A, int num){
                //create matrix c
                Matrix C(A.rowLength, A.colLength);
                for(int i=0;i<A.rowLength;i++){
                        for(int j=0;j<A.colLength;j++){
                                C.put(i, j, A.get(i, j)*num);
                        }
                }
                return C;
        }

        //prints the matrix
        void print(string identifier){
                cout<<identifier+": "<<endl;
                for(int i=0;i<rowLength;i++){
                        for(int j=0;j<colLength;j++){
                                cout<<arr2d[i][j]<<" ";
                        }
                        cout<<endl;
                }
                cout<<endl;
        }
};

// driver function 
int main()
{
        /*
        We need to perform (A*B)*10 + (2*C+D)
        So we need to follow these constraints
        column length of A should be equal to rowlength of B
        row and column length of C should be equal to row and column of D 
        row of A and column of B should be equal to row of C/D and column of C/D
        */

        // lets take an example
        Matrix A(3, 4); //A is a 3*4 matrix
        Matrix B(4,4); //B is a 4 *4 matrix
        Matrix C(3,4); //C is a 3*4 matrix
        Matrix D(3,4); //D is a 3*4 matrix

        //populate matrix with random values between 10 and 20
        A.fillRand(10, 20);
        B.fillRand(10, 20);
        C.fillRand(10, 20);
        D.fillRand(10, 20);

        // this represents result = (A*B)*10 + (2*C+D)
        Matrix result = Matrix::add(Matrix::mul(Matrix::mul(A, B), 10), Matrix::add(Matrix::mul(C, 2), D));

        //print input and output
        A.print("A");
        B.print("B");
        C.print("C");
        D.print("D");
        result.print("(A*B)*10 + (2*C+D)");
}

code screenshot

Sample output screenshot


Related Solutions

C Programming Question: Q) Write a C - program to check whether a given graph is...
C Programming Question: Q) Write a C - program to check whether a given graph is Bipartite using Breadth-first search (adjacency list) Do take your time but please do post full code and also please do it in C.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
Please write a program in c++ The method sellProduct of the Juice Machine programming example gives...
Please write a program in c++ The method sellProduct of the Juice Machine programming example gives the user only two chances to enter enough money to buy the product. Rewrite the definition of the method sellProduct so that it keeps prompting the user to enter more money as long as the user has not entered enough money to buy the product. Also, write a program to test your method. Your program should produce the following example output: *** Welcome to...
Please if you are able to answer the question below: Write C++ program using native C++...
Please if you are able to answer the question below: Write C++ program using native C++ (you can use STL)  that produces Huffman code for a string of text entered by the user.  Must accept all ASCII characters.  Pleas explain how you got frequencies of characters, how you sorted them, how you got codes.
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT