CS 1723, Data Structures, Answers to Final Examination 3. Recursive binary search function: /* from main function */ index = (int)(M*drand48()); x = a[index]; result = bin_search(a, 0, M-1, x); printf("Search for: %d, found at %d, same as %d\n", x, result, index); ... /* bin_serach: search for x in a from p to r */ int bin_search(int a[], int p, int r, int x) { int q; if (p > r) return -1; q = (p + r)/2; if (a[q] == x) return q; if (x < a[q]) return bin_search(a, p, q-1, x); /* else if (x > a[q]) */ return bin_search(a, q+1, r, x); } runner% final_3 Array sorted, 100000 items Search for: 24311336, found at 24295, same as 24295 4. Command line arguments. Part(a): All answers Yes. runnner% cat final_4.c #include void printit(char *argv[]); void main(int argc, char *argv[]) { char *myargv[] = {"mycc", "-o", "prog", "prog.c", NULL}; char **p = argv; printf("Part(b)(i): %s\n", *p); printf("Part(b)(i): %c\n", **p); printf("Part(b)(i): %s\n", *(p+3)); printf("Part(b)(i): %c\n", *(*(p+3) + 4)); printit(argv); printit(myargv); } void printit(char *argv[]) { int line = 0; char **p = argv; printf("Part(c):Array of Strings:\n"); while (*p) { /* or while(*p != NULL) */ printf(" Line %d:\"%s\"\n", line, *(p++)); line++; } } runner% cc -o final_4 final_4.c runner% final_4 -o prog prog.c (Extra args not used) Part(b)(i): final_4 Part(b)(i): f Part(b)(i): prog.c Part(b)(i): . Part(c):Array of Strings: Line 0:"final_4" Line 1:"-o" Line 2:"prog" Line 3:"prog.c" Part(c):Array of Strings: Line 0:"mycc" Line 1:"-o" Line 2:"prog" Line 3:"prog.c" 5. Linked list of characters runner% cat final_5.c #include #include struct lnode { char data; struct lnode *next; }; int listlength(struct lnode *list); struct lnode *makelist(char *s); void printlist(struct lnode *list); void main(void) { struct lnode *list; char t[] = "Why worry?"; list = makelist(t); printlist(list); printf("%d\n", listlength(list)); } int listlength(struct lnode *list) { int len = 0; while (list != NULL) { list = list -> next; len++; } return len; } void printlist(struct lnode *list) { while (list != NULL) { printf("%c", list -> data); list = list -> next; } printf("\n"); } struct lnode *makelist(char *s) { struct lnode *oldlist = NULL; struct lnode *newlist; while (*s != '\0') { newlist = (struct lnode *) malloc(sizeof(struct lnode)); newlist -> data = *s; newlist -> next = oldlist; oldlist = newlist; s++; } return newlist; } runner% cc -o final_5 final_5.c runner% final_5 ?yrrow yhW 10