Answer to Question #99882 in C++ for unique

Question #99882
you need to generate a suite of simple matrix and vector operations including:

vector + vector

vector * vector (dot product)

vector x vector (cross product)

scalar * vector

matrix * vec (matvec)

matrix * matrix (matmat)



choose the operation from a menu

ask the user for a size n (for vectors ) and n,m for matrices

generate as test small doubles -10 to 10 for the originals vecs/matrices

then compute the solution, display
1
Expert's answer
2019-12-05T10:32:58-0500

#include <iostream>

#include <vector>

#include <cstdlib>


using namespace std;


int main()

{

int Operation = 0;

int vecN, matN, matM;

double scalar = 5;

vector<double> vec1, vec2;

vector<double> vresult;

cout << "choose the operation from a menu \n";

cout << "1)vector + vector \n";

cout << "2)vector * vector(dot product) \n";

cout << "3)vector x vector(cross product) \n";

cout << "4)scalar * vector \n";

cout << "5)matrix * vec(matvec) \n";

cout << "6)matrix * matrix(matmat) \n";

cin >> Operation;

cout << "print in size n (for vectors ) and n,m for matrices \n";

cin >> vecN >> matN >> matM;

vec1.resize(vecN);

vec2.resize(vecN);

vresult.resize(vecN);

for (int i = 0; i < vecN; i++)

{

vec1.at(i) = (double)(rand() % 21) - 10;

vec2.at(i) = (double)(rand() % 21) - 10;

}

double **mat1 = new double* [matN]; 

double **mat2 = new double* [matN];

for (int i = 0; i < matN; i++)

{

mat1[i] = new double[matM];

mat2[i] = new double[matM];

for (int j = 0; j < matM; j++)

{

mat1[i][j] = rand() % 21 - 10;

mat2[i][j] = rand() % 21 - 10;

}

}

double** matresult = new double* [matN];

for (int i = 0; i < matN; i++)

{

matresult[i] = new double[matM];

}

double res = 0;

switch (Operation)

{

case(1):

for (int i = 0; i < vecN; i++)

{

vresult.at(i) = vec1.at(i) + vec2.at(i);

cout << vresult[i] << " ";

}

break;

case(2):


for (int i = 0; i < vecN; i++)

res += vec1.at(i) * vec2.at(i);

cout << res;

break;

case(3):

vresult[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];

vresult[1] = -1*(vec1[0] * vec2[2] - vec1[2] * vec2[0]);

vresult[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];

cout << vresult[0] << " " << vresult[1] << " " << vresult[2] << " ";

break;

case(4):

for (int i = 0; i < vecN; i++)

{

vresult.at(i) = scalar * vec1.at(i);

cout << vresult[i] << " ";

}

break;

case(5):

for (int i = 0; i < matN; i++)

{

vresult.at(i) = 0;

for (int j = 0; j < matN; j++)

{

vresult.at(i)+= vec1.at(j) * mat1[i][j];

cout << vresult[i] << " ";

}

}

break;

case(6):

for (int i = 0; i < matN; i++)

{

for (int j = 0; j < matM; j++)

{

matresult[i][j] = 0;

for (int m = 0; m < matN; m++)

{

matresult[i][j]+= mat1[i][m] * mat2[m][j];

}

cout << matresult[i][j] << " ";

}

cout << "\n";

}

break;

}

return 0;

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS