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

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

EECE.2160: ECE Application Programming Spring 2018

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 2013

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

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

EECE.2160: ECE Application Programming Fall 2017

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

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: 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 2011

16.216: ECE Application Programming Fall 2011

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

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

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

16.216: ECE Application Programming Fall 2015 Exam 2 Solution

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

EECE.2160: ECE Application Programming Fall 2017

EECE.2160: ECE Application Programming Fall 2017

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

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

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

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

Programming Language A

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

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

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

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

Data types, variables, constants

Name Roll No. Section

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

(b) Vsource R2 Vsource

C: How to Program. Week /Mar/05

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

16.216: ECE Application Programming Spring 2015 Exam 3 Solution

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

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

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

More examples for Control statements

Fundamental of Programming (C)

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

n Group of statements that are executed repeatedly while some condition remains true

CS 101, Spring 2016 March 22nd Exam 2

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

ESC 101N: Fundmentals of Computing ( IInd Semester) Mid Sem II Examination PM, Monday 7th March, 2011

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

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

COP 3223 Section 4 Exam #2 Form A Multiple Choice Fall /1/2014

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

Control Structure: Selection

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

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

1. The keyword main in C language is used for

Flow Control. CSC215 Lecture

Chapter 2 - Introduction to C Programming

Lecture 3. More About C

Introduction to Programming

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Structured programming

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

2. Numbers In, Numbers Out

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

80 Minutes CENG 230 MidtermExam :40

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

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

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Fundamentals of Programming

Worksheet 4 Basic Input functions and Mathematical Operators

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Problem # 1. calculate the grade. Allow a student the next grade if he/she needs only 0.5 marks to obtain the next grade. Use else-if construction.

Structured programming

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

ECE 15 Fall 15 Midterm Solutions

Lecture 6. Statements

Welcome! COMP s1. Programming Fundamentals

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

CS 101, Spring 2014 April 1st Exam 2 Question 1. [3 points] What output is printed by the following code?

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

COP 3223 Section 4 Exam #2 Form A Multiple Choice Spring /4/2015

Operators and expressions. (precedence and associability of operators, type conversions).

Programming for Engineers Introduction to C

Lecture 02 C FUNDAMENTALS

CS 101, Spring 2017 Apr 11th Exam 3

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

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

Fundamental of Programming (C)

2. Numbers In, Numbers Out

Fundamentals of Programming

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

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

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

Programming Language B

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Write code that simulates one roll of a 6-sided die and prints out the

Transcription:

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution 1. (20 points, 5 points per part) Multiple choice 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. Determine the output of the short code sequence below, given the following user input: C+ scanf ("%c", &grade); switch (grade) { case 'A': case 'a': printf("a"); break; case 'B': case 'b': printf("b"); case 'C': case 'c': printf("c"); case '+': printf("+"); break; default: printf("not Applicable"); i. A ii. iii. iv. B C C+ (The lack of a break after printf("c") means the program proceeds to the next case and prints "+" as well) v. Not Applicable 1

1 (continued) b. What is the output of the short code sequence below? int i = 0; int j = 6; while ((i * j) < 15) { i = i + 2; j = j 1; printf("%d ", i); i. 0 ii. 0 2 iii. 0 2 4 iv. 2 4 v. 2 2

1 (continued) c. Which of the following code sequences will print "Bingo" if integer x is equal to -1? A. if ( (x >= -7) && (x < 1) (x > 12) ) printf ("Bingo"); B. if (!((x >= 2) (x <= -5)) ) printf ("Bingo"); C. if ( (x >= 0) && (x <= 10) ); (The semicolon means printf() printf ("Bingo"); is always executed) D. if (x = 0) Assigns 0 to x and makes printf ("Miss"); condition false else if (x) Since x is 0, condition is printf ("Again"); treated as false else printf ("Bingo"); i. Only B ii. iii. iv. A and B A, B, and C A, B, and D v. A, B, C, and D 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 rest of the exam is as easy as this question. All choices are correct. 3

2. (40 points) C input/output; operators For each short program shown below, list the output 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 we can easily recognize your final answer. a. (12 points) int main() { int v1; double d1, d2; int v2 = 3; double d3 = 14.75; d1 = v2 / 6; d1 = 3 / 6 = 0.0 (int division) v1 = d1 * 7 + 12; v1 = 0 * 7 + 12 = 12 d2 = (v2 + 6) % (v2-2); d2 = (3 + 6) % (3 2) = 9 % 1 = 0.0 v2 = d3 + 2; v2 = 14.75 + 2 = 16.75 = 16 (truncate to int) printf("%d, %d", v1, v2); printf("/n%lf", d1); printf("\noclass\n%lf", d2); return 0; OUTPUT: 12, 16/n0.000000 ß Note: one space between, and 16 oclass 0.000000 4

2 (continued) b. (14 points) int main() { int i1 = 15; float f1, f2; double d1, d2; f1 = 3 + i1 % 4; f1 = 3 + 15 % 4 = 3 + 3 = 6.0 f2 = f1 / (- 3.0); f2 = 6.0 / -3.0 = -2.0 d1 = f1 + f2 + 0.987654; d1 = 6.0 + (-2.0) + 0.987654 = 4.987654 i1 = -i1; i1 = -15 d2 = -i1-10.72; d2 = -(-15) 10.72 = 15 10.72 = 4.28 printf ("%d\n", i1); printf ("%.2lf, %.0lf\n", f1, f2); printf ("%.0lf\n", d1); printf ("%.0lf\n", d2); return 0; OUTPUT: -15 6.00, -2 ß One space after comma 5 ß d1 and d2 both rounded to nearest integer when 4 printed 5

2 (continued) c. (14 points) For this program, assume the user inputs the line below. The '-' sign is the first character the user types. Each pair of numbers in the list "-17.17", "-2.35", and "510.99" is separated by a single space character (' '), while each pair of numbers in the list "510.99", "10.1234", and "5" is separated by two space characters. You must determine how scanf() handles this input and then print the appropriate results. Note that the program may not read all characters on the input line. -17.17-2.35 510.99 10.1234 5 int main() { int i1, i2; double d1, d2, d3; char ch1, ch2; scanf("%lf %d%lf %c%lf %c %d", &d1, &i1, &d2, &ch1, &d3, &ch2, &i2); printf("%d %d\n", i1, i2); printf("%.2lf %.3lf %.2lf\n", d1, d2, d3); printf("%c\n%c\n", ch1, ch2); return 0; Solution: d1 = -17.17 (The 1 st real number) i1 = -2 (only integer part is read) d2 = 0.35 (read in.35) ch1 = 5 (the first character after space is 5 ) d3 = 10.99 (the decimal number after 5 is 10.99) ch2 = 1 (the first character after space is 1 ) i2 = 0 (the first integer before decimal point is 0) OUTPUT: -2 0-17.17 0.350 10.99 5 1 6

3. (40 points, 20 per part) C input/output; conditional statements For each part of this problem, you are given a short program to complete. CHOOSE ANY TWO OF THE THREE PARTS and fill in the spaces provided with appropriate code. You may complete all three parts for up to 10 points of extra credit, but must clearly indicate which part is the extra one I will assume it is part (c) if you mark none of them. Remember, you must write all code required to make each program work as described you cannot simply fill in the blank lines and get full credit. Also, remember that each example only applies to one specific case it does not cover all possible results for that program. a. Translate the flowchart you were given (on a separate sheet) into a complete C program. (Hint: you have to combine some logic in order to match the provided program structure.) Three sample program runs are shown below (user input underlined): Enter 2 ints: -5 3 x = 1 Enter 2 ints: -2 1 x = -1 Enter 2 ints: 0 12 x = 0 Required code is in italic, underlined, bold font. void main() { int in1, in2; int x; // Input variables // Value to be modified // Initialize variables as needed x = 0; // Prompt for and read inputs printf("enter 2 ints: "); scanf("%d %d", &in1, &in2); // Test input values and perform appropriate operation on x if (in1 < -3 in1 > 0) x = x + 1; else if (in2 <= 1 && in2 >= -1) x = x - 1; else x = 0; // Print value of x as formatted in test cases printf("x = %d\n", x); 7

3 (continued) b. This program should read a time in standard form and convert it to military time. Note that: Standard form is hours:minutes, followed by the letter A or P for AM or PM. Military time is a 4-digit number between 0000 (12:00 AM) and 2359 (11:59 PM) o Times from 0000 to 1159 are in the morning (AM); 1200 to 2359 are afternoon or evening (PM) o Printing a 4-digit integer with leading 0s can be done with a precision of 4, as shown in the problem. (In other words, the output formatting is done for you.) If the user enters anything after the time besides 'A', 'a', 'P', or 'p', print an error message. Two sample program runs are shown below (user input underlined): Enter HH:MM <A or P>: 2:25 A Enter HH:MM <A or P>: 8:16 p Mil. time: 0225 Mil. time: 2016 Required code is in italic, underlined, bold font. void main() { int hr, min; char ap; char ch; // Hours and minutes in standard form // Tracks AM or PM // Used to read : between hours & minutes // Prompt for and read time printf("enter HH:MM <A or P>: "); scanf("%d%c%d %c", &hr, &ch, &min, &ap); if (hr == 12) hr = 0; switch (ap) { case 'A': case 'a': printf("mil. time: %.4d\n", hr * 100 + min); break; case 'P': case 'p': printf("mil. time: %.4d\n", (hr + 12) * 100 + min); break; default: printf("invalid character\n"); 8

3 (continued) c. This program prompts the user to enter two numbers representing the endpoints of a range, followed by a value to be tested. The program should check to see if the value falls within the range (including the endpoints), below the lowest endpoint, or above the highest endpoint, and print the appropriate relationship between the values, as shown in the test cases below (user input is underlined). Note: all output values should be printed with 2 decimal places. Endpoints: 5 10 Value: 7 5.00 <= 7.00 <= 10.00 Endpoints: -1 1 Value: 1-1.00 <= 1.00 <= 1.00 Endpoints: -1.2 3.4 Value: -5.6-5.60 < -1.20 Endpoints: 2.999 3.456 Value: 4.567 4.57 > 3.46 Required code is in italic, underlined, bold font. int main() { double lo, hi; double val; // Range endpoints // Value // Prompt for and read endpoints printf("endpoints: "); scanf("%lf %lf", &lo, &hi); // Prompt for and read value to be tested printf("value: "); scanf("%lf", &val); // Test each of the three cases (in range, too low, // too high) and print appropriate output if ((lo <= val) && (val <= hi)) printf("%.2lf <= %.2lf <= %.2lf\n", lo, val, hi); else if (val < lo) printf("%.2lf < %.2lf\n", val, lo); else printf("%.2lf > %.2lf\n", val, hi); return 0; 9