EECE.2160: ECE Application Programming Fall 2017

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

Q1: Multiple choice / 20 Q2: Structures / 40 Q3: Strings / 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

16.216: ECE Application Programming Fall 2015 Exam 2 Solution

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

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

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 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 Spring 2018

EECE.2160: ECE Application Programming Fall 2017

EECE.2160: ECE Application Programming Spring 2016 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

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

CS 1713 Introduction to Programming II

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

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

16.216: ECE Application Programming Fall 2013

SOFTWARE Ph.D. Qualifying Exam Fall 2017

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

Computer Science Foundation Exam

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

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

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

CSE 333 Midterm Exam 7/29/13

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

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

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

CS 1713 Introduction to Programming II

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

Question 1. [15 marks]

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

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

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

Computer Science Foundation Exam

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

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

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

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

MTH 307/417/515 Final Exam Solutions

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

Principles of C and Memory Management

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Computer Science Foundation Exam

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

EECE.2160: ECE Application Programming

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

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Computer Science Foundation Exam

COMP1917: 09 Arrays and Strings

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

ECE264 Fall 2013 Exam 3, November 20, 2013

Basic and Practice in Programming Lab7

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

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

Computer Science Foundation Exam

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

Computer Science Foundation Exam

EE 312 Fall 2017 Midterm 1 October 12, 2017

Here's how you declare a function that returns a pointer to a character:

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

ECE264 Spring 2013 Final Exam, April 30, 2013

Computer Science Foundation Exam

ECE 190 Final Exam. Practice exam

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

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

COP 3223H Fall 2017 Final Exam. Last Name:, First Name: Directions: Please answer questions 1-5 in C, and questions 6-10 in C++.

ECE264 Fall 2013 Exam 1, September 24, 2013

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

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

FORM 2 (Please put your name and form # on the scantron!!!!)

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

CSE 333 Midterm Exam July 24, Name UW ID#

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

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

CSE 333 Midterm Exam 5/10/13

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

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

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

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

Programming Fundamentals

CS 0449 Sample Midterm

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

CS 115 Exam 3, Spring 2010

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

Decimal Representation

Foundation Exam - Sample Problems for New Topics

Fall 2018 Discussion 2: September 3, 2018

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

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

C: How to Program. Week /Mar/05

Arrays and Strings. Arash Rafiey. September 12, 2017

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Transcription:

EECE.2160: ECE Application Programming Fall 2017 1. (34 points) Structures Exam 3 Solution 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) { Sets v1 and v2 members s1ptr->v1 = a + b; of structure to sum and s1ptr->v2 = a * b; product of a and b void f2(struct1 *s1ptr) { Prints structure contents printf("%d %d\n", s1ptr->v1, s1ptr->v2); int main() { Struct1 x = {5, 3; x.v1 = 5, x.v2 = 3 Struct1 y, z; f1(&y, x.v2, x.v1); y.v1 = x.v2 + x.v1 = 3 + 5 = 8 y.v2 = x.v2 * x.v1 = 3 * 5 = 15 f1(&z, y.v2, x.v2); z.v1 = y.v2 + x.v2 = 15 + 3 = 18 z.v2 = y.v2 * x.v2 = 15 * 3 = 45 f2(&x); f2(&y); f2(&z); return 0; OUTPUT: 5 3 8 15 18 45

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 GP = 0; cred = 0; // Go through all elements of arrays in grade list for (i = 0; i < glp->nclass; i++) { // Add credits for current class cred = cred + glp->ncred[i]; // Add grade points for current class // NOTE: You can ignore 'F' since it s worth 0 points switch (glp->grd[i]) { case 'A': GP += 4 * glp->ncred[i]; break; case 'B': GP += 3 * glp->ncred[i]; break; case 'C': GP += 2 * glp->ncred[i]; break; case 'D': GP += glp->ncred[i]; // Return final weighted GPA return GP / cred; 2

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; Always reads just one integer FILE *fp = fopen("f1.bin", "rb"); fread(&x, sizeof(int), 1, fp); printf("%d", x); ii. int i; Doesn t work if file contains <200 values 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; Tries to print results to input file int arr[200]; through file pointer fp 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; Tries to write, not read, input file 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]); 3

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 infp = fopen(inname, "r"); // Read each row from file, then store letter in board for (k = 0; k < 9; k++) { fscanf(infp, " %c %d %d", &XO, &i, &j); board[i][j] = XO; // Open output file outfp = fopen(outname, "w"); // Print board to output file one board row per line of output for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { fprintf(outfp, "%c ", board[i][j]); fprintf("\n"); // Close files fclose(infp); fclose(outfp); 4

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; Notes on solution: Each loop iteration reads up to three characters, stopping if (1) any of the characters is a newline (which is placed back in the input so the loop condition becomes false), or (2) the second character is '-' (c3 only changes if c2!= '-'). The three characters are printed in reverse order at the end of each iteration (c3, c2, c1) The program takes four iterations to process all input characters: Iteration 1: c1 = '9', c2 = '7', c3 = '-' Iteration 2: c1 = '8', c2 = '-' (so nothing new is read into c3, which remains '-') Iteration 3: c1 = '2', c2 = '1', c3 = '6' Iteration 4: c1 = '\n' à loop ends OUTPUT: - 7 9 - - 8 6 1 2 5

3 (continued) b. (5 points) You are given the short program below: int main() { char s1[10]; char s2[10]; scanf("%s", s1); Reads up to first space à s1 = "You re" fgets(s2, 10, stdin); Reads up to 9 characters, since there are >9 characters before newline à s2 = " almost d" (fgets() reads spaces!) 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 6

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)); p = {0, 0, 0, 0 for (i = 0; i < s[0]; i++) printf("%d ", p[i]); printf("\n"); p = (int *)realloc(p, s[1] * sizeof(int)); p = {0, 0, 0, 0,?,?,? for (i = 0; i < s[1]; i++) { p[i] = s[i%3]; p[0] = p[3] = p[6] = 4 p[1] = p[4] = 7 p[2] = p[5] = 5 printf("%d ", p[i]); printf("\n"); p = (int *)realloc(p, s[2] * sizeof(int)); p = {4, 7, 5, 4, 7 for (i = 0; i < s[2]; i++) { p[i] = p[i] / 2; p = {2, 3, 2, 2, 3 printf("%d ", p[i]); printf("\n"); return 0; OUTPUT: 0 0 0 0 4 7 5 4 7 5 4 2 3 2 2 3 7

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 = size of pointer, not whole list n = sizeof(p); ii. p = list; Loop doesn t run unless list is while (p == NULL) { empty (p == NULL), and program will p = p->next; crash when you attempt to access n++; p->next iii. p = list; n = sum of all values in list while (p!= NULL) { n += p->val; p = p->next; iv. p = list; while (p!= NULL) { p = p->next; n++; 8

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. All of the above are correct. 9

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; Solution notes: Each array entry is a linked list Node, with an integer value that may be used to place it in the list, and a character that will be printed when the list is traversed at the end. Each node s next pointer starts as NULL but is overwritten when the node is added to the list. The two add functions are basically the addnode() and addsortednode() functions we wrote in class, only the nodes have already been allocated and filled with data. add1()adds a node to the beginning of the list, while add2() uses the val field in the node to sort the new node being added. The for loop in main() alternates between the two add functions. The next page shows how the list is updated, showing the node that s added each time, the function used to add it, and the set of characters in the list after tha. 10

Node being added add func. used Characters in list {3, 'u', NULL add1 u (unordered) {16, 'a', NULL add2 ua (ordered) {7, 'o', NULL add1 oua {10, 'b', NULL add2 ouba {1, 'y', NULL add1 youba {14, 'e', NULL add2 youbea {0, ' ', NULL add1 youbea {18, 'k', NULL add2 youbeak {2, 'y', NULL add1 y youbeak {8, 'r', NULL add2 y yourbeak {0, 'o', NULL add1 oy yourbeak {12, 'r', NULL add2 oy yourbreak {6, 'j', NULL add1 joy yourbreak {9, ' ', NULL add2 joy your break {1, 'n', NULL add1 njoy your break {20, '!', NULL add2 njoy your break! {2, 'E', NULL add1 Enjoy your break! The while loop at the end of the program simply traverses the list and prints every character, so the output is: Enjoy your break! 11