/** * Do a lot of things using defines * @authod gtowell * Created: March 2021 * **/ #include #include #define SIZE 20 // get a random number #define RAND_RANGE(min, max) min + rand() / (RAND_MAX / (max - min + 1) + 1) // swap two ints This they must be ints kind of kills some of the // define benefit. #define SWAP_INT(a, b) {int t=a; a=b; b=t;} // only define the max func if not defined elesewhere #ifndef max #define max(a,b) (a > b ? a : b) #endif // likewise for min #ifndef min #define min(a,b) (a > b ? (b) : (a)) #endif // Use max and min to create a define for median_of_three #define MEDIAN(a,b,c) ( (a > b) ? max(b, min(a,c)) : min(b, max(a,c)) ) #define DO_MEDIAN 1 #define DO_SORT 1 #if DOSORT /** * A fairly standard insertion sort * But only a part of the compiled code if DOSORT * **/ void iSort(int *arr, int lo, int hi) { //printf("iSort %d %d\n", lo, hi); for (int i=lo; i<(hi-1); i++) { int lst = i; for (int j=i; j arr[j]) { lst = j; } } SWAP_INT(arr[i], arr[lst]); } } #endif void main(int argc, char * argv[]) { #if DO_SORT srand(5566743); int arr[SIZE]; for (int i=0; i