// file: lecture_29/example.cc // // local include files // #include "example.h" // declare the main program // int main (int argc, char** argv) { // demonstate #define's // fprintf(stdout, STRING2, STRING1); fprintf(stdout, "exp1 = %d\n", EXPRESSION1); fprintf(stdout, "exp2 = %d\n", EXPRESSION2); // demonstrate macros // printf ("abs: %f\n", (float)ABS(atof(argv[1]))); printf ("abs: %d\n", (long)ABS(atoi(argv[1]))); printf ("max: %f\n", (float)MAX(atof(argv[1]), atof(argv[2]))); printf ("max: %d\n", (long)MAX(atoi(argv[1]), atoi(argv[2]))); printf ("biggest: %f\n", (float)BIGGEST(atof(argv[1]), atof(argv[2]), atof(argv[3]))); // declare an array of order 3 of doubles // int N = 3; double array[N]; array[0] = 2.0; array[1] = 3.0; array[2] = 1.0; fprintf(stdout, "Before:\n"); for (long i = 0; i < N; i++) { fprintf(stdout, "array[%d] = %f\n", i, array[i]); } // sort the array // qsort(array, N, sizeof(double), compare_doubles); // print out the sorted values // fprintf(stdout, "After:\n"); for (long i = 0; i < N; i++) { fprintf(stdout, "array[%d] = %f\n", i, array[i]); } // declare an array of order 3 of strings // N = 3; char array2[N]; array2[0] = (char)'a'; array2[1] = (char)'Z'; array2[2] = (char)'c'; fprintf(stdout, "Before:\n"); for (long i = 0; i < N; i++) { fprintf(stdout, "array[%d] = %c (%d)\n", i, array2[i], array2[i]); } // sort the array // qsort(array2, N, sizeof(char), compare_chars); // print out the sorted values // fprintf(stdout, "After:\n"); for (long i = 0; i < N; i++) { fprintf(stdout, "array[%d] = %c (%d)\n", i, array2[i], array2[i]); } // exit gracefully // return 0; } // a function to compare two doubles // int compare_doubles(const void *a, const void *b) { // cast the void pointers to doubles // const double *da = (const double *) a; const double *db = (const double *) b; // compare the values as doubles // if (*da < *db) {return -1;} else if (*da == *db) {return 0;} else {return 1;} // return (*da < *db) - (*da > *db); } // a function to compare two chars // int compare_chars (const void *a, const void *b) { // cast the void pointers to doubles // const char *da = (const char *) a; const char *db = (const char *) b; char tmp_a = tolower(*da); char tmp_b = tolower(*db); // compare the values as chars // if (tmp_a < tmp_b) {return -1;} else if (tmp_a == tmp_b) {return 0;} else {return 1;} // return (*da > *db) - (*da < *db); } /* //=========== #include // A normal function with an int parameter // and void return type void fun(int a) { printf("Value of a is %d\n", a); } int main() { // fun_ptr is a pointer to function fun() void (*fun_ptr)(int) = &fun; // The above line is equivalent of following two void (*fun_ptr)(int); fun_ptr = &fun; // Invoking fun() using fun_ptr (*fun_ptr)(10); return 0; } */