LAST Name FIRST Name Points... 20 points total. (1) is worth 5 points. (2) 5 points. (3) 5 points. (4) 5 points. Grading... * For all problems except number 2, a / is -.5 and a X is -1. For number 2, each / is -1 and X is -2. 1. Random questions about C (1a) Give code to declare an array of 99 doubles. Answer: double nums[99]; (1b) Give code to declare an array of 500 C strings. Answer: char strings[500][100]; Note: 1/2 credit was given for something like char blah[100]; strings[0] is a character array with 100 characters. One C string would be like char s[100]; One char pointer would be char *s; This is a C string, but doesn't have any storage behind it... You could use it like s = (char *) malloc(sizeof(char) * 100); You could use it like... void fun(char *s) { ... } (1c) What is the largest value that can be stored in a 2-byte unsigned integer? Answer: 2^16 - 1. Note: If you said 2^16 you were off by one, but I didn't take off Note: 1/2 credit was given if you said something like 255, 2^8, or some other power of 2 other than 2^16. If you said 10^9 or something like that, that was no credit. (1d) Give code to declare a structure type that holds the following fields: double gpa, int years, char name[100] Answer: struct data {double gpa; int years; char name[100];}; struct data d; (1e) Give C code to ask the user to type a character. Then the character should be printed as a character, as an integer, and in hex. If they typed 'a', then the program would print a 97 0x61 Answer: printf("Type a character: "); char ch; scanf("%c",&ch); printf("%c %i 0x%x\n", ch, ch, ch); Note: 1 minor mistake wasn't taken off for, but at least 2 minor mistakes meant 1/2 credit. Note: if scanf and printf were both very far from being right, 0 credit. Note: for printf you give the value of the variable (ch above), while for scanf you give the address of where to store the value (&ch above). 2. Give C code Suppose we have these variables struct info {int id; char name[100];}; struct info data[100000]; int num_data_items=0; And suppose num_data_items is kept up to date so it has the total number of items in the data array being used, and the data array is kept as an unsorted array. Complete the code for the lookup function. It should return the index into the array where the info is for the given id, or return -1 if there isn't an info data item with that it. int lookup(int id) { int i; for(i=0; i < num_data_items; i++) if (data[i].id == id) return i; return -1; } Note: id is an integer, not a string. So don't use strcmp to compare id's, just use == Note: you should return -1 /after/ the for loop, not after the if inside the for loop, and not as an else to the if. For example, the following would be wrong for(i=0; i < num_data_items; i++) { if (data[i].id == id) return i; else return -1; } That would return -1 if the id didn't match the first thing in the array. In words/pseudocode, describe what lookup would entail if the data array were kept as a /sorted/ array. Code/Pseudocode is... min = 0; max = end; mid = (max-min)/2; do { if (data[mid] == what_looking_for) return mid; if (data[mid] < what_looking_for) max = mid-1; else min = mid+1; mid = (max-min)/2; } while (max > min); return -1; In words that's... Look at the middle of the array. If it matches what we're looking for return it. If what we're looking for is less than the middle element, then look in the first half. If what we're looking for is greater than the middle element, then look in the second half of the array. Repeat. Note: I was fairly lenient on this one. As long as you talked about dividing things in half and looking at the middle I gave you full credit. If you just mentioned some facts about binary search (e.g., that it takes log(n) time) but didn't describe how it works, that was 1/2 credit. 3. Think... (3a) Rank the data structures sorted array, unsorted array, and hash table from best to worst for doing inserts (assume the hash table behaves well). best .... -> worst Answer: unsorted array, hash, sorted array (3b) Rank the same three from best to worst for doing lookups best .... -> worst Answer: hash, sorted array, unsorted array (3c) Suppose your computer can do 1 billion operations per second. What is the largest number of items that could be stored in an unsorted array and still have lookups take at most 1 thousandth of a second? Answer... The time it would take is time/operation * #operations. In this case that's (1/10^9) * n, if n is the number of items in the array. If we want that to be at most 1/1000, then we set (1/10^9) * n = 1/1000 Solving for n, n = (10^9) / 1000 = 1,000,000 (3d) Same question, but now for a sorted array. Answer... Same process to solve it as 3c, but not the #operations is log(n). So... (1/10^9) * log(n) = 1/1000 log(n) = 1,000,000 n = 2^(1,000,000) (3e) Evaluate the following (assume numbers are written in decimal) Note: & | ^ are bitwise operators, while && || are Boolean operators. The bitwise operators look at the bits. The Boolean ones only care of the numbers are 0 or not. Note: full credit if you got 0 or 1 of them wrong. 1/2 credit if you got 2 or 3 of them wrong. otherwise no credit. 38 in binary = 100110 29 in binary = 011101 38 & 29 = 000100b = 4 38 | 29 = 111111b = 63 38 ^ 29 = 111011b = 59 38 && 29 = true && true = 1 39 || 29 = true || true = 1 4. Math/memorize (4a) 1 + 2 + 3 + 4 + ... + 300 = 300*(301)/2 = 45150 (4b) 1 + 2 + 4 + 8 + ... + 2^20 = (2^21-1)/(2-1) = 2^21 - 1 Note: this uses a different formula than 4a!!! (4c) log(2^16) = 16 * log(2) = 16 Note: the exponent comes down outside the log (4d) How many bits in 1 gigabyte? If you define a gigabyte as 1 billion bytes, then 8 billion If you define a gigabyte as 2^30, then 2^33 Note: 8 bits = 1 byte (4e) (10^4)^5 / (10^5) = 10^15 Note: 10^i / 10^j = 10^(i-j) 5. On computer part of the exam... * In your handin directory, create a directory called exam2 * In the exam2 directory, create a text file called exam2.txt A) 5 points: In exam2.txt, put answers to the on-paper questions if you think you got any of them wrong, but then know the correct answer by using the computer. The exam is exam2.txt in the class code directory. B) 5 points: For question (1e), create a program called print_char.c with the program. Compile it into a file called pc. A correct working version is called print_char in the class code directory. C) 5 points: Create a program called stats.c that reads from standard input and ... Reads an integer, call it n. Reads n double floating point numbers from standard input. After reading all of the numbers, print the min, max, and average of the double floating point numbers that were read. Compile it into a file called st. A correct working version is called stats in the class code directory. Grading on part A... * no mistakes fixed: 0/5 * at least one mistake fixed: 3/5 * at least two mistakes fixed: 4/5 * at least three mistakes fixed: 5/5 * if you didn't have many mistakes to fix... 4/5 if you fixed at least half of them 5/5 if you fixed at least 75% of them problem doesn't count for any points if you just didn't have any mistakes on the paper part Grading on parts B and C... * Some part of the output is correct: 4/5 * Compiles, runs, is heading in the right direction: 3/5 * Compiles, runs, but isn't really on the right track: 1/5 * Doesn't compile: 1/5 * Couldn't find your file: 0/5 Note... * scanf wants the address of the variable, printf wants the value for ints/floats. int x; scanf("%i", &x); printf("%i", x); * %i and %d are both integers (d is for decimal). floats are %f, doubles are %lf.