// Complete this main to benchmark the search algorithms outlined in // the project specification #include #include #include #include #include #include #include #include "search.h" int main(int argc, char *argv[]){ /* if(argc < 5){ exit(1); } */ //Create lists and arrays and tree with max length // max length is 2^ maxpow // You will print and complete mega searchs maxpow-minpow times // Therefore we need a mega for loop running maxpow-minpow times // repeats: JUST the number of times you repeat a search of all numbers //creates table with all of the pointer structs in it func_table_t cfuncs[] = { // table of comparison functions {.name="linnear_array_search", .func=(void*)linear_array_search, .create=(void*)make_evens_array, .destroy=(void*)free}, // castes required to coerce {.name="linkedlist_search" , .func=(void*)linkedlist_search, .create=(void*)make_evens_list,.destroy=(void*)list_free}, // different types of functions {.name="binary_tree_search" , .func=(void*)binary_tree_search, .create=(void*)make_evens_tree,.destroy=(void*)bst_free}, // to unifying type of struct {.name="binary_array_search" , .func=(void*)binary_array_search,.create=(void*)make_evens_array,.destroy=(void*)free}, // field {.name=NULL} // 'null' terminate the table }; //A table that will hold the funciton pointers to the functions specified by user func_table_t realFuncs[] = { {.name = NULL}, {.name = NULL}, {.name = NULL}, {.name = NULL}, {.name = NULL} }; //getting arguments from console int minpow = atoi(argv[1]); int maxpow = atoi(argv[2]); int repeats = atoi(argv[3]); //creating "booleans" for console input int bt = 0; //binary tree int ll = 0; //linked list int ba = 0; //binary array int la = 0; //linked array //Setting up variables for calculations later NO LONGER NEEDED WITH FUNCTION POINTERS /*int *array; list_t *list; bst_t *tree; */ int index = 0; //checking the arguments after minpow/maxpow/size and setting booleans accordingly for(int i =3; i< argc;i++){ //NOW: adds the correlating pre-made function pointer construct to the real_funcs array if(strcmp(argv[i], "bt") == 0 && !bt){ bt = 1; realFuncs[index] = cfuncs[2]; index++; } else if(strcmp(argv[i],"ll") == 0 && !ll){ ll =1; realFuncs[index] = cfuncs[1]; index++; } else if(strcmp(argv[i], "ba") == 0 && !ba) { ba = 1; realFuncs[index] = cfuncs[3]; index++; } else if(strcmp(argv[i], "la") == 0 && !la){ la = 1; realFuncs[index] = cfuncs[0]; index++; } } //checks if no booleans where set, then all are true if(index == 0){ index =4; ba = 1; la = 1; ll = 1; bt = 1; for(int i =0; i<4; i++){ //if no booleans are set, adds all function pointers structs to realFuncs realFuncs[i] = cfuncs[i]; } } //Prints out header for table printf("%8s%9s","SIZE", "NSEARCH"); if(la) printf("%15s","array"); if(ll) printf("%15s","list"); if(ba) printf("%15s","binary"); if(bt) printf("%15s","tree"); printf("\n"); //Sets up current size variable int cursize = 1; for(int i =1; i<=minpow; i++){ cursize *= 2; } //intilize variables for calculating time clock_t start, end; double time; for(int i = minpow; i<=maxpow;i++){//all tests run max-min times int check = 0; //variable for traversing funciton pointer array printf("%8d%9d",cursize , cursize * repeats * 2);//prints size and numofchecks while(realFuncs[check].name != NULL){ //traverses array void *temp = realFuncs[check].create(cursize);//creates the tree/list/array struct fur current index start = clock(); //begins time for(int j =0; j