CS 217, Intro to Programming Systems Midterm, Thursday, March 12, 1997 NAME__________________ 60 minutes, Open Books, Open Notes, NO Calculators allowed Honor Code Here:_________________________________________________________________ ____________________________________________________________________________ 1) Fun with words. Explain the following types in words: (10pts) Example: int *(*A[10])(char *) "A ten element array of pointers to functions that take char pointer and return pointer to integer" a) char *argv[10]; b) char **argv; b) const char **argv; d) int *a[MAX]; e) int* B(void *, char *); f) int (*B)(void *, char *); What are the differences between a), b), and c)? What would be returned by sizeof () for a), b), and c)? ____________________________________________________________________________ 2) Fun with Numbers: (20pts) a) Convert the following decimal numbers to Octal, Hexadecimal, and Binary. All given numbers are positive and in base 10. Use only as many bits as necessary to represent each. Example 11 = 13 = b = 1011 10 8 16 2 99 = 77 = 76 = 16 = b) Convert the following decimal numbers to 2's Complement Binary, 1's Complement Binary, and Sign Magnitide Binary. All given numbers are in base 10. Write 8 bit results. 11 = -11 = -63 = 128 = c) What are the advantages and disadvantages of i) Sign Magnitude Representation ii) 1's Complement Representation iii) 2's Complement Representation ____________________________________________________________________________ 3) The function code given below is bad in a number of ways. It (15pts) does not have bugs, it's just ugly and wrong for various reasons. Note what's bad about it, and fix it below. int m(int *anarrayofints, int sizeofarray) { /* BAD VERSION */ int indextoarray,temporaryvalue; temporaryvalue = -1; for (indextoarray=1;indextoarray<=sizeofarray;indextoarray++) { if (!(anarrayofints[indextoarray-1] > temporaryvalue)) continue; temporaryvalue = anarrayofints[indextoarray-1]; } return temporaryvalue; } Notes about what's bad: void max(int *a, int N) { /* GOOD VERSION */ } ____________________________________________________________________________ 4) What's the difference between malloc(), calloc(), and realloc()? (10pts) What are the safety considerations in using each of them? ____________________________________________________________________________ 5) Describe the important features of make and Makefiles. (5pts) ____________________________________________________________________________ 6) Describe the important features of RCS (including what it stands for). (5pts) ____________________________________________________________________________ 7) Write a function to return a copy of a NULL-terminated string. (10pts) This function should malloc the new string. Make this code as safe as you possibly can. Write any documentation that you think is reasonable to go with the function. /** YOUR DOCUMENTATION HERE **/ char* newstr(char *str) { } ____________________________________________________________________________ 8) You work for ArborSoft, a company that makes all of its money writing and maintaining binary tree code. You're called upon to rewrite two functions that manipulate binary trees. The two functions you must write are T_string_test(Tree *T, char *str) and T_free(Tree T). Be safe when writing these functions. For this round of the software, trees are defined thusly: (25pts) typedef struct tree *Tree; struct tree { void *element; int tag; struct tree *left, *right; }; /* T_string_test checks to see if the argument exists in the tree as an element, if so it returns the tag associated with that element, and if not it returns NULL. You can assume that all non-NULL element members in the tree are appropriately terminated strings (NULL-terminated). */ int T_string_test(Tree *T, char *str) { } /* T_free(Tree T) frees the tree rooted at T. */ void T_free(Tree T) { } ____________________________________________________________________________ THE END