Write a vector calculator program in C++ that works as follows: 1. Request one b
ID: 3542605 • Letter: W
Question
Write a vector calculator program in C++ that works as follows:
1. Request one bi-
Write a vector calculator program in C++ that works as follows: Request one bi-dimensional vector from the user: (x1, y1) Request an operation type out of 4 possibilities: addition (user enters '+'), subtraction ('-'), inner or dot product (7), and magnitude ('m' or 'M'). If the user entered an unknown operation, display an error message and exit the program. If the user entered any of the first 3 operations, request a second vector: (x2, y2) Execute the operation: For addition: (x1 + x2, y1 + y2) For subtraction: (x2-x1, y2-y1) For the inner product: x1 * x2 + y1 * y2 For magnitude: the square root of the sum of squares Show the result with exactly 1 number after the decimal point. Repeat the process (from point 1) until the user decides to stop. For example (user input is highlighted): Enter a vector: 10 10 Enter operation: a Enter a second vector: 20 20 Result: (10.0,10.0) + (20.0,20.0) = (30.0,30.0) Do you wish to continue: y Enter a vector: 10 10 Enter operation: m Result: |(10.0,10.0)| = 14.1 Do you wish to continue: n Goodbye...Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
int main(){
float x1, y1, x2, y2, x;
char o, choice;
cout << "Enter the x & y components of the first vector: ";
cin >> x1 >> y1;
cout << "Enter the operation(+ for addition, - for subtraction, . for dot product, m or M for magnitude): ";
cin >> o;
if(o != '+' && o != '-' && o != '.' && o != 'm' && o != 'M'){
cout << "Error!!";
return 0;
}
if(o != 'm' && o != 'M'){
cout << "Enter the x & y components of the second vector: ";
cin >> x2 >> y2;
}
switch(o){
case '+':
cout << "(" << x1 << ", " << y1 << ") + (" << x2 << ", " << y2 << ") = (" << x1 + x2 << ", " << y1 + y2 << ")" << endl;
break;
case '-':
cout << "(" << x1 << ", " << y1 << ") - (" << x2 << ", " << y2 << ") = (" << x1 - x2 << ", " << y1 - y2 << ")" << endl;
break;
case '.':
cout << "(" << x1 << ", " << y1 << ") . (" << x2 << ", " << y2 << ") = (" << x1 * x2 + y1 * y2 << ")" << endl;
break;
case 'm':
x = sqrt((x1 * x1) + (y1 * y1));
cout << "mod(" << x1 << ", " << y1 << ") = " << x << endl;
break;
case 'M':
x = sqrt((x1 * x1) + (y1 * y1));
cout << "mod(" << x1 << ", " << y1 << ") = " << x << endl;
break;
}
cout << "Do you wish to continue? y/n: ";
cin >> choice;
if(choice == 'y') main();
if(choice == 'n') cout << "Goodbye" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.