CSE 142 Wi01 Midterm 2 page 1 of 6

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions)

CSE413 Midterm. Question Max Points Total 100

CS 108 Computing Fundamentals. October/November Array Bootcamp

CSE 142 Sample Midterm Exam #3

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

CSE 142 Sample Midterm Exam #3

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Principles of Programming. Chapter 6: Arrays

What are the most likely declarations of "month" in the old and new versions of the program?

Decision Making and Loops

CSE 143 Sample Midterm Exam #1

Midterm Review. Short Answer. Short Answer. Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions.

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

COS 126 General Computer Science Spring Midterm 1

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Answer all questions. Write your answers only in the space provided. Full marks = 50

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

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

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

CC112 Structured Programming

Programming Language A

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

(a) Assume that in a certain country, tax is payable at the following rates:

TEST BDA24202 / BTI10202 COMPUTER PROGRAMMING May 2013

CSE 142 Su01 Midterm 1 Sample Solution page 1 of 5

LAB 6 FUNCTIONS I School of Computer and Communication Engineering

Basic and Practice in Programming Lab7

CS 139 Practice Midterm Questions #2

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

DECLARAING AND INITIALIZING POINTERS

Name CIS 201 Midterm II: Chapters 1-8

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSE 143. Lecture 9: introduction to recursion reading: 12.1

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Lecture 19: Recursion

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

More Arrays. Last updated 2/6/19

CS240: Programming in C

CSE 143, Winter 2009 Sample Midterm Exam #2

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

CSE 1223: Exam II Autumn 2016

Lesson 7. Reading and Writing a.k.a. Input and Output

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CSE 142 Sample Midterm Exam #2

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

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

EECE.2160: ECE Application Programming Fall 2017

CSE 143 Sample Midterm Exam #7 (11wi)

Dept. of CSE, IIT KGP

Assoc. Prof. Dr. Tansu FİLİK

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

ECE 190 Midterm Exam 3 Spring 2011

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

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

C introduction: part 1

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

User Defined Functions

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

CSE 142, Autumn 2007 Midterm Exam, Friday, November 2, 2007

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS 0449 Sample Midterm

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Programming for Electrical and Computer Engineers. Pointers and Arrays

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CSCI 2132 Software Development. Lecture 18: Functions

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Practice problems Set 2

CSCI 101 Midterm Sample Questions

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Recursion continued. Programming using server Covers material done in Recitation. Part 2 Friday 8am to 4pm in CS110 lab

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

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

CSE 143 Sample Midterm Exam #8 (12wi)

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

CSE 303 Midterm Exam

CSE 143 Lecture 10. Recursion

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Programming Language B

Example: Pointer Basics

Suppose that the following is from a correct C++ program:

Assembly Programming (III)

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

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

CS , Fall 2001 Exam 2

CS106B Sprint 2017 April 27 th, 2017 CS106B Practice Midterm (KEY)

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

CSE / ENGR 142 Programming I

Transcription:

CSE 142 Wi01 Midterm 2 page 1 of 6 Answer all of the following questions. READ EACH QUESTION CAREFULLY. Answer each question in the space provided on these pages. Keep your answers short and to the point. There are 6 questions worth a total of 45 points. Good luck. 1. (3 points) Suppose we have two variables in a C program declared like this: int list[42]; int k; Complete the following loop so it stores 0 in all 42 elements of the array list. for (k = 0 ; k < 42 ; k++) { list[k] = 0; 2. (6 points) Rewrite the following as equivalent conditions that do not use the not (! ) operator. (The solutions can, of course, use the!= operator as needed.) a)!(a==2 && b==3) (a!=2 b!=3) b)!((a==b && b>10)!(a!=0 c<=3)) (a!=b b<=10) && (a!=0 c<=3)

CSE 142 Wi01 Midterm 2 page 2 of 6 3. (10 points) The input for this problem consists of a sequence of zero or more positive integers followed by a 0 that indicates the end of the input. Write a function longestsequence that reads the input and returns the length of the longest consecutive sequence of any single integer. For example, if the input were 4 35 35 35 35 12 9 9 35 5 0 the return value should be 4, because there are 4 consecutive occurrences of 35 in the input, and no number has a longer sequence. If the input were 12 15 3 9 0 the result should be 1. If there are no input numbers (i.e., the first thing read is the 0 that marks the end of the input), the function should return 0. YoushouldcallafunctionnamedreadInt to read the integers. Assume that it returns the next integer from the input sequence if there are no input errors, and otherwise detects the input error and terminates execution if one is found. Its prototype is: int readint(void); /* return the length of the longest consecutive sequence */ /* of identical numbers in the input. */ int longestsequence(void) { int longestrunsofar; int currentrun; int lastnum; int nextnum; lastnum = 0; longestrunsofar = 0; currentrun = 0; do { nextnum = readint(); if (nextnum==lastnum && nextnum!=0) { currentrun++; else { if (currentrun > longestrunsofar) { longestrunsofar = currentrun; currentrun = 1; lastnum = nextnum; while (lastnum!= 0); return longestrunsofar;

CSE 142 Wi01 Midterm 2 page 3 of 6 Second solution using the input strategy talked about in the 9:30 lecture. /* return the length of the longest consecutive sequence */ /* of identical numbers in the input. */ int longestsequence(void) { int longestrunsofar; /* length of longest run seen so far*/ int currentrun; /* length of current run */ int lastnum; /* previous # read from input */ /* (0 if no numbers read so far) */ int currentnum; /* current # read from input */ lastnum = 0; longestrunsofar = 0; currentrun = 0; currentnum = readint(); while (currentnum!= 0) { if (currentnum == lastnum) { currentrun++; else { if (currentrun > longestrunsofar) { longestrunsofar = currentrun; currentrun = 1; lastnum = currentnum; currentnum = readint(); return longestrunsofar;

CSE 142 Wi01 Midterm 2 page 4 of 6 4. (8 points) Write a function named printtriangle that prints a right triangle with straight edges on the bottom and on the right. This function should have a single integer parameter that specifies the height (number of rows) and width (length of the last row) of the triangle. Examples: printtriangle(4) * ** *** **** printtriangle(1) * void printtriangle (int size) { int row, col; for (row = 1; row <= size; row++) { col = 1; while (col <= size-row) { printf( ); col++; while (col <= size) { printf( * ); col++; printf( \n ); or, if you prefer for loops, void printtriangle (int size) { int row, col; for (row = 1; row <= size; row++) { for (col = 1; col <= size-row; col++) { printf( ); for (col = size-row+1; col <= size; col++) { printf( * ); printf( \n );

CSE 142 Wi01 Midterm 2 page 5 of 6 5. (8 points) Trace the execution of this program and do the following: a) Draw a diagram (boxes and arrows) showing the situation right before execution of the printf in function riddle. Your diagram should contain a box for each function showing its parameters and local variables, the values of all simple integer variables, and arrows for each pointer variable showing what it points to. b) Write down the output produced by the program. #include <stdio.h> int riddle(int a, int *b) { a = a + 2; *b = *b + a; /* diagram should show boxes and */ /* arrows when execution reaches */ /* this point */ printf("%d %d\n", a, *b); return *b; int mystery(int a, int b, int *c) { b = riddle(a, c); printf("%d %d %d\n", a, b, *c); return a; int main(void) { int a, b, c; a = 0; b = 1; c = 3; b = mystery(a, b, &c); printf("%d %d %d\n", a, b, c); 2 5 0 5 5 0 0 5 riddle a 0 2 b mystery a 0 b 1 c main a 0 b 1 c 3 5

CSE 142 Wi01 Midterm 2 page 6 of 6 6. (10 points) Assume that a herd of rabbits grows as follows: 1) 1/2 of the herd (rounded down) is always female, no matter how many rabbits there are. 2) Every month, each female produces 5 new rabbits. 3) Every month, 10% of the herd dies (but only after the new rabbits have been born) Write a function monthstotakeoverus that has one integer parameter giving the initial size of the herd. The function should return the number of months needed for the rabbit population to become greater than the population of the US. Assume for this problem that the US population is 280,000,000 and does not change. Also, assume that the initial size of the herd is always at least 2 (i.e. there is always at least 1 female rabbit). Finally, remember that the number of rabbits is always an integer (there are no fractional rabbits). int monthstotakeoverus(int initialpop) { int months = 0; int rabbitpop = initialpop; while (rabbitpop <= 280000000) { months = months+1; rabbitpop = (rabbitpop + (rabbitpop/2)*5) * 0.9; return months;