Last active
August 30, 2015 14:07
-
-
Save sullyj3/61738ef8be7b4adab268 to your computer and use it in GitHub Desktop.
Something horrifying I created as an extension exercise my first semester learning C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void print_prototype(int num_elements); | |
void print_func(int num_elements); | |
void sort_func_generator(int num_elements); | |
int main(int argc, char *argv[]) { | |
int num_ints; | |
scanf("%d", &num_ints); | |
sort_func_generator(num_ints); | |
return 0; | |
} | |
void sort_func_generator(int num_elements) { | |
int i; | |
printf("#include <stdio.h>\n\n"); | |
/* function prototypes */ | |
printf("void int_swap(int *p1, int *p2);\n"); | |
for (i=2; i<=num_elements; i++) { | |
print_prototype(i); | |
} | |
printf("\n"); | |
printf("int main(int argc, char *argv[]) {\n"); | |
/* declare int variables to be sorted */ | |
printf(" int "); | |
for (i=1; i<num_elements; i++) { | |
printf("i%d, ",i); | |
} | |
printf("i%d;",i); | |
printf(" printf(\"enter %d numbers to sort:\\n> \");\n", num_elements); | |
printf(" scanf("); | |
for (i=1; i<num_elements; i++) { | |
printf("%%d "); | |
} | |
printf("%%d\", "); | |
for (i=1; i<num_elements; i++) { | |
printf("&i%d, ",i); | |
} | |
printf("&i%d);\n",i); | |
printf(" intsort%d(",num_elements); | |
for (i=1; i<num_elements; i++) { | |
printf("&i%d, ",i); | |
} | |
printf("&i%d);\n",i); | |
printf(" printf(\"\\ninput has been sorted:\\n\", "); | |
for (i=1; i<num_elements; i++) { | |
printf("i%d, ",i); | |
} | |
printf("i%d);\n",i); | |
printf(" return 0;\n"); | |
/* end main function */ | |
printf("}\n\n"); | |
/* intswap */ | |
printf("void int_swap(int *p1, int *p2) {\n"); | |
printf(" tmp = *p1;\n"); | |
printf(" *p1 = *p2;\n"); | |
printf(" *p2 = tmp;\n"); | |
printf("}\n\n"); | |
/* intsort2 */ | |
printf("void intsort2(int *p1, int *p2) {\n"); | |
printf(" if (*p2>*p1)\n"); | |
printf(" int_swap(p1, p2);\n}\n\n"); | |
/* all the other intsorts */ | |
for (i=3; i<=num_elements; i++) { | |
/* TODO */ | |
print_func(i); | |
} | |
} | |
void print_prototype(int num_elements) { | |
int i; | |
printf("void intsort%d (", num_elements); | |
for (i=1; i<num_elements; i++) { | |
printf("int *p%d, ", i); | |
} | |
printf("int *p%d);\n", i); | |
} | |
/* assumes a minimum input of 3, since intsort2 looks different */ | |
void print_func(int num_elements) { | |
int i, j; | |
printf("void intsort%d (", num_elements); | |
/* add parameters */ | |
for (i=1; i<num_elements; i++) { | |
printf("*p%d, ", i); | |
} | |
printf("*p%d) {\n", i); | |
/* first function call */ | |
printf(" intsort%d(", num_elements-1); | |
for (j=1; j<num_elements-1; j++) { | |
printf("p%d, ", j); | |
} | |
printf("p%d);\n", j); | |
/* second function call */ | |
printf(" intsort%d(", num_elements-1); | |
for (j=2; j<num_elements; j++) { | |
printf("p%d, ", j); | |
} | |
printf("p%d);\n", j); | |
/* aaaand third (same as first) */ | |
printf(" intsort%d(", num_elements-1); | |
for (j=1; j<num_elements-1; j++) { | |
printf("p%d, ", j); | |
} | |
printf("p%d);\n", j); | |
/* close body */ | |
printf("}\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment