170 likes | 187 Views
Review of Exam #2. CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Results. Maximum – 85 Mean – 51.2 Median – 47.5
E N D
Review of Exam #2 CS-2301, System Programmingfor Non-Majors (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) Review of Exam #2
Results • Maximum – 85 • Mean – 51.2 • Median – 47.5 • Minimum – 29 Review of Exam #2
Question 1 • (10 points) Define or explain the following C language terms in the space provided:– • subscript: • The expression in square brackets following an array identifier (or array expression) that denotes which element of the array to select. See Kernighan and Ritchie, pp 22, 97, 99, 100 • typedef: • A declaration/definition that gives a new name to an existing data type. (Does not delete the old name.) • malloc: • A library function in C that allocates memory from the heap and returns a pointer to that memory. • linked list: • A set of data elements, generally all of the same type, that are allocated independently of each other that are linked together by pointers in a linear sequence. An element points to the next element — it does not “call” it! • sizeof: • A unary operator in C that returns the size (in bytes) of the result of an expression or of a data type. Applies to any type, not just a character array initialized by a string! Review of Exam #2
Question 2 • (5 points) Evaluate the following expressions, where S as declared as follows:– char S[] = "A simple string.\n"; • (a) strlen(S) 17. • (b) sizeof(S) 18. • (c) *S The character 'A'. • (d) *(S+17) The null character; i.e., '\0'. • (e) strcmp(S+16, "\n") 0 — answer sheet was incorrect! Review of Exam #2
Question 3 • (5 points) Explain the purpose of the functions fopen() and fclose(). What is the type of the result returned by fopen()? fopen() “opens” a file. That is, it prepares the file for access by the program. This means that the operating system finds the file and sets up its internal data structures so that the program can read, write, or append, according to the mode. fopen() returns a pointer to type FILE (which is defined in <stdio.h>). fclose() “closes” the file. That is, it writes out any data to the disk or file storage medium, cleans up the data structures that the system maintained for the file, and generally completes the operations of preserving the data on the disk or file system. What are stdin, stdout, stderr? Review of Exam #2
Question 4 • (10 points) Explain the loop in the following function — in particular, how it steps through all of the characters of the string and only the characters of the string. int isAllLowerCase(char *c) {// PRE:- c is a string of zero or more characters// POST:- returns 1 if all letters are lower case// returns 0 if any letter is upper case for (; *c; c++) { if (*c >= 'A' && *c <= 'Z') return 0; } return 1; } // int isAllLowerCase(...) Review of Exam #2
Question 5 • (10 points) Let the function isAllLowerCase be declared as in Question 4. For each of the following expressions or statements, tell whether it compiles correctly and, if it does compile correctly, what is the result of attempting to execute it. (a) isAllLowerCase("Yes, we can. ") Compiles correctly and returns zero (b) isAllLowerCase('x') Does not compile because 'x' is not a pointer. (c) isAllLowerCase(0) Compiles correctly because 0 denotes the NULL pointer; causes a segmentation fault when executing. (b) isAllLowerCase(5) Does not compile because 5 is not a pointer. (e) char s[] = {'\0', 'A', 'b', 'C', 'd', 'E'};printf("%d",isAllLowerCase(s)); Compiles correctly, Prints the number 1, because the function encounters the character '\0' before encountering any uppercase letters. Review of Exam #2
Question 6 • (5 points) Explain why the following two function prototypes are identical. int main(int argc, char *argv[]); int main(int argc, char **argv); Pointers and arrays are the same thing in C. Specifically, argv[] and *argv both denote pointers, in this cases they are pointers to pointers to characters. Review of Exam #2
Question 7 (array version) • (20 points) Write an implementation of the C library function strcmp(char *s, char *t). See the cover sheet this exam for an explanation of this function. [Hint: there are many correct implementations of this function. Several were given in Kernighan and Ritchie.] /* array version */ int strcmp(char *s, char *t) { int i; for (i = 0; s[i] == t[i]; i++) if (s[i] == '\0') return 0; return s[i] – t[i]; } • Issues:– • one loop, not two • Testing for equality • “Art” versus “Arthur” • Lexical order meansdictionary order • Testing for length, notcomparing characters • “Arthur” versus “Bob” Review of Exam #2
Question 7 (pointer version) • (20 points) Write an implementation of the C library function strcmp(char *s, char *t). See the cover sheet this exam for an explanation of this function. [Hint: there are many correct implementations of this function. Several were given in Kernighan and Ritchie.] /* pointer version */ int strcmp(char *s, char *t) { for (; *s == *t; s++, t++) if (*s == '\0') return 0; return *s – *t; } • Issues (continued):– • Some misread “implement” • return < 0 Review of Exam #2
Question 8 • In a struct, memory is allocated for each, individual member. All members can be accessed. • In a union, memory is allocated for only one member, namely the largest. Only one member can be stored in a union at any one time, and the program has to have a means of knowing which member is populated in each individual union. Review of Exam #2
A: To declare this. Q: Why do we need this? Question 9a • (a) Declare a struct to represent a list element for one motor, and define a typedef called motor to give a type name to that struct. Don’t forget to include a link to the next element on the list. typedef struct _motor { float voltage; float amps; float rpm; int phases struct _motor *next; } motor; Review of Exam #2
volts volts volts amps amps amps rpm rpm rpm phases phases phases next next next Question 9b motor *first, *last; Review of Exam #2
Notice this notation! • Issues:– • Many didn’t use “->” notation! • Messed up “.” notation • Messed up relinking the tail! • My sloppiness – did not assignNULL to newMotor->next Question 10a (a) Write a function called addMotor() that adds a new element of type motor to the end of the list. Be sure to deal with the case where the list is empty — i.e., where both first and last are NULL. The prototype for your function should be void addMotor(motor *newMotor) { if (newMotor) /*don’t do anything if newMotor is NULL*/ if (tail ) //i.e., list is not empty tail = tail -> next = newMotor; else head = tail = newMotor; } //addMotor Review of Exam #2
Notice this notation! • My sloppiness:– • Should beif (head != tail) head = heal -> next;else head = tail = NULL; Question 10b (b) Write a function called getFirstMotor() that removes the first element from the list and returns a pointer to it. Be sure to deal with the cases where the list is empty and where the list has only one element. The prototype for your function should be motor *getFirstMotor(void) { motor *first = head; if (head) { //i.e., list is not empty head = head-> next; if (head == NULL) tail = NULL;/*i.e., list has become empty */ } return first; } //getFirstMotor Review of Exam #2
Questions? On the ExamOther Review questions? Review of Exam #2
Project #6 Assignment Review of Exam #2