Page 1 of 7 School of Computer Science 60-141-01 Introduction to Algorithms and Programming Winter 2014 Midterm Examination # 2 Wednesday, March 19, 2014 ANSWERS Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER: 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 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 remain seated during the last 5 minutes of the examination. 5. The examination must be surrendered immediately when the instructor announces the end of the test period. 6. Each student must sign the examination class list before leaving the classroom. Failure to sign out may invalidate the examination for the student. 7. The reverse side of each page is left blank so that it may be used for rough work, or continuation of answers. Total mark obtained: Maximum mark: 40
Page 2 of 7 Question 1. [ 13 marks ] Answer each part of this question. Each part is independent of the other parts. A. Assume the following declarations: [ 2 marks ] int X = 3, Y = 4 ; char M[100] ; Write a single C language statement that places the string The sum of 3 + 4 = 7 into the array M and which refers directly to the variables X and Y. Your answer must also be correct in any case where the values assigned to X or Y are modified so that a different sum is involved. You may use any C library functions in your answer. sprintf( M, The sum of %d + %d = %d, X, Y, X+Y ) ; B. Assume that A and B are 4-bit storages. A contains the bits 0011, while B contains the bits 0101. What are the results of applying the following operators to A and B? [ 3 marks ] A 0011 0011 0011 B 0101 0101 0101 A & B = 0001 A B = 0111 A ^ B = 0110 C. Write C language statements to (i) define a union that contains members X (int), S (a 6 character string) and Y (float), (ii) define a data type called XYSunion_t based on your definition, (iii) define and initialize a variable XYS of type XYSunion_t. [ 4 marks ] union XYS_union { int X ; // If first, initialize to 0 char S[6] ; // If first, initialize to \0 float Y ; // If first, initialize to 0 or 0.0 ; // Define union typedef union XYS_union XYSunion_t ; // Define type XYSunion_t XYS ; // Define variable XYS.X = 0 ; // Init. variable
Page 3 of 7 D. Write a single C language statement that outputs (to stdout) the number of bytes in the array defined by: long int A[100]; Your answer must work on any computer that supports the C language. [ 1 mark ] printf( %d, 100*sizeof(long int) ) ; // OR as below printf( %d, 100*sizeof long int ) ; E. Assume that P1 and P2 are two char * type pointers, each pointing to a different character in the same string, so that the character pointed to by P1 occurs before the character pointed to by P2. Write a single C language statement that outputs (to stdout) the number of characters between P1 and P2. As an example, for the string ABCDE, if P1 points at A and P2 points at E, then the output would be 3 (i.e. the length of the substring BCD ). [ 1 mark ] printf( %d, P2 P1-1 ) ; F. Assume the following declarations: int N ; char * S ; Assuming only that S points to a valid string, write a single C language statement that determines the length of the string, storing the final result in N. [ 2 marks ] for( N=0; *S!= \0 ; N++ ) ; // OR as below N = strlen( S ) ;
Page 4 of 7 Question 2. [ 6 marks ] Write a complete function definition for the function StrPBrk() that conforms to the function prototype, expected behavior and assumptions stated below. char * StrPBrk( const char * S1, const char * S2 ); The purpose is to locate the first occurrence in S1 of any character found in S2, and returns a pointer to that position in S1. If no character in S2 is found in S1, a NULL value is returned. It is assumed that both S1 and S2 are valid strings. If either S1 or S2 is an empty string, the value NULL is returned. You must not use any C library functions in your answer. ANSWER: char * StrPBrk( const char * S1, const char * S2 ) { int k ; char * T1, * T2 = NULL ; for( T1 = S1 ; *T1!= '\0' ; T1++ ) for( T2 = S2 ; T2!= '\0' ; T2++ ) if( *T2 == *T1 ) return T1; return NULL ; NOTES: Although this solution is presented in a straightforward manner, there may be other correct approaches. In this problem it must be understood that it is the string data in S1 and S2 that is asserted as const, and not the pointer values.
Page 5 of 7 Question 3. [ 6 marks ] Write a complete function definition for the function StrCat() that conforms to the function prototype, expected behavior and assumptions stated below. void StrCat( char * S1, const char * S2); The purpose is to copy string S2 to a position immediately following the string S1, thereby forming a single string. Note that the original \0 delimiter in S1 is overwritten by the first character in the S2 string, so that only one delimiter occurs at the end of the modified S1 string. Assume that both S1 and S2 are valid strings, either or both may be empty, and the storage allocated to S1 is sufficient to hold the result. You must not use any C library functions in your answer. ANSWER: void StrCat( char * S1, const char * S2) { char * T ; for( T = S1, T!= '\0' ; T++ ) ; // Locate end of S1 for( ; S2!= '\0' ; S2++, T++ ) // Copy S2 to S1 *T = *S2 ; *T = '\0' ; // Delimit the final string return ; NOTES: Because of the strong restrictions in the assumptions for this problem, this solution is straightforward. Students should consider the more general problem that must deal with situations such as the limited size of the destination string, and also what to do when the concatenated string exactly fills the destination string without leaving enough room for the \0 delimiter character.
Page 6 of 7 Question 4. [ 15 marks ] The ABC Academy awards a special prize to the student (or students) who complete their 30 course program with the highest grade point average (GPA). Each course mark is out of 100 and the GPA is the average of all 30 marks. Write a complete C language program that inputs data for each student into an array of structures. Each structure must contain a unique student id number (SID), an array for holding all their course marks, and a field for the GPA. There are a maximum of 100 students in the graduating class. The program must output the complete record (i.e. SID, course marks and GPA) for all students who hold the highest GPA (there may be more than one student). You may use any C library functions in your answer. Although documentation is not required, it is recommended that you provide documentation. ANSWER: #include <stdio.h> #define MAX 50 typedef struct { int SID ; float Mark[30] ; float GPA ; Stud_t ; int main ( void) { int j, k, N = 0, EOFcheck ; float LargestGPA ; Stud_t Stud[ MAX ] ; /* Obtain all input data */ EOFcheck = scanf( %d, &(Stud[0].SID) ) ; while( EOFcheck!= EOF ) { Stud[N].GPA = 0.0 ; for( k=0 ; k < 30 ; k++ ) { scanf( %f, &(Stud[N].Mark[k]) ) ; Stud[N].GPA += Stud[N].Mark[k] ; /* Compute the average GPA */ Stud[N].GPA = Stud[N].GPA/30.0 ; N++ ; EOFcheck = scanf( %d, &(Stud[N].SID) ) ; /* Now find the largest GPA */ LargestGPA = Stud[0].GPA ;
Page 7 of 7 for( k=1 ; k<n ; k++ ) if( LargestGPA < Stud[k].GPA ) LargestGPA = Stud[k].GPA ; /* Now output the list of students with largest GPA */ for( k=1 ; k<n ; k++ ) if( Stud[k].GPA == LargestGPA ) { printf( SID = %d, Marks:, Stud[k].SID ) ; for( j=0 ; j<30 ; j++ ) printf( %f, Stud[k].Mark[j] ) ; printf( GPA = %f\n, Stud[k].GPA ) ; return 0 ; NOTES: There are many ways to solve this problem. Solutions are not evaluated based on efficiency, only on correctness and completeness. Some students may use a sort on the array of structures, using the GPA as the sort variable. The solution above does contain some statements that shorten the amount of code students should read each statement carefully to ensure full understanding. It is possible to further condense the code by using pointers and some students may have used this technique.