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

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

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

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

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

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

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

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

ESC101N: Fundamentals of Computing End-sem st semester

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

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

ECE264 Spring 2013 Exam 1, February 14, 2013

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

BSM540 Basics of C Language

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

ECE264 Fall 2013 Exam 3, November 20, 2013

C: How to Program. Week /Mar/05

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

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

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

Storage class and Scope:

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

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

Chapter 2 - Introduction to C Programming

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

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

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

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

BLM2031 Structured Programming. Zeyneb KURT

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

EE 312 Fall 2017 Midterm 1 October 12, 2017

Signature: ECE 551 Midterm Exam

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

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CGS 3460 Summer 07 Midterm Exam

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

ECE264 Summer 2013 Exam 1, June 20, 2013

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013

Computer System and programming in C

Personal SE. Functions, Arrays, Strings & Command Line Arguments

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Computer Programming. Decision Making (2) Loops

Chapter 3: Arrays and More C Functionality

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

C Program Development and Debugging under Unix SEEM 3460

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

return return else return

Bit Manipulation in C

CSE 5A Final Fall 2006

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Midterm #2 CISC 220, Winter 2008

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

SAMPLE MIDTERM SOLUTION

Kurt Schmidt. October 30, 2018

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Dept. of CSE, IIT KGP

ECE264 Fall 2013 Exam 1, September 24, 2013

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

AMCAT Automata Coding Sample Questions And Answers

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

Introduction to C Language (M3-R )

Procedural Programming & Fundamentals of Programming

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 14, FALL 2012

CS 108 Computing Fundamentals. October/November Array Bootcamp

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Functions. Systems Programming Concepts

Assembler Programming. Lecture 10

Structured Program Development in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CSE 303 Winter 2008 Midterm Key

ECE551 Midterm Version 1

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

CS 1713 Introduction to Programming II

Intermediate Programming, Spring 2017*

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

Structures can be easily used as a function parameters. It is done similar as with variables.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Do not start the test until instructed to do so!

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

Midterm Exam 1 Solutions C Programming Dr. Beeson, Spring 2009

SOFTWARE Ph.D. Qualifying Exam Fall 2017

C Programming Review CSC 4320/6320

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

ONE DIMENSIONAL ARRAYS

ECE264 Spring 2014 Exam 2, March 11, 2014

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

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

Approximately a Final Exam CPSC 206

Transcription:

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