CS 202, fall 2019 Exam 1 on paper Grading - unless stated otherwise each part is worth 1 point. There are a total of __ points. Please fill in the following. 0) Name: 1) C keywords (1/2 point each) For this section answer with the appropriate C keyword 1.1) floating point variable (single precision) - float 1.2) integer variable - int 1.3) declare a structure data type - struct 1.4) define a new data type - typeof 1.5) used in place of if/else if/else if/else, four keywords - switch, case, default, break 1.6) stop a loop (skip to after the loop) - break 1.7) integer variable that takes up 2x as many bytes (one keyword) - long 1.8) floating point variable (double precision) - double 1.9) declare a local variable that behaves like a global variable - static void f() { static int x = 5; } // local scope, global persistence 1.10) loop based on a condition (one keyword) - while 1.11) character variable - char 1.12) integer variable that takes up 1/2 as many bytes (one keyword) - short 1.13) variable whose value may change at any time even if the C program did not change it - volatile 1.14) declare a union grouping of data types - union 1.15) declare an integer that is always non-negative - unsigned 1.16) go back to the beginning of a loop - continue 1.17) stop a function and send back a value - return 1.18) create a register variable (may be faster) - register 1.19) variable that cannot be modified - const 1.20) automatic variable - auto // aka variable on the stack, default 1.21) number of bytes of a variable or data type - sizeof 1.22) declare variable from a separate C file - extern Note - does not allocate storage, one of the files needs to declare it without extern 1.23) enumerated data type - enum 1.24) type of a function that does not return anything - void 1.25) loop based on a condition that runs at least once (two keywords) - do, while 1.26) loop with init, condition, and increment - for 1.27) used in place of a loop, not recommended for use in C - goto Also called "jump" in some assembly languages. 1.28) conditional execution (two keywords) - if, else 2) C data types For each of the following, give the values for programs compiled with gcc on CS. 2.1) integer value of 'a' - 97 2.2) integer value of '0' - 48 2.3) min/max values for int (approximate, to one significant figure) - +/- 2,000,000,000 2.4) integer value of ' ' - 32 2.5) min/max values for unsigned char (exact) - 0 to 255 2.6) circle which fit within an unsigned char: -1000, -200, -100, 0, 100, 200, 1000 0, 100, 200 2.7) # bytes for long int - 8 2.8) # bytes for short - 2 2.9) integer value of 'A' - 65 2.10) # bytes for unsigned short - 2 2.11) # bytes for double - 8 2.12) # bytes for float - 4 2.13) # bytes for int - 4 2.14) # bytes for char - 1 2.15) integer value of '\n' - 10 2.16) min/max values for unsigned int (exact, give formula): 0 to (2**32 - 1) 2.17) circle which fit within an int: -100, 0, 1000, 1e6, 4e6, 1e9, 4e9, 1e10 -100, 0, 1000, 1e6, 4e6, 1e9 3) C Expressions. Write down the value of the C expression, and also indicate the new values of any variables that were modified. Assume that the following was executed just before each of the following. Consider each part separately (assume the initialization code was just run). int i = 10, j = 14, k = ' '; char c1 = 'k', c2 = 100, c3=0; float f1 = 3.14; int A[4] = {1, 2, 3, 4}; char s[6] = "hi ok"; int *B = (int *) malloc(sizeof(int) * 3); B[0] = B[1] = B[2] = 42; 3.1) *(++A) // note - error because no pointer arithmetic on an array variable. // if A were a pointer, int *A = (int *) malloc... 2, and A would change to point to the second location 3.2) j < k 14 < 10 0 // note: false is not a correct answer. there is no false in C. 3.3) (i = j) < (c1 = c2) // note that = is assignment, not checking equality. == is for checking equality. i=10, c1=100 (10) < (100) 1 3.4) 3 < 4 && 4 < 3 || 4 != 5 1 && 0 || 1 0 || 1 1 3.5) (c2 = c3) || (c2 = c3 = i) c2=0 0 0 || (c3=10, c2=10) 0 || 1 1 3.6) 15 & 7 1111b & 0111b ------- 0111b = 7 3.7) i = j = ++c3; c3=1 i = j = 1 i=1, j=1 1 3.8) B[0] 42 3.9) *(A + 2) // pointer arithmetic would give an error on an array // but if it did work, then... 3 3.10) (i ^ j) >> 1 1010b ^ 1110b ------- 0100b = 4 4 >> 1 2 3.11) 4 + 2 * 3 4 + 6 10 3.12) c3 && c2 || s[5] 'k' && 100 || s[5] 1 1 4) Functions every citizen should know. Note - you don't need to know the process management, threads, or synchronization functions (not yet anyway). 4.1) scanf, meaning of return value - the number of fields that were read/filled. incorrect - status. incorrect - the information read. 4.2) fopen, what it does - tries to open a file from the file system incorrect - returns a FILE pointer (that is not what the question asked). 4.3) free, what it does - de-allocate memory. free up memory that was malloc'ed. incorrect - deletes memory. 4.4) strcmp, meaning of return value - 0 if strings are identical, < 0 if first string is "larger", > 0 if second "larger" Note - "larger" based on alphabetical order using the ASCII table. Note - 'a' is greater than 'A' 4.5) fprintf, meaning of first two parameters - 1st - file pointer to print to, 2nd - format string for printing 4.6) realloc, what it does - reallocate memory. if requested size for a pointer is larger than already allocated, then allocate enough memory for it and copy the contents over, and free previous. incorrect - copy memory. 4.7) strlen, type of parameter - char *, C string 4.8) malloc, meaning of return value - pointer to memory that has been allocated, NULL if failure incorrect - memory. 4.9) time, meaning of return value - # of seconds since the "unix epoch" (Jan 1 1970). incorrect - seconds. incorrect - current time and date. incorrect - /description of unix time command - measures the amount of CPU time of a process/. man 2 time - C function man time - unix command 4.10) sleep, what it does - halts/pauses for the given # of seconds. 4.11) fgetc, meaning of return value - character read from the file stream, or EOF if none left. incorrect - status. 4.12) memset, what it does - sets a block of memory to some given value. 4.13) usleep, difference between it and sleep - usleep is in microseconds, sleep is in seconds. 4.14) open, difference between it and fopen - fopen is a C standard library function, open is a unix/linux system call. open has many more options. fopen is the same on just about any OS/compiler, open will be different (or not exist) depending on the OS. 5) Data Structures Chart (6 points) Complete the following data structures chart. For each spot, put the best-case and worst-case running time for the operation when the data structure contains n items. Note that for delete you should assume the location or pointer of the item is already known. For stack and queue, for insert/delete put in the info for push/pop and enqueue/dequeue. For hash table assume we deal with collisions using "buckets". best worst Array Linked List Stack Queue Hash table Binary search tree Sorted Unsorted balanced not-necessarily-balanced ------------------------------------------------------------------------------------------------------- Insert best log(n) 1 1 1 1 1 log(n) 1 worst n 1 1 1 1 1 log(n) n Lookup best O(1) O(1) O(1) O(1) O(1) O(1) O(1) O(1) worst O(log n) n n n n n log(n) n Delete best 1 1 1 1 1 1 1+ 1 worst n 1 1* 1* 1* 1* ? ? * if we have a pointer to the node before the one we're deleting + deleting a leaf if already have pointer to it and its parent 6) Data structures For each data structure and operation listed, give the short description of the algorithm for the operation. 6.1) Ordered array, lookup - binary search. check the lookup key against the middle of the array, look in first half of array if smaller / second half if larger, and repeat until found (or not). 6.2) Ordered array, delete - find the item to delete. shift all items to the right of the deleted item by 1. 6.3) Binary search tree (not necessarily balanced), insert - do a lookup for the item and put it when you encounter NULL. Check root node, if item is smaller go left, if item is larger go right; repeat until leaf or NULL. Create new node and put it there. 6.4) Unordered array, lookup - linear search, scan through entire array until found or the end. 6.5) Unordered array, delete - mark this location as available, and move the last item into this available spot, and decrease by 1 the number of items in the array. 6.6) Linked list, delete - need to have a pointer to the previous node. adjust previous's next pointer to be the deleted item's next pointer. free the node. 6.7) Hash table (using buckets for collisions), lookup - compute the hash function of the key being looked up, go and do a lookup in the linked list at that spot in the table (linked list at index h(key)). 7) Memory Given the following program, list where the variable (the value) is located (stack, heap, data, or code). For pointer or array variables, also list whether the address is a stack/heap/data/code address. #include char *lines[1024]; int main() { char buffer[1024]; int len = 0; while(fgets(buffer, 1024, stdin) != NULL) { lines[len++] = strdup(buffer); if(len >= 1024) break; } return 0; } 7.1) len - stack 7.2) buffer - stack, an address for memory on the stack 7.3) buffer[0] - stack 7.4) lines - data, data 7.5) lines[0] - data, an address for memory in the heap Note - &(lines[0]) - that is an address in the data section of memory 7.6) *(lines[0]) - heap