Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Exam #2CS-2301, B-Term 20091 Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,

Similar presentations


Presentation on theme: "Review of Exam #2CS-2301, B-Term 20091 Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,"— Presentation transcript:

1 Review of Exam #2CS-2301, B-Term 20091 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)

2 Review of Exam #2CS-2301, B-Term 20092 Results Maximum – 85 Mean – 51.2 Median – 47.5 Minimum – 29

3 Review of Exam #2CS-2301, B-Term 20093 Question 1 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!

4 Review of Exam #2CS-2301, B-Term 20094 Question 2 1.(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!

5 Review of Exam #2CS-2301, B-Term 20095 Question 3 1. (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 ). 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 ?

6 Review of Exam #2CS-2301, B-Term 20096 Question 4 1.(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(...)

7 Review of Exam #2CS-2301, B-Term 20097 Question 5 1.(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.

8 Review of Exam #2CS-2301, B-Term 20098 Question 6 1. (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.

9 Review of Exam #2CS-2301, B-Term 20099 Question 7 (array version) 1. (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 means dictionary order Testing for length, not comparing characters “Arthur” versus “Bob”

10 Review of Exam #2CS-2301, B-Term 200910 Question 7 (pointer version) 1. (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

11 Review of Exam #2CS-2301, B-Term 200911 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.

12 Review of Exam #2CS-2301, B-Term 200912 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; Q: Why do we need this? A: To declare this.

13 Review of Exam #2CS-2301, B-Term 200913 Question 9b phases volts amps next rpm phases volts amps next rpm phases volts amps next rpm motor *first, *last;

14 Review of Exam #2CS-2301, B-Term 200914 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 Issues:– Many didn’t use “->” notation! Messed up “.” notation Messed up relinking the tail! My sloppiness – did not assign NULL to newMotor->next Notice this notation!

15 Review of Exam #2CS-2301, B-Term 200915 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 Notice this notation! My sloppiness:– Should be if (head != tail) head = heal -> next; else head = tail = NULL;

16 Review of Exam #2CS-2301, B-Term 200916 Questions? On the Exam Other Review questions?

17 Review of Exam #2CS-2301, B-Term 200917 Project #6 Assignment


Download ppt "Review of Exam #2CS-2301, B-Term 20091 Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,"

Similar presentations


Ads by Google