Advertisement
M_A_Tabarani

School Stuff 09

Oct 4th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. //Function with output parameters
  2. //Add two vectors that are at right angles to each other
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. const double PI=3.14159265358979;
  11.  
  12. void addVect (double&,int&,double,double); //use shorter format of prototype
  13.  
  14. int main()
  15. {
  16.     double aVect;
  17.     double bVect;
  18.     double rMag;
  19.     int rDir;
  20.  
  21.     cout<<"Enter the magnitude of the horizontal vector A : ";
  22.     cin>>aVect;
  23.  
  24.     cout<<"Enter the magnitude of the vertical vector B : ";
  25.     cin>>bVect;
  26.  
  27.     addVect(rMag,rDir,aVect,bVect);
  28.  
  29.     cout<<setiosflags(ios::fixed)<<setprecision(1);
  30.  
  31.     cout<<"A + B yields resultant vector of magnitude "<<rMag<<endl<<"and direction "<<rDir<<" degrees"<<endl;
  32.  
  33.     return 0;
  34. }
  35.  
  36. //
  37. //Add vectors a and b that are at right angles
  38. //a is horizontal, b is vertical, a>0, b>0
  39. //Express direction of resultant in whole degrees,
  40. //
  41.     void addVect(double& rMagnitude, //output-magnitude of resultant
  42.                  int& rDirection, //output - direction of resultant in whole degrees
  43.                  double a, double b) //input - vectors at right angles
  44. {
  45.     double rDirRadians; //direction of resultant in radians
  46.  
  47.     rMagnitude = sqrt(a*b+b*b);
  48.     rDirRadians = atan(b/a);
  49.     rDirection = int (180/PI*rDirRadians+0.5);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement