Monday, July 27, 2009

Beginner C++ question regarding sorting numbers.?

Write a procedure sortThree(int%26amp; x, int%26amp; y, int%26amp; z) that swaps its three inputs to arrange them in sorted order that uses sortTwo. For example,





int a = 3;


int b = 4;


int c = 1;


sortThree(a, b, c); /* a is now 1, b is now 3, c is now 4 */

Beginner C++ question regarding sorting numbers.?
I am assuming that you need a sortTwo, then build from that a sortThree.





void sortThree(int %26amp;x, int %26amp;y, int %26amp;z)


{


sortTwo(x, y);


sortTwo(y, z); //The largest value is in place


sortTwo(x, y); //The other two values are in place


}





void sortTwo(int %26amp;x, int %26amp;y)


{


if(x %26gt; y) //If not already sorted, swap them


{


int temp = x;


x = y;


y = temp;


}


}
Reply:http://www.doyourownhomework.com/


No comments:

Post a Comment