Sunday, July 12, 2009

C++ Sorting Numbers using procedures?

Write a procedure void sortTwo (int%26amp; n, int%26amp; m) that swaps the values of n and m if n is greater thanm and otherwise leaves n and m unchanged. For example,





int a = 2;


int b = 3;


int c = 4;


int d = 1;





sort(a, b); /* a is still 2, b is still 3 */


sort(c, d); /* c is now 1, d is now 4 */





Also 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 */

C++ Sorting Numbers using procedures?
well first the methods need to take the int as references. so it'll look something like this.





sort(int %26amp;a, int %26amp;b)


{


if(a %26gt; b)


{


int temp = a;


a = b;


b = temp;


}


}








sort(int %26amp;a, int %26amp;b, int %26amp;c)


{


sort(a,b);


sort(c,b);


sort(a,b);


}


No comments:

Post a Comment