# include # include int iran ( int a, int b, int *seed ); // // In this example, we don't know the values of A, B, and C. // We want to put them into sorted order. // int main ( ) { int a; int b; int c; int seed; int t; // // To make this fair, we set A, B, and C randomly. // To keep things interesting, we let the "seed" depend on the current time. // seed = time ( 0 ); a = iran ( 0, 100, &seed ); b = iran ( 0, 100, &seed ); c = iran ( 0, 100, &seed ); printf ( "Starting out, (A,B,C) = (%i,%i,%i)\n", a, b, c ); // // A, all by itself, is in order. // Is (A,B) in order? // If not, swap A and B. // if ( b < a ) { t = a; a = b; b = t; printf ( "Swapped A and B: (A,B,C) = (%i,%i,%i)\n", a, b, c ); } // // (A,B) is in order. // Is (A,B,C) in order? C might slide one place left, or even two. // if ( c < b ) { t = b; b = c; c = t; printf ( "Swapped B and C, (A,B,C) = (%i,%i,%i)\n", a, b, c ); if ( b < a ) { t = a; a = b; b = t; printf ( "Swapped A and B: (A,B,C) = (%i,%i,%i)\n", a, b, c ); } } return 0; }