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

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

EECE.2160: ECE Application Programming Fall 2017

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

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 2 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

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

EECE.2160: ECE Application Programming Spring 2018

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

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

EECE.2160: ECE Application Programming Fall 2017

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

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

16.216: ECE Application Programming Spring 2015 Exam 3 Solution

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

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

CS 101, Spring 2016 March 22nd Exam 2

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

16.216: ECE Application Programming Fall 2013

EECE.2160: ECE Application Programming Fall 2017

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

16.216: ECE Application Programming Fall 2011

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

Functions. Arash Rafiey. September 26, 2017

Principles of C and Memory Management

Question 2. [2 points] True False By default, structures are passed-by-reference.

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

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

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

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

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Fundamentals of Computer Programming Using C

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

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

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

APCS Semester #1 Final Exam Practice Problems

Write a C program using arrays and structure

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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):

Other Loop Options EXAMPLE

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

ECE264 Fall 2013 Exam 3, November 20, 2013

ECE264 Spring 2013 Final Exam, April 30, 2013

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

Introduction to Computer Science Midterm 3 Fall, Points

CS 115 Exam 3, Spring 2010

Question 2. [5 points] Given the following symbolic constant definition

Functions. Lecture 6 COP 3014 Spring February 11, 2018

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

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

Department of Electrical Engineering and Computer Sciences Spring 2007 Instructor: Dr. Dan Garcia

ESC101N: Fundamentals of Computing End-sem st semester

2015 Mathworks Math Contest. Mathworks Math Contest For Middle School Students November 10, 2015

Essar Placement Paper

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

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Spring 2013 COMP Midterm Exam Solutions March 07, 2013

Computer Science Foundation Exam

CSE Fall 2015 Section 002 Exam 2, Time: 80 mins

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

C Characters and Strings

ECE 15 Fall 15 Final Solutions

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

EECE.2160: ECE Application Programming Spring 2018 Programming Assignment #6: Using Arrays to Count Letters in Text Due Wednesday, 4/4/18, 11:59:59 PM

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Computer Science Foundation Exam

PROGRAMMING IN C AND C++:

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

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

CGS 3460 Summer 07 Midterm Exam

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Excel Basics Fall 2016

CS 1713 Introduction to Programming II

CSE 333 Midterm Exam 7/29/13

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

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

Final Exam. COMP Summer I June 26, points

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

ECE264 Fall 2013 Exam 1, September 24, 2013

Two Dimensional Array - An array with a multiple indexs.

Computer Science Foundation Exam

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Computer Science Foundation Exam

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

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Page 1 Name: CUNYfirstID: CS111 Summer Term 2 - Final 6/26/18

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

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

UNIVERSITY REGULATIONS

Make sure the version number is marked on your scantron sheet. This is Version 1

Transcription:

EECE.2160: ECE Application Programming Spring 2017 Exam 2 March 29, 2017 Name: Section (circle 1): 201 (Dr. Li, MWF 8-8:50) 202 (Dr. Geiger, MWF 12-12:50) 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 it off prior to the start of the exam to avoid distracting other students. The exam contains 3 questions for a total of 100 points. 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: Question 3 has three parts, but you are only required to complete two of the three parts. o You may complete all three parts for up to 10 points of extra credit. If you do so, please clearly indicate which part is the extra one we will assume it is part (c) if you mark none of them. For each part of Question 3, you must complete a short function. We have provided comments to describe what your function should do and written some of the code for you. o Note that each function 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 function work as described do not simply fill in the blank lines. o Each test case is an example of how the function should behave in one specific case it does not cover all possible results of running that function. You can solve each part of Question 3 using only the variables that have been declared, but you may declare and use other variables if you want. You will have 50 minutes to complete this exam. Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10 1

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. What is the output of the short code sequence below? char s1[] = "Exam2"; char s2[15]; int i; strncpy(s2, s1, 10); printf("%s", s2); for(i = 0; i < 10; i++) if(s2[i] >= '0' && s2[i] <= '9') s2[i]++; strncat(s2, s1, 4); printf("%s", s2); i. Exam2Exam3 ii. iii. iv. Exam1Exam2 Exam2Exam3Exam3 Exam2Exam3Exam2 v. Exam2Exam3Exam 2

1 (continued) b. What is the output of the short code sequence below? Remember the following function used in Program 6: int toupper(int ch): If ch is a lowercase letter, this function returns the uppercase version. Otherwise, toupper() returns the original value of ch. For example, toupper('q') returns 'Q'; toupper('a') returns 'A'. char s1[] = "ABCD"; char s2[] = "abcd"; int i; for(i = 0; i < strlen(s1) 1; i++) s2[i] = toupper(s2[i]); printf("%s ", s2); if( strncmp (s1, s2, 3) == 0 ) printf("s1 == s2"); else printf("s1!= s2"); i. ABCD s1 == s2 ii. iii. iv. ABCD s1!= s2 ABCd s1 == s2 ABCd s1!= s2 v. abcd s1!= s2 3

