/*************************************************************************** Program: A2Q1.cpp Purpose: This program implements mathematical operations on two 3D vectors. The user is asked to input 3-dimension vector v1 and v2 and real a real number a. A loop is entered to ask the user which operation he/she would like to perform on the vectors and the result is output to the screen. ****************************************************************************/ #include #include #include using namespace std; // Function Prototypes void getVector(vector &v); void showVector(const vector &v); int main(){ vector v1(3); // create vector v1 of size 3 vector v2(3); // create vector v2 of size 3 vector result(3); // create vector for results double a, ans = 0.0; int choice; // get user input for vector v1 cout << "\nPlease enter 3-dimensional vector v1:\n"; getVector(v1); // get user input for vector v2 cout << "\nPlease enter 3-dimensional vector v2:\n"; getVector(v2); // get user input for number a cout << "\nPlease enter real number a:\n"; cin >> a; // loop to perform operations on vectors do{ cout << "\nWhat operation would you like to do on the vectors?\n" << "1. Addition (v1+v2).\n" << "2. Subtraction (v1-v2).\n" << "3. Multiply v1 by a constant (a*v1 = v1*a).\n" << "4. Dot Product (v1*v2).\n" << "5. Magnitude (|v1| = sqrt(x1*x1+y1*y1+z1*z1)).\n" << "6. Exit program.\n\n"; cin >> choice; // get user option switch(choice){ case 1: // addition cout << "\nThe sum of v1+v2 is: "; for (unsigned int i = 0; i < v1.size(); i++) result[i] = v1[i] + v2[i]; showVector(result); cout << endl << endl; break; case 2: // subtraction cout << "\nThe difference of v1-v2 is: "; for (unsigned int i = 0; i < v1.size(); i++) result[i] = v1[i] - v2[i]; showVector(result); cout << endl << endl; break; case 3: // constant * vector cout << "\nThe result of a*v1 = v1*a is: "; for (unsigned int i = 0; i < v1.size(); i++) result[i] = a*v1[i]; showVector(result); cout << endl << endl; break; case 4: // dot product for (unsigned int i = 0; i < v1.size(); i++) ans = ans + v1[i]*v2[i]; cout << "\nThe dot product is " << ans << endl << endl; break; case 5: // magnitude for (unsigned int i = 0; i < v1.size(); i++) ans = ans + v1[i]*v1[i]; ans = sqrt(ans); cout << "\nThe magnitude is " << ans << endl << endl; break; default: // exit switch if (choice != 6) cout << "That is not a valid choice. Please choose again.\n\n"; } ans = 0.0; // reset variable }while(choice!=6); // exit program } /**************************************************************************** Function: getVector Purpose: Prompt user to input the vector Parameters: vector of size double Return Value: NONE ****************************************************************************/ void getVector(vector &v){ for (unsigned int i=0; i> v[i]; } /**************************************************************************** Function: showVector Purpose: To display the vector Parameters: const vector of size double Return Value: NONE ****************************************************************************/ void showVector(const vector &v){ for (unsigned int i = 0; i < v.size(); i++) cout << v[i] << " "; }