ECE264 Spring 2013 Exam 1, February 14, 2013

Similar documents
ECE264 Summer 2013 Exam 1, June 20, 2013

ECE264 Fall 2013 Exam 1, September 24, 2013

ECE264 Spring 2013 Final Exam, April 30, 2013

ECE264 Fall 2013 Exam 2, October 24, 2013

ECE264 Spring 2014 Exam 2, March 11, 2014

ECE264 Fall 2013 Exam 3, November 20, 2013

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

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

CSE 303 Midterm Exam

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

ECE551 Midterm Version 1

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

CSE 333 Midterm Exam July 24, Name UW ID#

Signature: ECE 551 Midterm Exam

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

File I/O. Arash Rafiey. November 7, 2017

File IO and command line input CSE 2451

C programming basics T3-1 -

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.

ESC101N: Fundamentals of Computing End-sem st semester

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

ECE551 Midterm Version 2

Sample Problems for Quiz # 2

PRINCIPLES OF OPERATING SYSTEMS

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

Arrays, Strings, & Pointers

CS 1713 Introduction to Programming II

Arrays and Pointers in C. Alan L. Cox

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

ECE 264 Purdue University

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

CSE 333 Midterm Exam July 24, 2017 Sample Solution

CS1003: Intro to CS, Summer 2008

Arrays, Pointers and Memory Management

Pointers and File Handling

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

C Sample Code. Nyoman Bogi Aditya Karna Sisfo IMTelkom

ECE 551D Spring 2018 Midterm Exam

CS 0449 Sample Midterm

Programming in C. Pointers and Arrays

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Today s Learning Objectives

Computer Science Foundation Exam

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

COS 126 General Computer Science Spring Written Exam 1

CSE 333 Midterm Exam 2/14/14

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

Today s class. Review of more C Operating system overview. Informationsteknologi

ECE551 Midterm Version 1

Organization of a file

Kurt Schmidt. October 30, 2018

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

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

Final Intro to C Review

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

CSE413 Midterm. Question Max Points Total 100

gcc o driver std=c99 -Wall driver.c everynth.c

Basic and Practice in Programming Lab7

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Question 1. [15 marks]

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

CSE 333 Midterm Exam 7/22/12

Understanding Pointers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Introduction to file management

CSE 333 Midterm Exam 5/10/13

Procedural Programming

ALGORITHM 2-1 Solution for Exercise 4

CS 1713 Introduction to Programming II

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

Approximately a Test II CPSC 206

CSC 210, Exam Two Section February 1999

CS 241 Data Organization Pointers and Arrays

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Lecture 07 Debugging Programs with GDB

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

Dynamic memory allocation

Processes. Johan Montelius KTH

Computer Science Foundation Exam

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

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

C mini reference. 5 Binary numbers 12

CS 2301 Exam 3 B-Term 2011

Sample Problems for Quiz # 3

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

A process. the stack

CS240: Programming in C. Lecture 14: Errors

A Crash Course in C. Steven Reeves

Transcription:

ECE264 Spring 2013 Exam 1, February 14, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it. I also declare that I will not discuss/share this exam with anybody on February 14, 2013. I understand that, if I fail to honor this agreement, I will receive a score of ZERO for this exam and will be subject to possible disciplinary action. Signature: You must sign here. Otherwise you will receive a 2-point penalty. Read the questions carefully. You must return all pages before you leave the room. Otherwise, your exam will not be graded and you will receive a zero score. This is an open-book, open-note exam. You may use any book, notes, or program printouts. No personal electronic device is allowed. You may not borrow books from other students. Two learning objectives (recursion and file) are tested in this exam. To pass an objective, you must receive 50% or more points in the corresponding question. Questions (Total 15 points) 1. recursion (6 points) 2. file (6 points) 3. pointer (3 points) 1

Contents 1 Recursion (6 points) 3 1.1 Recursive Relation (1 points).......................... 3 1.2 Trace Recursive Function (2 points)....................... 4 1.3 Permutation (3 points).............................. 5 2 File (6 Points) 8 3 Pointers (3 points) 11 Learning Objective 1 (Recursion) Pass Fail Learning Objective 2 (File) Pass Fail Total 2

1 Recursion (6 points) For a given positive integer, we want to partition it into the sum of some positive integers, or itself. For example, 1 to 4 can be partitioned as 1 = 1 2 = 1 + 1 3 = 1 + 1 + 1 4 = 1 + 1 + 1 + 1 = 2 = 1 + 2 = 1 + 1 + 2 = 2 + 1 = 1 + 2 + 1 = 3 = 1 + 3 = 2 + 1 + 1 = 2 + 2 = 3 + 1 = 4 We observe that there exists One way to partition 1. Two ways to partition 2. Four ways to partition 3. Eight ways to partition 4. In general, there are 2 n 1 ways to partition value n. You do not have to prove this. Also by observation, we find One 1 is used to partition 1. Two 1 is used to partition 2. Five 1 are used to partition 3. Twelve 1 are used to partition 4. 1.1 Recursive Relation (1 points) How many 1 are used for partitioning value n? Let f(n) be the number of 1 used for partitioning n. f(1) = 1 and f(2) = 2. Write down the general rule. You can write down a recursive form. You do not need to write the closed form. 3

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1.2 Trace Recursive Function (2 points) Consider the following program. # include <stdio.h> # include <stdlib.h> int f( int n, int * max, int * sum, int depth ) int fnm1 ; /* f(n - 1) */ int fnm2 ; /* f(n - 2) */ int fsum ; /* f(n - 1) + f(n - 2) */ (* sum ) ++; /* how many times f is called? */ if ((* max ) < depth ) * max = depth ; if ((n == 0) (n == 1)) return 1; fnm1 = f( n - 1, max, sum, depth + 1); fnm2 = f( n - 2, max, sum, depth + 1); fsum = fnm1 + fnm2 ; return fsum ; int main ( int argc, char * * argv ) int max = 0; int sum = 0; int val = 6; int fv = f(val, & max, & sum, 1); printf ("f(%d) = %d, max = %d, sum = %d\n", val, fv, max, sum ); return EXIT_SUCCESS ; For your reference, the values of f(n) is n 0 1 2 3 4 5 6 7 8 9 10 f(n) 1 1 2 3 5 8 13 21 34 55 89 sum 1 1 3 What are the values of max and sum? (0.5 point each) You can write an arithmetic expression without calculating the result. For example, if the answer is 17, you can write 1 + 5 + 11. 4

Explain your answers. (1 point) 1.3 Permutation (3 points) In PA03, you wrote a program to permute a set of characters. Suppose the input array contains both digits (1, 2, 3, 4) and alphabets (A, B, C, D): #define MAX_LENGTH 8... char set[max_length] = 1, A, 2, B, 3, C, 4, D ; We want to permute the elements. There are two restrictions: (1) The first element (index is 0) must be a digit. (2) Two digits must be separated by one and only one alphabet. Thus, the following are valid permutations: 1 C 2 D 4 A 3 B 4 B 1 C 3 A 2 D 3 D 2 B 1 A 4 C The following are invalid permutations 1 A 2 B C D 4 3 B A C D 1 3 4 2 A B 4 D 1 C 2 3 Make necessary changes to the program below so that only valid permutations are printed. Hint: Generating only valid permutations is easier (fewer lines of code) than generating all possible permutations and then eliminating the invalid ones. 5

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # include <stdio.h> # include <stdlib.h> # define MAX_LENGTH 8 void printpermute ( char * charset, int length ); void swap ( char * c1, char * c2) char t = * c1; * c1 = * c2; * c2 = t; void recursivepermute ( char * charset, int ind, int length ) int iter ; if ( ind == length ) printpermute ( charset, length ); for ( iter = ind ; iter < length ; iter ++) swap (& charset [ iter ], & charset [ ind ]); recursivepermute ( charset, ind + 1, length ); swap (& charset [ iter ], & charset [ ind ]); void permute ( char * charset, int length ) recursivepermute ( charset, 0, length ); int main ( int argc, char * * argv ) char set [ MAX_LENGTH ] = 1, A, 2, B, 3, C, 4, D ; permute (set, 8); return EXIT_SUCCESS ; void printpermute ( char * charset, int length ) int iter ; 6

43 44 45 46 47 48 for ( iter = 0; iter < length ; iter ++) printf ("% c ", charset [ iter ]); printf ("\ n "); 7

2 File (6 Points) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 The following is an attempt for the countstring function in the fourth programming assignment. This function, unfortunately, does not work. In fact, it has many problems. You need to identify the problems and describe the solutions. The problems are 1. The countstring always returns EXIT FAILURE regardless of the arguments file1 and file2. 2. The program never stops. 3. The output file is empty. 4. The program s output appears? times sometimes is a garbage value. 5. The program always reports appears 0 times even though the input file (whose name is given by the argument file1) contains the string str. 6. The program never stops if the input file contains the string. # include <string.h> # include <stdlib.h> # include <stdio.h> # define LINE_LENGTH 80 /* length of a line. Each line has at most 80 characters, including \ n and \0 */ /* * The function counts the occurrences of a string in the input file. * The string is case - sensitive. " ECE " and " ece " are different. * * file1 : name of the input file * file2 : name of the output file * str : the string to search * * This function returns EXIT_SUCCESS if it finishes without * problem. The function returns EXIT_FAILURE if there is a problem * in opening the file, reading from the input file, or writing * to the output file. * * It is possible that the string may appear multiple times in the * same line. If this occurs, the function should count all * occurrences. * * For simplicity, we assume that each line contains at most 80 * characters. Use LINE_LENGTH and do not use 80. * */ 8

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 int countstring ( char * file1, char * file2, char * str ) FILE * fptrin = fopen (" file1 ", "r "); if ( fptrin == NULL ) return EXIT_FAILURE ; FILE * fptrout = fopen (" file2 ", "w "); if ( fptrout == NULL ) fclose ( fptrin ); return EXIT_FAILURE ; char oneline [ LINE_LENGTH ]; int counter ; char * cptr ; fclose ( fptrin ); fclose ( fptrout ); while ( fgets ( oneline, LINE_LENGTH, fptrin ) == NULL ) cptr = oneline ; while ( cptr!= NULL ) cptr = strstr ( cptr, str ); if ( cptr!= NULL ) counter ++; fprintf ( fptrout, "%s appears %d times.\n", str, counter ); return EXIT_SUCCESS ; int main ( int argc, char ** argv ) /* argv [1]: input file name argv [2]: output file name argv [3]: string to search 9

72 73 74 75 */ return countstring ( argv [1], argv [2], argv [3]); 10

3 Pointers (3 points) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 For the following program, # include <stdio.h> # include <stdlib.h> void swap1 ( int a, int b) int t = a; a = b; b = t; void swap2 ( int * a, int * b) int * t = a; a = b; b = t; void swap3 ( int * a, int * b) int t = * a; a = b; * b = t; void swap4 ( int * a, int * b) int t = * a; * a = * b; * b = * t; void swap5 ( int * a, int * b) int t = a; a = b; b = t; void swap6 ( int * a, int * b) 11

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 int t = * a; a = b; b = t; void swap7 ( int a, int b) int * t = a; * a = b; b = t; int main ( int argc, char * * argv ) int u; int t; swap1 (u, t); printf (" swap1 : u = %d, t = %d\n", u, t); swap2 (& u, & t); printf (" swap2 : u = %d, t = %d\n", u, t); swap3 (& u, & t); printf (" swap3 : u = %d, t = %d\n", u, t); swap4 (& u, & t); printf (" swap4 : u = %d, t = %d\n", u, t); swap5 (& u, & t); printf (" swap5 : u = %d, t = %d\n", u, t); 12

82 83 84 85 86 87 88 89 90 91 swap6 (u, t); printf (" swap6 : u = %d, t = %d\n", u, t); swap7 (u, t); printf (" swap7 : u = %d, t = %d\n", u, t); return EXIT_SUCCESS ; Functions swap1 - swap3 have no warning or error from gcc. What are the outputs after running these three functions? Remember to write the values of both u and t (0.5 points for each pair of answers) swap1: u =, t = swap2: u =, t = swap3: u =, t = Functions swap4 - swap7 have warnings or errors from gcc. The warnings or errors may be inside the functions or when the functions are called in main. Write down the line numbers and the reasons. You can mark on the lines and explain why these lines cause warnings or errors. There are more than three problems but you need to write only three. (0.5 points for each answer that includes both the line number and the reason) 13