1 (continued) c. You are given the structure definition below: typedef struct { char name[10]; int id; double salary; Employee; Your program declares a single Employee variable: Employee e1; Which of the following groups of statements will compile correctly and set the salary field inside e1 to be 75000? A. e1.salary = 75000; B. Employee *ep = &e1; ep.salary = 75000; C. Employee.salary.e1 = 75000; D. Employee *ep = &e1; ep->salary = 75000; i. Only A ii. iii. iv. Only B A and D B and C v. A, C, and D 4

1 (continued) 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. I think the most recent programming assignments are still pretty easy. ii. iii. iv. I think the programming assignments have gotten to be too difficult. I think the programming assignments have gotten harder, but are still fair. Is the semester over yet? 5

2. (40 points) Arrays 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 assume all appropriate libraries are included. 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. a. (12 points) int main() { int arr[7] = {2, 4, 1; int i; for (i = 1; i < 6; i++) { arr[i] = arr[i-1] + arr[i]; printf("%d ", arr[i]); printf("\n"); for (i = 0; i < 7; i += 2) { arr[i] = arr[i%3] + arr[i/2]; printf("%d ", arr[i]); printf("\n"); return 0; 6

2 (continued) b. (13 points) int main() { int i, j; int arr[4][2] = {{7, 5, {4, 3, {1, 9, {12, 2; for (j = 0; j < 2; j++) { for (i = 0; i < 4; i++) { arr[i][j] = arr[i][j] / 2; printf("%d ", arr[i][j]); printf("\n"); j = 0; for (i = 0; i < 4; i++) { arr[i][j]++; for (j = 0; j < 2; j++) { if (arr[i][j] % 2) arr[i][j]++; printf("%d ", arr[i][j]); return 0; 7

2 (continued) c. (14 points) void f(double arr[], int n, int step) { double temp; int i; if (n <= step) { printf("array not changed.\n"); return; else { for (i = 0; i < n step; i++) { temp = arr[i]; arr[i] = arr[i + step]; arr[i + step] = temp; for (i = 0; i < n; i ++) { printf("%0.lf ", arr[i]); printf ("\n"); int main () { double myarr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10; f(myarr, 10, 2); f(myarr, 4, 5); f(myarr, 6, 3); return 0; 8

3. (40 points, 20 per part) Functions For each part of this problem, you are given a short function 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 function work as described do not assume you can simply fill in the blank lines and get full credit. Also, remember that each example provided is only applicable in one specific case it does not cover all possible results of using that function. In order to allow plenty of space to solve each problem, this page is essentially just a cover sheet for Question 3 the actual problems start on the next page. 9

3 (continued) a. void printtriangle(int len); This function prints a right triangle with two equal sides of length len. The outer edge of the triangle is represented by a series of * characters, and the inner part of the box contains only spaces. The triangle should be printed so that the hypotenuse (the side opposite the right angle) goes from the top right corner to the bottom left corner. For example, printtriangle(4) would print the following: * (three spaces followed by a star) ** (two spaces followed by two stars) * * (one space, one star, one space, one star) **** (no spaces, four stars) void printtriangle(int len) { int i, j, k; // Loop indexes (you may not need all 3) // Outer loop controls "rows" of output--make sure each "row" // starts on a new line ( ) { // Inner loop controls "columns" of output ( ) // Test to see if current column is on border of triangle // If so, print star ( printf("*"); ) // Otherwise, print space printf(" "); // End inner loop // End outer loop--remember, each "row" starts on new line 10

3 (continued) b. int isperfect(int num); A perfect number is a positive integer that equals the sum of its positive factors (the numbers that evenly divide into it), excluding the number itself. For example, the lowest perfect number is 6, because the factors of 6 are 1, 2, and 3, and 1 + 2 + 3 = 6. This function takes a single argument, num, and returns 1 if num is a perfect number and 0 otherwise. To determine if a number is perfect, find all of its factors, add them together, and check if the sum of the factors matches the original number. Note: For full credit, your loop that finds all factors should not test any values that cannot possibly be factors of num. Hint: think about the smallest possible factors of any number and what the corresponding largest factors can be. Remember to exclude the original number itself! int isperfect(int num) { int i; // Loop index int sum; // Running total of factors // Initialize variables as needed // Return 0 for any value too small to be a perfect number if ( ) return 0; // Loop to find all factors of num and add them up // Remember, for full credit, this loop shouldn t test any // value that can t possibly be a factor of num ( ) { // If number is perfect, return 1; otherwise, return 0 if ( ) return 1; else return 0; 11

3 (continued) c. void avgsd(double list[], int n, double *avg, double *sd); Given an array, list[], containing n double-precision values, calculate the average and standard deviation of the array and store them at the addresses pointed to by avg and sd, respectively. To calculate the standard deviation of a list of values, follow the steps below, using {7, 5, 3, 1 as an example list: Calculate the average of all values (which is 4 for the example values) Go through the list and add up the squares of the differences between each value and the average (for this example, (7 4) 2 + (5 4) 2 + (3 4) 2 + (1 4) 2 = 9 + 1 + 1 + 9 = 20) Divide by the number of values (for this example, 20 / 4 = 5) The standard deviation is the square root of that result, which you can find using the sqrt() function from the math library (for this example, sqrt(5) 2.236) void avgsd(double list[], int n, double *avg, double *sd) { double sum; // Running total int i; // Loop index // Calculate average for ( ) { // Calculate standard deviation for ( ) { 12