ECE264 Fall 2013 Exam 1, September 24, 2013

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

ECE264 Spring 2013 Exam 1, February 14, 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.

Sample Problems for Quiz # 2

ESC101N: Fundamentals of Computing End-sem st semester

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

CSE 303 Midterm Exam

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

Functions. Arash Rafiey. September 26, 2017

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

ECE368 Exam 2 Spring 2016

Computer Science Foundation Exam

CSE413 Midterm. Question Max Points Total 100

Computer Science Foundation Exam

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

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Arrays and Pointers in C. Alan L. Cox

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

Computer Science Foundation Exam

COS 126 Midterm 1 Written Exam, Fall 2009

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Computer Science Foundation Exam

COS 126 General Computer Science Fall Midterm 1

Introduction to Computing Systems Fall Lab # 3

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

Basic and Practice in Programming Lab7

Programming in C. Pointers and Arrays

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

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

ECE 2400 Computer Systems Programming, Fall 2017 Programming Assignment 3: Sorting Algorithms

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

CSL 201 Data Structures Mid-Semester Exam minutes

EECE.2160: ECE Application Programming Fall 2017

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

CS 0449 Sample Midterm

CSE 5A Final Fall 2006

ECE 15 Fall 15 Final Solutions

Pointers as Arguments

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Question: Total Points: Score:

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CS 223: Data Structures and Programming Techniques. Exam 2

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

C 101. Davide Vernizzi. April 29, 2011

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

Computer Science Foundation Exam

Language comparison. C has pointers. Java has references. C++ has pointers and references

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

ECE 264 Purdue University

Princeton University COS 217: Introduction to Programming Systems Spring 2009 Final Exam Answers

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Procedural programming with C

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Exercise Session 2 Simon Gerber

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Signature: ECE 551 Midterm Exam

PRINCIPLES OF OPERATING SYSTEMS

Computer Science 217 Final Exam May 15, :30pm-4:30pm

Using pointers with functions

C programming for beginners

CS 2301 Exam 3 B-Term 2011

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Computer Science Foundation Exam

ECE551 Midterm Version 1

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

Lecture 04 FUNCTIONS AND ARRAYS

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Question: Total Points: Score:

Two Dimensional Array - An array with a multiple indexs.

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

Dept. of CSE, IIT KGP

CS 241 Data Organization Pointers and Arrays

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Procedural Programming

ALGORITHM 2-1 Solution for Exercise 4

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Memory Corruption 101 From Primitives to Exploit

Computer Science Foundation Exam

Introduction to C Language (M3-R )

CS16 Exam #1 7/17/ Minutes 100 Points total

Solutions to Assessment

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

UNIT III (PART-II) & UNIT IV(PART-I)

Sample Problems for Quiz # 3

CS61, Fall 2012 Section 2 Notes

Exercise Session 2 Systems Programming and Computer Architecture

Arrays and Strings. Arash Rafiey. September 12, 2017

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

C Arrays Pearson Education, Inc. All rights reserved.

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


Transcription:

ECE264 Fall 2013 Exam 1, September 24, 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 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. Some questions have conditions and restrictions. 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. One learning objective (recursion) is tested in this exam. To pass an objective, you must receive 50% or more points in this exam. 1

Contents 1 Recursive Formula (5 points) 3 2 Incorrect Recursive Binary Search (5 points) 5 3 Trace Recursive Function (5 points) 7 4 Reverse Array (5 points) 9 Learning Objective 1 (Recursion) Pass Fail Total Score: 2

1 Recursive Formula (5 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 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 can use this fact and do not have to prove it. In the above examples, when partitioning 1, 3 is not used. when partitioning 2, 3 is not used. when partitioning 3, 3 is used once. when partitioning 4, 3 is used twice. Question: When partitioning value n (n > 4), how many times is 3 used? You must explain your answer. An answer without explanation will receive no point. Additional information is on the next page. This question asks you to write a mathematical formula, not a C program. 3

The answer is not n 2. You can use the following table to validate your formula. If your formula does not match these cases, the formula is definitely wrong. However, matching these cases does not mean your formula is correct. You must have a systematic way to find the formula. Do not try to find a formula to match these values. n 4 5 6 7 8 9 f(n) 2 5 12 28 64 144 4

2 Incorrect Recursive Binary Search (5 points) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Consider the following incorrect implementation of recursive binary search. int binarysearch ( int * arr, int key, int low, int high ) if ( low >= high ) /* ERROR : should be >, not >= */ return -1; int mid = ( low + high ) / 2; if ( arr [ mid ] == key ) return mid ; if ( arr [ mid ] > key ) return binarysearch ( arr, key, low, mid - 1); return binarysearch ( arr, key, mid + 1, high ); The main function is shown below. # include <stdio.h> # include <stdlib.h> int binarysearch ( int * arr, int key, int low, int high ); # define ARRAYSIZE 10 int main ( int argc, char * * argv ) int arr [ ARRAYSIZE ] = 1, 12, 23, 44, 65, 76, 77, 98, 109, 110; int ind ; for ( ind = 0; ind < ARRAYSIZE ; ind ++) printf ("% d\n", binarysearch (arr, arr [ ind ], 0, ARRAYSIZE )); return EXIT_SUCCESS ; 5

One line in binarysearch is incorrect, as marked by a comment. What is the output of this program? Please put your answers in the following table. Hint: You will not receive the full score if your answers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. 6

3 Trace Recursive Function (5 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 Consider the following program. # include <stdio.h> # include <stdlib.h> int f( int n, int * count ) (* count ) ++; /* how many times f is called? */ if ((n == 0) (n == 1)) return 1; int val = 0; int a = f(n - 1, count ); int b = f(n / 2, count ); val += a + b; return val ; int main ( int argc, char * * argv ) int count = 0; int val = 4; int fv = f(val, & count ); printf ("f(%d) = %d, count = %d\n", val, fv, count ); return EXIT_SUCCESS ; What are the values of fv and count printed by this program? get the answers. An answer without explanation will receive no point. Explain how you 7

You need to write two answers. fv count You can write an arithmetic expression without calculating the result. For example, if the answer is 17, you can write 1 + 5 + 11. Hint: Drawing the calling relationships will help you determine how many times the function is called and how the values change. 8

4 Reverse Array (5 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 The following is a recursive function that reverses the elements in an array. Please fill in the necessary code. # include <stdio.h> # include <stdlib.h> /* * The function takes two pointers of integers. * Each pointer stores an address ; an integer is stored at the address. * The function swaps the values stored at the two addresses. */ void swap ( ) /* fill the arguments (0.5 point ) */ /* fill the code (1 point ) */ /* ------------------------------------- */ void reversehelper ( int * arr, int low, int high ) if ( ) /* fill the terminating condition (1 point ) */ /* do not call recursive function again */ return ; /* swap the first and the last elements */ swap ( ); /* fill the arguments (1 point ) */ /* call the function itself to reverse the remaining elements of the array (1 point ) */ 9

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 72 73 74 75 76 77 78 79 80 /* ------------------------------------- */ void reverse ( int * arr, int len ) /* call the helper function */ /* fill the arguments (0.5 point ) */ reversehelper ( ); /* ------------------------------------- */ /* Do not change anything below this line */ void printarray ( int * arr, int len ) int ind ; for ( ind = 0; ind < len ; ind ++) printf ("% d ", arr [ ind ]); printf ("\ n "); # define ARRAYSIZE 16 int main ( int argc, char * * argv ) int arr [ ARRAYSIZE ]; int iter ; for ( iter = 0; iter < ARRAYSIZE ; iter ++) /* initialize the array */ int ind ; for ( ind = 0; ind < ARRAYSIZE ; ind ++) arr [ ind ] = ind ; reverse (arr, iter ); printarray ( arr, iter ); return EXIT_SUCCESS ; 10