Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Similar documents
Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

School of Computer Science Algorithms & Programming. Fall Midterm Examination # 2 Wednesday, November 14, 2007.

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

ESC101N: Fundamentals of Computing End-sem st semester

Introduction to C Language (M3-R )

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

CS 1713 Introduction to Programming II

C mini reference. 5 Binary numbers 12

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Procedural Programming & Fundamentals of Programming

Pointers and Arrays 1

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

ECE551 Midterm Version 1

C programming basics T3-1 -

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Introduction to Data Structures. Systems Programming

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

AMCAT Automata Coding Sample Questions And Answers

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Midterm #2 CISC 220, Winter 2008

Student Number: Computer Science 211b Final Examination. 28 April hours

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Kurt Schmidt. October 30, 2018

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

CS10001: Programming & Data Structures. Dept. of Computer Science & Engineering

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Approximately a Final Exam CPSC 206

Euclid s algorithm, 133

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

EE 312 Fall 2017 Midterm 1 October 12, 2017

CS 1713 Introduction to Programming II

BLM2031 Structured Programming. Zeyneb KURT

CS349/SE382 A1 C Programming Tutorial

Lecture 02 C FUNDAMENTALS

IV Unit Second Part STRUCTURES

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Self-referential Structures and Linked List. Programming and Data Structure 1

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

C Programming Review CSC 4320/6320

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Linked List. April 2, 2007 Programming and Data Structure 1

One Dimension Arrays 1

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

ECE264 Spring 2014 Exam 2, March 11, 2014

Fundamentals of Programming

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Procedures, Parameters, Values and Variables. Steven R. Bagley

Approximately a Test II CPSC 206

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

M1-R4: Programing and Problem Solving using C (JULY 2018)

Syntax and Variables

Contents. A Review of C language. Visual C Visual C++ 6.0

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 6

For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit

Lab 3. Pointers Programming Lab (Using C) XU Silei

Characters and Strings

Question 1. [15 marks]

Largest Online Community of VU Students

C-LANGUAGE CURRICULAM

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

ECE551 Midterm Version 2

ECE264 Spring 2013 Final Exam, April 30, 2013

Signature: ECE 551 Midterm Exam

Structures and Unions in C

Introduction to string

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

CS61C : Machine Structures

CSE 303, Winter 2007, Midterm Examination 9 February Please do not turn the page until everyone is ready.

Chapter 8 - Characters and Strings

ASSIGNMENT BOOKLET. M.Sc.(Mathematics with Applications in Computer Science) Programming and Data Structures (1 st January, 2018 December, 2018)

The Hyderabad Public School, Begumpet, Hyderabad, A.P

Standard C Library Functions

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Transcription:

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.