Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. // Sorting 3 Values.  Study the slide 28 and the code in alpha3.cpp
  2. // Run the program, then  A) modify sort3 so it sorts the three variables
  3. // passed to it in main.  then B) modify main to read in 4 values, and
  4. // use repeated calls to sort3 to arrange them in increasing order.
  5. //
  6.  
  7. void swap(float& x, float& y)
  8. {//  exchanges the values of x , y:
  9.  float temp = x;
  10.  x = y;
  11.  y = temp;
  12. }
  13.  
  14. // sort3() Place three parameters into numerical order
  15. // IN/OUT: x, y, z are any values. Upon return, they
  16. //      will be in numeric sequence   SEE TEST PLAN
  17.  
  18. void sort3 (float& x, float& y, float& z)
  19. {
  20.  
  21.    if (x > y) swap(x,y);
  22.    if (x > z) swap(x,z);
  23.    if (y > z) swap(y,z);
  24.  
  25.  
  26. }
  27.  
  28.  
  29. // B) After A works, and you can sort 3 values properly, modify main
  30. //    to read in 4 values and sort them by using several calls to sort3
  31. //    SEE TEST PLAN
  32.  
  33. int main( )
  34.  
  35.  
  36. {
  37.     float q,r,s,t;
  38.     cout<<"Enter 3 values: \n";
  39.     cin>>q>>r>>s>>t;
  40.     sort3 (q,r,s);
  41.     sort3 (r,s,t);
  42.     sort3 (q,r,s);
  43.     cout<<"in order:" << q <<" "<< r <<" "<< s <<" "<< t <<" "<< endl;
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement