Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write code by using C++ File: matrix_vector.cpp In this assignment you will create two structs called Vec3 and Mat3 which represent 3D vectors and

Please write code by using C++

File: matrix_vector.cpp

In this assignment you will create two structs called Vec3 and Mat3 which represent 3D vectors and matrices, and write functions that act on these structs. This problem is time consuming thus plan properly, structure your code with functions you reuse systematically. Design your solution before you start implementing it. Below is the list of functions you will need to write. Please do not change the function signatures (that is, do not change the functions parameter and return types).

Vec3 read_vec(): read three doubles from the standard input, use them to construct a Vec3 and return it. You can assume the three doubles will be given in the standard input as three numbers separated by spaces.

Mat3 read_mat(): read nine doubles from the standard input, use them to construct a Mat3 and return it. We use the row-major ordering for the nine numbers. That is, the first three numbers make up the first row of the matrix, the next three make up the second row, and so on.

void print(Vec3 v): print the given vector to the standard output. The format is (x, y, z).

void print(Mat3 m): print the given matrix to the standard output. The format is [ a, b, c, d, e, f, g, h, i ]. The elements are in row-major ordering (see above for what this means).

Vec3 add(Vec3 u, Vec3 v): add two vectors and return the result.

double dot(Vec3 u, Vec3 v): return the dot product of two vectors.

double length(Vec3 u): return the length (or magnitude) of the input vector.

Mat3 transpose(Mat3 m): return the transpose of the given matrix

Vec3 row(Mat3 m, int i): return the i-th row of the given matrix (indexing starts from 0)

Vec3 col(Mat3 m, int i): return the i-th column of the given matrix (indexing starts from 0)

Vec3 multiply(Mat3 m, Vec3 u): multiply a matrix with a vector and return the resulting vector.

Mat3 multiply(Mat3 m1, Mat3 m2): multiply two matrices (in the order given) and return the resulting matrix.

Each case in the input will start with an operation, which is a string telling you what to compute for the current case. The operation can be one of { add, dot, length, transpose, row, col, multiply }. The operands to the operation come next. Some operations require two operands (add, dot, multiply), others work with one operand. A matrix operand will start with an M followed by nine numbers. A vector operand will start with a V followed by three numbers. You can safely assume all the inputs will be valid (you do not have to handle invalid inputs). For each case, print the result to applying the given operation on the two given operands, using the print functions that you have written.

image text in transcribed

image text in transcribed

image text in transcribed

4. Simple Matrix and Vector Structs: 40 Points File: matrix vector.cpp In this assignment you wil create two structs called Vec3 and Mat3 which represent 3D vectors and matrices, and write functions that act on these structs. This problem is time consuming thus plan properly, structure your Code With TuncHons you reuse systernatically. Desig your solution n y our solution before you start implementig it Below is the list of functions you will need to write. Please do not change the function signatures (that is, do not change the functions' parameter and return types) 4. Simple Matrix and Vector Structs: 40 Points File: matrix vector.cpp In this assignment you wil create two structs called Vec3 and Mat3 which represent 3D vectors and matrices, and write functions that act on these structs. This problem is time consuming thus plan properly, structure your Code With TuncHons you reuse systernatically. Desig your solution n y our solution before you start implementig it Below is the list of functions you will need to write. Please do not change the function signatures (that is, do not change the functions' parameter and return types)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

A 2060 n. 2220 e. 2060 D 3.1100

Answered: 1 week ago

Question

8. Do the organizations fringe benefits reflect diversity?

Answered: 1 week ago