School of Computer Science 60-141-01 Algorithms & Programming Winter 2007 Midterm Examination # 2 Wednesday, March 21, 2007 Student Name: First Name Family Name Student ID Number: Duration of examination: 75 minutes 1. Answer all questions on this examination paper in the space provided. 2. This is a closed-book examination no notes or books or electronic computing or storage or communications devices may be used. 3. Do not copy from other students or communicate in any way. All questions will be answered only by the attending proctors. 4. All students must sign an attendance sheet at the beginning of the examination. Each student must also sign an exit attendance sheet before leaving the examination. 5. The examination must be surrendered immediately when the instructor announces the end of the test period. Failure to do so will result in a mark penalty of 2 marks. Total mark obtained: Maximum mark: 45 Page 1
Question 1. [ 10 marks ] This question deals with tracing. For the C program below, show what output would be produced. Your answers must be stated precisely, accounting for proper formatting. It is highly recommended that you show your tracing work deriving the answers, neatly, within the space provided beside the program codes. #include <stdio.h> #include <string.h> int main ( ) /* SHOW TRACE BELOW */ int L = 3, M = 4, N = 5 ; float X=2.3456 ; char S[] = Spring flowers ; char *P, *Q ; P = &S[2]; Q = strstr( S, low ) ; printf( A1 = %s ** %5s\n, Q, low ) ; printf( A2 = %.1f ** %5.2f\n, X, X ) ; printf( A3 = %d ** %d\n, Q-P, strlen(s) ) ; printf( A4 = %d ** %d\n, L & M, M N ) ; printf( A5 = %d ** %d\n, L<<2, L<M&&N-M N==L ) ; return 0 ; Neatly, print the output, if any, that would be produced by the program above. Proper formatting is very important so a grid is provided use one character per box. More than enough boxes are provided and the first row is labeled for easier referencing of column spacing. 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 A 1 = l o w e r s * * l o w A 2 = 2. 3 * * 2. 3 5 A 3 = 6 * * 1 4 A 4 = 0 * * 5 A 5 = 1 2 * * 1 0 Page 2
Question 2. [ 20 marks ] This question deals with programming of string handling functions. For each part you are given a specification for a function, including a prototype, description of input and output requirements and assumptions. For each part you are to write a complete function definition for each stated prototype. You must not use any char or string handling functions from the C libraries. However, you may reference any function that is required for an earlier Part of this Question 2. PART 2A. [ 3 marks ] Prototype: int StringLength ( char * S ) ; Input: S is a pointer to a collection of characters (char). Output: Returns the number (int) of characters (char) in S not equal to \0. Assumptions: All char values in S are valid ASCII. It is possible that \0 might not occur in this case, when the number of char values is greater than MAXLEN a return value of -1 is executed immediately. MAXLEN is defined by a #define statement elsewhere (by another programmer). /* SOLUTION Q.2.A */ int StringLength ( char * S ) int L = 0 ; while( *S!= \0 ) L++ ; S++ ; if( L > MAXLEN ) return -1 ; return L ; Page 3
Question 2. ( Continued ) PART 2B. [ 10 marks ] Prototype: int StringCompareN ( char * S, char * F, unsigned int N ) ; Input: S and F are pointers to different, non-overlapping, collections of characters (char). N is an unsigned integer value (>= 0). Output: Returns an integer value (int) indicating: 1 :: S > F; 0 :: S == F; -1 :: S < F. Assumptions: All char values in S and F are valid ASCII strings, delimited by \0. The string comparison is lexical (ie. dictionary). It is possible that either S or F or both are null strings (with length 0). Strings of length zero have minimum lexical value so all non- NULL strings are defined as greater than 0 length strings. It is possible that N is zero (0), in which case a value of 0 is returned. It is possible that N is greater than the minimum length of S and F, in which case string F is compared with string S. You may use the StringLength() function defined in Part A. /* SOLUTION Q.2.B */ int StringCompareN ( char * S, char * F, unsigned int N ) int SL, FL, k, L ; if( N == 0 ) return 0 ; /* Zero length comparison*/ SL = StringLength(S) ; FL = StringLength(F) ; if( SL == 0 && FL == 0 ) return 0 ; /* Zero length strings */ if( SL == 0 ) return 1 ; /* S is minimal length */ if( FL == 0 ) return -1 ; /* F is minimal length */ if( SL >= FL ) L = FL ; /* Determine the */ else L = SL ; /* minimum of SL, FL */ if( N < L ) L = N ; /* and initialize L */ for( k=0 ; k<l ; k++, S++, F++ ) /* Compare each char */ if( *S > *F ) return 1 ; /* S > F case */ if( *S < *F ) return -1 ; /* S < F case */ return 0 ; /* S == F case */ Page 4
Question 2. ( Continued ) PART 2C. [ 7 marks ] Prototype: char * StringFind ( char * S, char * F ) ; Input: Output: S and F are pointers to different, non-overlapping, collections of characters (char). Returns a pointer to the location in S where F is found. If F is not found in S, a value of NULL is returned. Assumptions: All char values in S and F are valid ASCII strings, delimited by \0. It is possible that either S or F or both are null strings. It is possible that the length of S is less than the length of F, in which case a NULL value can be returned. You may use the StringLength() function defined in Part 2A. You may use the StringCompareLength () function defined in Part 2B. /* SOLUTION Q.2.C */ char * StringFind ( char * S, char * F ) int L, SL, FL, j, k ; SL = StringLength(S) ; FL = StringLength(F) ; if( SL < FL ) return NULL ; /* S is smaller than F */ /* Determine maximum number of comparison substrings in S */ L = SL FL + 1 ; for( j=0 ; j < L ; j++ ) if( StringCompareLength( S+j, F, FL ) == 0 ) return (S+j) ; /* F is found in S, return location */ return NULL ; /* F is not found in S, return NULL */ Page 5
Question 3. [ 15 marks ] Below and to the left is a sample input for a C program. The input is subdivided into groups of student test data consisting of (a) a 4-digit integer ID number, (b) a First name string of maximum length 7 characters, (d) a Last name string of maximum length 9 characters, and (d) an integer Mark. The last input (corresponding to an ID) is always a zero ( 0 ). Below and to the right is an output report corresponding to the sample input provided. The report consists of a title line, then lists the input data by ID, First and Last names and the Mark for each student. Finally, the average of all the marks is outputted. NOTE: the report is formatted very specifically (a line of column numbers is provided as a reference). The average is outputted with exactly one digit following the decimal point and one blank line between the last student and the average output. 1234567890123456789012345678901 COLUMN REFERENCE ONLY! Input: Output: 1210 ID First Last Grade Maxwell 1210 Maxwell Smart 60 Smart 1234 John Smith 86 60 1176 Mary Littleton 79 1234 John Average: 75.0 Smith 86 1176 Mary Littleton 79 0 The following C code skeleton is provided to you to illustrate the program structure that must be used to obtain the input and output a formatted report, including the average mark. #include <stdio.h> #include <string.h> /* Define StudRec structure type here. */ /* 5 marks */ /* Function prototypes go here. */ /* 2 marks */ int main ( ) int N = 0 ; StudRec A[ 100 ] ; InputData( A, &N ) ; /* 3 marks */ OutputReport( A, N ) ; /* 5 marks */ return 0 ; /* Function definitions go here. */ On the following blank page, write the complete C program, including the definition of the StudRec type (a struct) and each function specified. Comments are not required, but they are recommended. The mark for each requirement is indicated above as a comment. Page 6
/* SOLUTION Q.3 */ #include <stdio.h> #include <string.h> /* Define StudRec structure type here. */ /* 5 marks */ typedef struct int ID ; char First[8] ; char Last[10] ; int Mark ; StudRec ; /* Function prototypes go here. */ /* 2 marks */ void InputData( StudRec A[], int *N ) ; void OutputReport( StudRec A[], int N ) ; int main ( ) int N = 0 ; StudRec A[ 100 ] ; InputData( A, &N ) ; OutputReport( A, N ) ; return 0 ; /* Function definitions go here. */ void InputData( StudRec A[], int *N ) /* 3 marks */ StudRec T ; scanf( %d, &T.ID ) ; *N = 0 ; while( T.ID!= 0 ) scanf( %s%s%d, &T.First, &T.Last, &T.Mark ) ; A[*N] = T ; *N = *N + 1 ; scanf( %d, &T.ID ) ; void OutputReport( StudRec A[], int N ) /* 5 marks */ int k, Sum = 0 ; float Ave ; StudRec T ; printf( ID First Last Grade\n ) ; for( k=0; k<n ; k++ ) T = A[k] ; printf( %d %9s%11s%5d\n, T.ID, T.First, T.Last, T.Mark ) ; Ave = 1.0 * Sum / N ; /* Make sure division is not integer */ printf( \n Average: %5.1f\n, Ave ) ; Page 7