Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. struct vect
  7. {
  8.     double x,y;
  9. };
  10.  
  11. void add(vect &a, vect &b);
  12. void subtract(vect &a, vect &b);
  13. void multiply(vect &a, vect &b);
  14. void scalarProduct(vect &a, vect &b);
  15. void magnitude(vect &a, vect &b);
  16.  
  17. int main()
  18. {
  19.     int o;
  20.     vect a, b;
  21.    
  22.     cout << "Please enter the x value for the first vector: ";
  23.     cin >> a.x;
  24.     cout << "Please enter the y value for the first vector: ";
  25.     cin >> a.y;
  26.     cout << "Please enter the x value for the second vector: ";
  27.     cin >> b.x;
  28.     cout << "Please enter the y value for the second vector: ";
  29.     cin >> b.y;
  30.    
  31.     do
  32.     {
  33.         cout << "What would you like to do:\n1. Add\n2. Subtract\n3. Multiply\n4. Find the scalar product\n5. Find the magnitude\n";
  34.         cin >> o;
  35.        
  36.         switch(o)
  37.         {
  38.             case 1: add(a,b);
  39.             break;
  40.             case 2: subtract(a,b);
  41.             break;
  42.             case 3: multiply(a,b);
  43.             break;
  44.             case 4: scalarProduct(a,b);
  45.             break;
  46.             case 5: magnitude(a,b);
  47.             break;
  48.         }
  49.     }
  50.     while( o <= 5 && o >= 1 );
  51.    
  52.     return 0;
  53. }
  54.  
  55. void add(vect &a, vect &b)
  56. {
  57.     cout << "(" << a.x + b.x << "," << a.y + b.y << ")" << endl;
  58. }
  59.  
  60. void subtract(vect &a, vect &b)
  61. {
  62.     cout << "(" << a.x - b.x << "," << a.y - b.y << ")" << endl;
  63. }
  64.  
  65. void multiply(vect &a, vect &b)
  66. {
  67.     cout << "(" << a.x * b.x << "," << a.y * b.y << ")" << endl;
  68. }
  69.  
  70. void scalarProduct(vect &a, vect &b)
  71. {
  72.     cout << (a.x * b.x) + (a.y * b.y) << endl;
  73. }
  74.  
  75. void magnitude(vect &a, vect &b)
  76. {
  77.     int i;
  78.     cout << "Which vector would you like to know the magnitude of?\n1: (" << a.x << "," << a.y << ")\n2: (" << b.x << "," << b.y << ")\n";
  79.     cin >> i;
  80.  
  81.     switch(i)
  82.     {
  83.         case 1: cout << sqrt((a.x*a.x) + (a.y*a.y)) << " or √" << (a.x*a.x) + (a.y*a.y) << endl;
  84.         break;
  85.         case 2: cout << sqrt((b.x*b.x) + (b.y*b.y)) << " or √" << (b.x*b.x) + (b.y*b.y) << endl;
  86.         break;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement