Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sorting 3 Values. Study the slide 28 and the code in alpha3.cpp
- // Run the program, then A) modify sort3 so it sorts the three variables
- // passed to it in main. then B) modify main to read in 4 values, and
- // use repeated calls to sort3 to arrange them in increasing order.
- //
- void swap(float& x, float& y)
- {// exchanges the values of x , y:
- float temp = x;
- x = y;
- y = temp;
- }
- // sort3() Place three parameters into numerical order
- // IN/OUT: x, y, z are any values. Upon return, they
- // will be in numeric sequence SEE TEST PLAN
- void sort3 (float& x, float& y, float& z)
- {
- if (x > y) swap(x,y);
- if (x > z) swap(x,z);
- if (y > z) swap(y,z);
- }
- // B) After A works, and you can sort 3 values properly, modify main
- // to read in 4 values and sort them by using several calls to sort3
- // SEE TEST PLAN
- int main( )
- {
- float q,r,s,t;
- cout<<"Enter 3 values: \n";
- cin>>q>>r>>s>>t;
- sort3 (q,r,s);
- sort3 (r,s,t);
- sort3 (q,r,s);
- cout<<"in order:" << q <<" "<< r <<" "<< s <<" "<< t <<" "<< endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement