Question

In: Computer Science

Write a function that will accept one integer matrix E and one column vector F by...

Write a function that will accept one integer matrix E and one column vector F by reference parameters, and one integer I as a value parameter. The function will return an integer v, which is the i-th coefficient of row vector denoted by E*F (multiplication of E and F). For example, if V = E*F, the function will return v, which is equal to V[i].

Explain the time complexity of this function inside of the function code as a comment.

in c++ program

Solutions

Expert Solution

Program :

//here this program is not with dynamically allocated memory because we can not pass by reference an array with unknown bound so we need to pass the size also

//we can make this dynamic by use of pointers

//time complexity of the function will be O(n) where n is no of cols in matrix E or no of rows in col vector F

as we just need to return the coefficient i of V where V = E*F

so don't need to find all the coefficients just need find the coefficient for ith row of E

1 2 3 1 14 = (1*1 + 2*2 + 3*3)

4 5 6 X 2 = 32 = (4*1+ 5*2+6*3)

7 8 9 3 50 = (7*1+8*2+9*3)

main.cpp

#include <iostream>
#include <vector>
using namespace std;

//Required function
//we can not pass reference array of unkown bound
int findCoefficient(int (&E)[3][3],vector<int> &F,int i)
{
    int row1 = sizeof(E)/sizeof(E[0]);  //no of rows in matrix E
    int col1 = sizeof(E[0])/sizeof(int); // no of cols in matrix E
    int row2 = F.size();  // no of rows in F
    
    if(i < 0 || i>= row1)
    {
        cout<<"\nCoefficient index is out of range!";
        return -1;
    }
    if(col1 != row2)
    {
        cout<<"\nMultipliction is not possible!";
        return -1;
    }
    int v=0;  // to store value of V[i] where V = E*F index i = 0 to row1-1
    /***
    * here loop is going from 1 to (no of cols in E or no of rows in F) 
    *therefore the time complexity = O(n)   where n is no of cols in matrix E or no of rows in vector F
    *here we don't need to find all multiplications we just need to find the multiplication for the 
    ith Coefficient of E*F so we will consider all cols of ith row in matrix E and all rows of vector F 
    ***/
    
    for(int j=0; j<col1; j++)
    {
        v = v + E[i][j]*F[j];
    }

    return v;
}

//main program for testing the function
int main()
{
    int E[][3]= {{1,2,3},{4,5,6},{7,8,9}};
    vector<int> F;
    F.push_back(1);
    F.push_back(2);
    F.push_back(3);
    
    //entring the index to find v[i]
    int index;
    cout<<"\nEnter the Coefficient index i = (0 to row-1) to find V[i] where V = E*F : ";
    cin>>index;
    
    //calling the function
    int v = findCoefficient(E,F,index);
    cout<<"\nValue of V["<<index<<"]= "<<v;
    
    return 0;
}

Output :

Program screenshot :


Related Solutions

Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
for input matrix a , write a function to return the element in row 2 column...
for input matrix a , write a function to return the element in row 2 column 3 of that matrix
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
Write a function script DirCos.m that takes a vector (any row or column array) as the...
Write a function script DirCos.m that takes a vector (any row or column array) as the argument and returns the direction cosines for that vector. This is for a MatLab script
Write a function that takes a numeric or integer vector and adds up only the numbers...
Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even. Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd. Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or...
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of...
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of Person pointers as a parameter. Return a pointer to the Person with the highest salary. The function must be signatured like: Person* getBestPaid(vector*); The class definition is: class Person { public: double salary; string name; }; You may include code in your main() to test, but the tests in this assignment will ensure your code is correct. You may assume there will ways be...
Suppose A is an mxn matrix of real numbers and x in an nx1 column vector....
Suppose A is an mxn matrix of real numbers and x in an nx1 column vector. a.) suppose Ax=0. Show that ATAx=0. b.)Suppose ATAx=0. show Ax=0. c.) by part a and b, we can conclude that Nul(A) = Nul(ATA), and thus dim(Nul A) = dim(Nul(ATA)), and thus nullity(A) = nullity(ATA). prove the columns of A are linearly independent iff ATA is invertible.
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average). Use the following main function in driver1b.cpp as a starting...
Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument.
/** * FUNCTION SIGNATURE: * PURPOSE: * PARAMETER: * RETURN VALUE: * Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the time slice represented by your input vector. Remember BUY LOW, SELL HIGH
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT