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

Similar documents
EECE.2160: ECE Application Programming Fall 2017

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

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

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

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

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 2016 Exam 3 Solution

16.216: ECE Application Programming Spring 2015 Exam 3 Solution

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

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

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 2016 Exam 1 Solution

EECE.2160: ECE Application Programming Fall 2017

EECE.2160: ECE Application Programming Spring 2018

EECE.2160: ECE Application Programming Fall 2017

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

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

16.216: ECE Application Programming Fall 2013

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

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

SOFTWARE Ph.D. Qualifying Exam 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

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

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

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

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

16.216: ECE Application Programming Fall 2011

CSE 333 Midterm Exam 7/29/13

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

CS 1713 Introduction to Programming II

Basic and Practice in Programming Lab7

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

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

ECE264 Fall 2013 Exam 3, November 20, 2013

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

ESC101N: Fundamentals of Computing End-sem st semester

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

ECE264 Fall 2013 Exam 1, September 24, 2013

Computer Science Foundation Exam

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

Principles of C and Memory Management

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

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

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

Computer Science Foundation Exam

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

Welcome! COMP s1. Programming Fundamentals

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

Huawei Test 3. 1 A card is drawn from a pack of 52 cards. The probability of getting a queen of club or a king of heart is:

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Question 1. [15 marks]

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

EE 312 Fall 2017 Midterm 1 October 12, 2017

Programming Language B

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

Standard File Pointers

C programming basics T3-1 -

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CS 101, Spring 2016 March 22nd Exam 2

Lesson #8. Structures Linked Lists Command Line Arguments

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

ECE264 Spring 2013 Final Exam, April 30, 2013

Computer Science Foundation Exam

CS 1713 Introduction to Programming II

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

This exam is worth 24 points, or 24% of your total course grade. The exam contains six

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

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

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

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

Introduction to Computer Science Midterm 3 Fall, Points

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Today s Learning Objectives

ECET 264 C Programming Language with Applications

1. The keyword main in C language is used for

COMP1917 Computing 1 Written Exam Sample Questions

Computer Science Foundation Exam

University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman

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

ECET 264 C Programming Language with Applications

Pointers and File Handling

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Fundamental of Programming (C)

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

EE 472 Embedded Systems. Name solutions. Instructions:

ECE264 Spring 2013 Exam 1, February 14, 2013

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

Transcription:

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 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 4 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 1b and 2b require you to complete short functions. We have provided comments to describe what each function should do and written some of the code. 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 function is accompanied by one or more test cases. 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. 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. For this exam, unlike Exams 1 and 2, you may attempt the extra credit problem even if you have not at least partially completed all other problems on the exam. The extra pages accompanying the exam hold structure and function definitions, as well as test cases for functions you must write info needed for questions 1a, 1b, 2b, and 5. You will have 3 hours to complete this exam. Q1: Structures / 34 Q2: File I/O / 25 Q3: Character & line I/O / 17 Q4: Dynamic allocation & linked lists / 24 TOTAL SCORE / 100 Q5: EXTRA CREDIT / 10

1. (34 points) Structures a. (14 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 I can easily recognize your final answer. The Struct1 structure definition is in the extra document provided with the exam. void f1(struct1 *s1ptr, int a, int b) { s1ptr->v1 = a + b; s1ptr->v2 = a * b; void f2(struct1 *s1ptr) { printf("%d %d\n", s1ptr->v1, s1ptr->v2); int main() { Struct1 x = {5, 3; Struct1 y, z; f1(&y, x.v2, x.v1); f1(&z, y.v2, x.v2); f2(&x); f2(&y); f2(&z); return 0; 2

1 (continued) b. (20 points) Complete the function described below: double semestergpa(gradelist *glp); This function reads a GradeList structure, pointed to by argument glp, and calculates and returns the grade point average, in which each grade is weighted by the number of credits per class. Each grade is represented by a letter (A, B, C, D, F) no + or grades and is worth the following number of points: A = 4 points, B = 3 points, C = 2 points, D = 1 point, F = 0 points. The GradeList structure definition and test cases for this function are in the extra document provided with the exam. double semestergpa(gradelist *glp) { int i; // Loop index double GP; // Total grade points int cred; // Total credits // Initialize variables // Go through all elements of arrays in grade list for ( ) { // Add credits for current class // Add grade points for current class switch ( ) { // Return final weighted GPA return ; 3

2. (25 points) File I/O a. (5 points) A program must read a binary file, f1.bin, which contains a set of integer values, store those values in an array, and print the array contents to the screen. The file has at least 1 value in it and may hold up to 200 values. Which of the choices below will always perform the operations described above correctly, regardless of the file size? Assume the file always opens with no errors. Clearly indicate your response by marking the one choice you think best answers the question. i. int x; FILE *fp = fopen("f1.bin", "rb"); fread(&x, sizeof(int), 1, fp); printf("%d", x); ii. int i; int arr[200]; FILE *fp = fopen("f1.bin", "rb"); fread(arr, sizeof(int), 200, fp); for (i = 0; i < 200; i++) printf("%d\n", arr[i]); iii. int i, n; int arr[200]; FILE *fp = fopen("f1.bin", "rb"); n = fread(arr, sizeof(int), 200, fp); for (i = 0; i < n; i++) printf("%d\n", arr[i]); iv. int i, n; int arr[200]; FILE *fp = fopen("f1.bin", "rb"); n = fread(arr, sizeof(int), 200, fp); for (i = 0; i < n; i++) fprintf(fp, "%d\n", arr[i]); v. int i, n; int arr[200]; FILE *fp = fopen("f1.bin", "rb"); n = fwrite(arr, sizeof(int), 200, fp); for (i = 0; i < n; i++) printf("%d\n", arr[i]); 4

2 (continued) b. (20 points) Complete the function described below: TicTacToe(char *inname, char *outname); This function takes two arguments: strings representing the names of an input (inname) and output file (outname). The input text file represents moves in a game of tic tac toe each of the 9 lines contains a letter ('X' or 'O') and a row and column number in the 3x3 tic tac toe board. The function should read each input line, storing the letter into the row and column specified, and then print the whole 3x3 board to the named output file. You may assume both files always open without errors. Test cases for this function are in the extra pages supplied with the exam. void TicTacToe(char *inname, char *outname) { FILE *infp, *outfp; // Input/output file pointers char board[3][3]; // Tic tac toe board int i, j, k; // Array/loop indexes char XO; // Input letter--'x' or 'O' // Open input file // Read each row from file, then store letter in board for ( ) { fscanf( ); // Open output file // Print board to output file one board row per line of output for ( ) { for ( ) { fprintf( ); fprintf( ); // Close files 5

3. (17 points) Character & line I/O a. (12 points) Show the output of the short program below exactly as it will appear on the screen, assuming the user types the following input at the command line (including a newline character after the last digit (6)): 97-8-216 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 I can easily recognize your final answer. int main() { char c1, c2, c3; while ((c1 = getchar())!= '\n') { c2 = fgetc(stdin); if (c2 == '\n') ungetc(c2, stdin); else if (c2!= '-') { c3 = getchar(); if (c3 == '\n') ungetc(c3, stdin); printf("%c %c %c\n", c3, c2, c1); return 0; 6

3 (continued) b. (5 points) You are given the short program below: int main() { char s1[10]; char s2[10]; scanf("%s", s1); fgets(s2, 10, stdin); printf("%s\n", s1); printf("%s\n", s2); return 0; What will the program print if the user enters the line below, including a newline at the end? You're almost done Clearly indicate your response by marking the one choice you think best answers the question. i. You're almost done ii. iii. iv. You 're almost You're almost done You're almost d v. You'll be done soon 7

4. (24 points) Dynamic allocation & linked lists a. (14 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 I can easily recognize your final answer. int main() { int i; int *p; int s[3] = {4, 7, 5; p = (int *)calloc(s[0], sizeof(int)); for (i = 0; i < s[0]; i++) printf("%d ", p[i]); printf("\n"); p = (int *)realloc(p, s[1] * sizeof(int)); for (i = 0; i < s[1]; i++) { p[i] = s[i%3]; printf("%d ", p[i]); printf("\n"); p = (int *)realloc(p, s[2] * sizeof(int)); for (i = 0; i < s[2]; i++) { p[i] = p[i] / 2; printf("%d ", p[i]); printf("\n"); return 0; 8

4 (continued) b. (5 points) Say you have a linked list built using the structure definition below for each node: typedef struct node { int value; // Data struct node *next; // Pointer to next node LLnode; Assume you have a pointer, list, which points to the first node in the linked list. You are also given two other variables, which are declared as follows: LLnode *p; int n = 0; // General node pointer // Counter Which of the following code snippets will set n equal to the total number of nodes in the list? Clearly indicate your response by marking the one choice you think best answers the question. i. p = list; n = sizeof(p); ii. iii. iv. p = list; while (p == NULL) { p = p->next; n++; p = list; while (p!= NULL) { n += p->val; p = p->next; p = list; while (p!= NULL) { p = p->next; n++; 9

4 (continued) c. (5 points) Circle one (or more) of the choices below that you feel best answers this question. i. Thanks for the free points. ii. iii. iv. This is the best final exam I ve taken today. It s a good thing we have this exam today I couldn t think of anything better to do on a Saturday afternoon. None of the above. 10

5. (10 points) EXTRA CREDIT Everyone may attempt this problem, even if you have not at least partially solved the rest of the exam. Remember, you can earn partial credit for a partial solution to this problem. 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. (Hint: The final output is easily readable, but you must show at least some work to get credit.) Definitions for the Node structure, add1(), and add2() are on the extra handout. int p4b() { int i; Node arr[17] = { {3, 'u', NULL, {16, 'a', NULL, {7, 'o', NULL, {10, 'b', NULL, {1, 'y', NULL, {14, 'e', NULL, {0, ' ', NULL, {18, 'k', NULL, {2, 'y', NULL, {8, 'r', NULL, {0, 'o', NULL, {12, 'r', NULL, {6, 'j', NULL, {9, ' ', NULL, {1, 'n', NULL, {20, '!', NULL, {2, 'E', NULL ; Node *first = NULL; Node *n; for (i = 0; i < 17; i++) { if (i % 2 == 0) first = add1(first, &arr[i]); else first = add2(first, &arr[i]); n = first; while (n!= NULL) { printf("%c", n->ch); n = n->next; printf("\n"); return 0; 11

5 (continued) ADDITIONAL SPACE TO SOLVE EXTRA CREDIT PROBLEM 12