Question

In: Computer Science

In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at()...

In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at() to access its elements.

Write a program that multiplies a 2D matrix with a vector. If you need to see the math, follow this link: https://mathinsight.org/matrix_vector_multiplication (Links to an external site.)

For simplicity, our matrix will be of size 3 x 3. Initialize the matrix as shown in to become

[1.0, 2.0, 3.0]

[4.0 ,5.0 ,6.0]

[7.0, 8.0, 9.0]

Read the three values of the vector to be multiplied on the right of the matrix from the user input. Then, multiply it and print the resulting vector to cout. The interaction should look like this:

Please enter the three vector coefficients

1 1 1

The result vector is [6, 15, 24]

Write a function that takes the matrix and the right-side vector as parameters and that returns the vector resulting from the multiplication.

The program must not use arrays, only vectors.

this program should be programmed using c++ only

Solutions

Expert Solution

C++ code:

//=========================================================

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


// user can update if he want to multiple other sized matrix to vector
// where matrix size is SIZE_MxSIZE_N and vector size is SIZE_Nx1
// result vector size is SIZE_Mx1
#define SIZE_M 3
#define SIZE_N 3


vector<float> multiply_matrix_vector(vector<vector<float>> mat, vector<float> vect)
{
   // creating a output vector that will store multiplication output
   vector<float> result(SIZE_M);
   for (int i = 0; i < SIZE_M; i++)
   {
       //initializing ith position of vector
       result.at(i) = 0;
       for (int j = 0; j < SIZE_N; j++)
       {
           //calculating sum of ai1*x1 + ai2*x2 ... +aij*xj + .... + ain*xn
           result.at(i) = result.at(i) + mat.at(i).at(j)*vect.at(j);
       }
   }
   // return result vector
   return result;
}
int main()
{
   // creating vector of vector with data type double
   // because in question numbers are decimal number (float)
   vector<vector<float>> mat{ { 1.0, 2.0, 3.0 },
                               { 4.0, 5.0, 6.0 },
                               { 7.0, 8.0, 9.0 } };

   // creating a right vector with size 3;
   vector<float> vect(SIZE_N);
   cout << "Please enter the three vector coefficients" <<endl;

   // read input vector coefficients
   for (int i = 0; i < SIZE_N; i++)
       cin >> vect.at(i);

   // multiply_matrix_vector function is called for mutilplication of matrix and right vector
   // result is stored in result vector
   vector<float> result = multiply_matrix_vector(mat, vect);

   //print result vector
   cout << "The result vector is [";
   for (int i = 0; i < SIZE_M-1; i++)
       cout<< result.at(i)<<", ";
   cout << result.at(SIZE_M - 1) << ']' << endl;

   return 0;
}

//=========================================================

output 1:

output 2:


Related Solutions

To describe and analyze motion in 2D (or higher), the use of vectors is the common...
To describe and analyze motion in 2D (or higher), the use of vectors is the common method used. From your work with vectors, past and present, do you find the vector method a good (even best) way to analyze quantities in 2D? Are there other methods that could be used that will give the same results? From your own experience, what are the pros and cons in using the vector method? Please no handwritten or typed responses - only typed...
In this problem, you will illustrate a geometric fact using vectors associated with 2D regions in...
In this problem, you will illustrate a geometric fact using vectors associated with 2D regions in 3D space. We start with the four points given as: A : (0, 0, 0) B : (1, 0, 0) C : (0, 1, 0) D : (0, 0, 1). These four points define a 3D object which is a tetrahedron (not a regular one, however). 1. Find an equation for the plane through points A, B, C; call this plane P1. Using this,...
Show how Rotation Matrix R transforms vectors to vectors
Show how Rotation Matrix R transforms vectors to vectors
Write each vector as a linear combination of the vectors in S. (Use s1 and s2,...
Write each vector as a linear combination of the vectors in S. (Use s1 and s2, respectively, for the vectors in the set. If not possible, enter IMPOSSIBLE.) S = {(1, 2, −2), (2, −1, 1)} (a)    z = (−8, −1, 1) z = (b)    v = (−2, −5, 5) v = (c)    w = (1, −23, 23) w = (d)    u = (2, −6, −6) u =
Write each vector as a linear combination of the vectors in S. (Use s1 and s2,...
Write each vector as a linear combination of the vectors in S. (Use s1 and s2, respectively, for the vectors in the set. If not possible, enter IMPOSSIBLE.) S = {(1, 2, −2), (2, −1, 1)} (a)    z = (−13, −1, 1) z =      (b)    v = (−1, −5, 5) v =      (c)    w = (−2, −14, 14) w =      (d)    u = (1, −4, −4) u =     
In the assignment you will use the vector class tomanage a dynamic array.After completing this assignmentyou...
In the assignment you will use the vector class tomanage a dynamic array.After completing this assignmentyou will be able to • use thevector classto implement programs; • using iteratorsto morethrough a vector; • use pointers to a vector; • use member functions of thevector class. Assignment Description: Call your driver "vector_test_driver.cpp", your classdeclaration file “vlist.h” and your class implementation file "vlist.cpp". Define the following behavior for vlist 1. Implement a default constructor. Include the followingmessage, "Default Constructor Invoked" every time...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to...
Matrix Multiplication with Threads - C/C++ 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 (i.e. both dimensions should be equal). The function should allocate storage from...
Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Given a matrix ? (i.e., a 2D table) of positive integers of size ? × ?....
Given a matrix ? (i.e., a 2D table) of positive integers of size ? × ?. A robot starts at the top- left position ?[?, ?] and must reach the bottom-right position ?[?, ?]. From a position ?[?, ?], the robot can only go either right to ?[?, ? + ?] or down to ?[? + ?, ?]. Let us define the cost of a path the total values of integers in it. Find the least-cost path for the robot...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT