#include #include #include void swap_if(int & x, int & y) { if (x > y) { int temp = x; x = y; y = temp; } } void main(int argc, char * argv[]) /* If I run the program by typing a.out 15 argv[0] will be "a.out" argv[1] will be "15" argv[2] will be NULL argc will be 2 */ { srandomdev(); const int N = atol(argv[1]); // atol : convert ascii string of digits to int int A[1000]; for (int i = 0; i < N; i += 1) A[i] = random() % 1000; printf("before:\n "); for (int i = 0; i < N; i += 1) printf(" %3d", A[i]); printf("\n"); /* This is the original triangle of bricks swap_if(a, b); swap_if(b, c); swap_if(c, d); swap_if(d, e); swap_if(e, f); swap_if(a, b); swap_if(b, c); swap_if(c, d); swap_if(d, e); swap_if(a, b); swap_if(b, c); swap_if(c, d); swap_if(a, b); swap_if(b, c); swap_if(a, b); swap_if(b, c); swap_if(a, b); Now we'll convert it into a little loop */ for (int stop = N-1; stop > 0; stop -= 1) for (int first = 0; first < stop ; first += 1) swap_if(A[first], A[first+1]); printf("after:\n "); for (int i = 0; i < N; i += 1) printf(" %3d", A[i]); printf("\n"); }