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

Similar documents
EECE.2160: ECE Application Programming Fall 2017

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

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

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

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

EECE.2160: ECE Application Programming Spring 2018

16.216: ECE Application Programming Fall 2013

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

EECE.2160: ECE Application Programming Fall 2016 Exam 1 Solution

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

EECE.2160: ECE Application Programming Spring 2017 Exam 1 Solution

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

16.216: ECE Application Programming Fall 2011

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

16.216: ECE Application Programming Fall 2011

EECE.2160: ECE Application Programming Fall 2017

EECE.2160: ECE Application Programming Spring 2018 Exam 3 May 10, 2018

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

16.216: ECE Application Programming Fall 2015 Exam 2 Solution

Q1: Multiple choice / 20 Q2: Structures / 40 Q3: Strings / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

EECE.2160: ECE Application Programming Fall 2017

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

1. The keyword main in C language is used for

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Chapter 11 Introduction to Programming in C

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Programming Language A

Fundamental of Programming (C)

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

ECE 15 Fall 15 Midterm Solutions

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

EECE.2160: ECE Application Programming Fall 2016 Exam 3 Solution

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

CpSc 1111 Lab 4 Formatting and Flow Control

Chapter 11 Introduction to Programming in C

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Data types, variables, constants

Formatted Input/Output

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

Goals of this Lecture

Chapter 11 Introduction to Programming in C

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

Structures in C. C allows you to declare a struct. Once you do so, you can create variables of this type. Here is the general syntax:

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

16.216: ECE Application Programming Spring 2015 Exam 3 Solution

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

C: How to Program. Week /Mar/05

Structured programming. Exercises 3

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

More examples for Control statements

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

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

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

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Midterm Exam

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Fundamentals of Programming Session 15

Chapter 11 Introduction to Programming in C

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

BSM540 Basics of C Language

Pointers and scanf() Steven R. Bagley

EECE.2160: ECE Application Programming Spring 2018 Exam 3 Solution

ECE15: Lab #4. Problem 1. University of California San Diego

Fundamentals of Programming Session 8

CC112 Structured Programming

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

16.317: Microprocessor Systems Design I Fall Exam 3 December 15, Name: ID #:

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

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

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

CSCI 2132 Software Development. Lecture 8: Introduction to C

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Welcome! COMP s1. Programming Fundamentals

ESC101N: Fundamentals of Computing End-sem st semester

Eastern Mediterranean University Department of Computer Engineering

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

Chapter 3: Arrays and More C Functionality

Computer Science Foundation Exam

Programming for Electrical and Computer Engineers. Loops

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

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

Name Roll No. Section

CpSc 1111 Lab 5 Formatting and Flow Control

CS 101, Spring 2016 March 22nd Exam 2

Lecture 3. More About C

Variables and literals

Transcription:

EECE.2160: ECE Application Programming Fall 2017 Exam 1 October 4, 2017 Name: Lecture time (circle 1): 8-8:50 (Sec. 201) 12-12:50 (Sec. 203) 1-1:50 (Sec. 202) For this exam, you may use only one 8.5 x 11 double-sided page of notes. All electronic devices (e.g., calculators, cell phones) are prohibited. If you have a cell phone, please turn off your ringer prior to the start of the exam to avoid distracting other students. The exam contains 3 questions for a total of 100 points, plus a 10 point extra credit question. Please answer the questions in the spaces provided. If you need additional space, use the back of the page on which the question is written and clearly indicate that you have done so. Please read each question carefully before you answer. In particular, note that: Questions 1c and 2b require you to complete short programs. We have provided comments to describe what each program should do and written some of the code. o Note that each program contains both lines that are partially written (for example, a printf() call missing the format string and expressions) and blank spaces in which you must write additional code. You must write all code required to make each program work as described do not simply fill in the blank lines. o Each program is accompanied by one or more test cases. Each test case is an example of how the program should behave in one specific case it does not cover all possible results of running that program. o You can solve each of these questions using only the variables that have been declared, but you may declare and use other variables if you want. To ensure you do not ignore the main part of the exam in favor of the extra credit question, you cannot earn any extra credit without writing at least partial solutions for all parts of Questions 1, 2, and 3. In other words, don t try the extra credit question until you ve attempted to solve every other question on the exam. You will have 50 minutes to complete this exam. Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10 1

1. (46 points) C input/output; operators a. (13 points) Show the output of the short program below exactly as it will appear on the screen. Be sure to clearly indicate spaces between characters when necessary. You may use the available space to show your work as well as the output; just be sure to clearly mark where you show the output so that I can easily recognize your final answer. int main () { double d1, d2, d3; int x = 4; d1 = x % 3 / 4.0; d2 = d1 * (x + 3); d3 = d2 - d1 * 2; x = d2 + d3; printf("%lf ", d1); printf("%.3lf\n", d2); printf("%.1lf ", d3); // Print d3 with precision of 1 printf("%d\n", x); return 0; 2

1 (continued) b. (13 points) For this program, assume the user inputs the line below. The digit '2' in 2160 is the first character the user types. There is one space (' ') between 2160 and 8.00, and there is one space between 8.00 and -10.20. You must determine how scanf() handles this input and then print the appropriate results, exactly as they would be shown on the screen. The program may not read all characters on the input line, but scanf() will read something into all seven variables declared in the program. 2160 8.00-10.20 int main () { int i1, i2; double d1, d2; char ch1, ch2, ch3; scanf("%c %d%d%lf %c %c%lf", &ch1, &i1, &i2, &d1, &ch2, &ch3, &d2); printf("%d %d\n", i1, i2); printf("%.2lf %.2lf\n", d1, d2); printf("%c%c%c\n", ch1, ch2, ch3); return 0; 3

1 (continued) c. (20 points) Complete this program, which reads the dimensions of a rectangular prism (a box with 6 rectangular sides, as shown on the right) with length L, width W, and height H, reprints the dimensions, and calculates and prints the volume and surface area of the prism. Note that: The volume of a rectangular prism is the product of all three dimensions. The surface area is the sum of the areas of all 6 sides. The formatting of your output should exactly match the formatting of the two test cases below, in which user input is underlined: Enter L, W, H: 2 3 4 Length: 2.00 Width: 3.00 Height: 4.00 Volume: 24.0000 Surface area: 52.0000 Enter L, W, H: 1.2 3.4 5.6 Length: 1.20 Width: 3.40 Height: 5.60 Volume: 22.8480 Surface area: 59.6800 int main() { double L, W, H; // Length, width, and height of prism // Prompt for and read dimensions printf("enter L, W, H: "); scanf(" ", ); // Reprint dimensions printf("length: \n", ); printf("width: \n", ); printf("height: \n", ); // Calculate and print volume and surface area printf("volume: \n", ); printf("surface area: \n", return 0; ); 4

2. (34 points) Conditional statements a. (14 points) For the short program shown below, the first line of output (the prompt "Enter values: ") and the user input (3 1) is listed at the bottom of the page. Complete the rest of the output for this program, given those input values. int main() { int in1, in2; printf("enter values: "); scanf("%d %d", &in1, &in2); switch (in1 + in2) { case 0: if (in1 > in2) printf("%d > %d\n", in1, in2); else printf("%d <= %d\n", in1, in2); break; case 2: case 4: case 6: if (in1 % 2 == 1) printf("%d is odd\n", in1); if (in2 % 2 == 0) printf("%d is even\n", in2); else printf("%d is odder still\n", in2); default: printf("%d isn't 4, is it?\n", in1 + in2); return 0; OUTPUT (the first line is given; write the remaining line(s)): Enter values: 3 1 5

2 (continued) b. (20 points) Complete this program, which implements a simple version of the game Yahtzee using only 3 dice. Your program should prompt for and read the 3 dice values, then check for one of the conditions below. Assume dice are entered in order from lowest to highest value. If all three dice match, print "3 of a kind". If only two of the three dice match, print "2 of a kind". If all three dice hold consecutive values (i.e., 3 4 5 or 1 2 3), print "Straight". In all other cases, print "Chance". Three test cases are shown below, with user input underlined. Enter 3 dice: 4 4 6 2 of a kind Enter 3 dice: 4 5 6 Straight Enter 3 dice: 1 3 5 Chance void main() { int d1, d2, d3; // Values of 3 dice // Prompt for and read dice values printf("enter 3 dice: "); scanf(" ", ); // Check for possible results printf("3 of a kind\n"); // All 3 dice match printf("2 of a kind\n"); // 2 out of 3 dice match printf("straight\n"); // All 3 dice are consecutive printf("chance\n"); // All other cases 6

3. (20 points, 5 points each) While and do-while loops For each of the multiple choice questions below, clearly indicate your response by circling or underlining the one choice you think best answers the question. a. What is the output of the short code sequence below? int i = 8; int j = -1; while (j < i) { i = i - 2; printf("%d %d ", i, j); j = j + 1; i. 8 0 6 1 4 2 ii. 8-1 6 0 4 1 iii. 6 0 4 1 2 2 iv. 6-1 4 0 2 1 v. 8 6 7 5 3 0 9 b. What is the output of the short code sequence below? int i = 17; do { printf("%d ", i); i = i / 2; while (i % 2 == 0); i. 17 ii. 17 8 4 2 iii. 8 4 2 1 iv. 17 8 4 2 1 v. This code sequence produces no output 7

3 (continued) c. Which of the loops below will print the output: 0 1 2? i. int i = 0; while (i < 3) { printf("%d ", i); ii. int j = 2; do { printf("%d ", j); j = j - 1; while (j >= 0); iii. int k = 9; while (k - 1) { printf("%d ", k % 3); k = k / 2; iv. int m = 0; do { printf("%d ", m); m = m + 1; while (m > 2); d. Which of the following statements accurately reflect your opinion(s)? Circle all that apply (but please don t waste too much time on this question )! i. This course is moving too quickly. ii. iii. iv. This course is moving too slowly. I ve attended very few lectures, so I don t really know what the pace of the course is. I hope the next exam is as easy as this question. 8

4. (10 points) EXTRA CREDIT REMEMBER, YOU CANNOT GET EXTRA CREDIT WITHOUT WRITING AT LEAST PARTIAL SOLUTIONS FOR ALL OTHER PROBLEMS ON THE EXAM. Write a short code sequence to read an expression of arbitrary length using integers and + and - operators, and calculate and print the result of the expression. For example, if the user enters: 1+2-3+4+5-6 your code would read that expression and print Result = 3 You may assume: The shortest possible expression is a single number with no + or. So, for example, if the user enters 15, your code simply prints: Result = 15 The only characters the user will input are digits, plus signs ('+'), minus signs ('-') and a single newline character ('\n'), which indicates the end of the expression. No other characters including spaces will be entered, so don t account for them. The user always enters a valid expression, so don t account for any error cases. If you would like additional space to write your solution, use the next page. 9

4 (continued) EXTRA SPACE TO SOLVE EXTRA CREDIT PROBLEM 10