CSE 333 Midterm Exam 7/29/13

Similar documents
CSE 333 Midterm Exam Sample Solution 7/29/13

CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam 2/14/14

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

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

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam 7/22/12

CSE 333 Midterm Exam 2/12/16. Name UW ID#

CSE 333 Final Exam 3/19/14

CSE 374 Final Exam 3/15/17. Name UW ID#

CSE 333 Midterm Exam 5/9/14 Sample Solution

CSE 331 Final Exam 12/9/13

Final CSE 131B Spring 2004

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 303 Midterm Exam

Pointers, Arrays and Parameters

CSE 331 Midterm Exam 2/13/12

CSE 374 Midterm Exam 11/2/15. Name Id #

CSE 333 Midterm Exam 7/27/15 Sample Solution

Pointers and scanf() Steven R. Bagley

CSE 374 Programming Concepts & Tools

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

CSE351 Winter 2016, Final Examination March 16, 2016

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

CSE 303 Final Exam. March 16, 2009

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

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

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

Question 1. [15 marks]

Common Misunderstandings from Exam 1 Material

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

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

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

CSE 333 Midterm Exam Nov. 3, Name UW ID#

CS 103 Lab - Party Like A Char Star

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CSE P 501 Exam 12/1/11

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

CSE 333 Midterm Exam July 24, 2017 Sample Solution

CSE 333 Autumn 2013 Midterm

CSE 333 Lecture 6 - data structures

CS3157: Advanced Programming. Outline

CSE wi Midterm Exam 2/8/18. Name UW ID #

CSE 333 Lecture 2 Memory

CSE P 501 Exam 8/5/04

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

Student Number: Computer Science 211b Final Examination. 28 April hours

CS 237 Meeting 19 10/24/12

211: Computer Architecture Summer 2016

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Procedures, Parameters, Values and Variables. Steven R. Bagley

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

C++ for Java Programmers

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

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

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

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

CSE wi Final Exam 3/12/18. Name UW ID#

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

Kurt Schmidt. October 30, 2018

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Data Structure Series

CSE 333 Lecture 6 - data structures

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

EECE.2160: ECE Application Programming Fall 2017

CSE 303, Autumn 2006, Final Examination 12 December 2006

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

CSE 374 Midterm Exam Sample Solution 2/11/13. Question 1. (8 points) What does each of the following bash commands do?

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

C introduction: part 1

CSE 333 Final Exam June 6, 2017 Sample Solution

Grades. Notes (by question) Score Num Students Approx Grade A 90s 7 A 80s 20 B 70s 9 C 60s 9 C

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 331 Final Exam 6/5/17. Name UW ID#

CSCI 2132 Final Exam Solutions

CSE 331 Final Exam 3/12/12

CSE 303, Winter 2007, Final Examination 15 March Please do not turn the page until everyone is ready.

Computer Science Foundation Exam

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

Operator overloading

Computer Science Foundation Exam

CS 1713 Introduction to Programming II

CS61, Fall 2012 Section 2 Notes

CSE 333 Final Exam Sample Solution 3/19/14

Objective-C. Stanford CS193p Fall 2013

CSE 374 Final Exam Sample Solution 6/10/10

Dynamic arrays / C Strings

CSE 374 Final Exam Sample Solution 3/15/12

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

CSE 374 Midterm Exam Sample Solution 2/6/12

Transcription:

Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed electronics, closed telepathy, open mind. If you don t remember the exact syntax for something, make the best attempt you can. We will make allowances when grading. Don t be alarmed if there seems to be more space than is needed for your answers we tried to include more than enough blank space. Relax, you are here to learn. Please wait to turn the page until everyone is told to begin. Score / 100 1. / 44 2. / 20 3. / 15 4. / 20 5. / 1 Page 1 of 9

Question 1. (44 points) C hacking a question of several parts. The next several pages are questions about a linked list of 2-D points. Each point is represented by a Point struct containing the point s x and y coordinates: typedef struct point { double x; double y; Point; // a 2-D point // x and y coordinates A list of points is a single-linked list, where each node contains a pointer to its associated Point struct and a pointer to the next node in the list, if there is one. typedef struct pt_node { // node in a linked list of Points Point * pt; // point owned by this node struct pt_node * next; // next list node, or NULL if none Ptnode; All of the data in the list (points and nodes) is owned by the list and allocated on the heap. Point and node structs are created and initialized when they are added to the list. Example: A list containing the points (1.0, 2.0) and (3.0, 4.0) would be drawn like this: x 1.0 y 2.0 x 3.0 y 4.0 list pt next pt next / Answer the questions that use this list structure on the following pages. You can detach this page from the test for reference while you are working if that is convenient. Page 2 of 9

Question 1. (cont.) (a) (15 points) The distance of a point (x,y) from the origin is calculated as sqrt(x*x+y*y). Implement the function max_distance below so it calculates the distance from the origin of each point in the list and returns the largest distance found. If the list is empty, the maximum distance returned should be 0.0. Restriction: for full credit your function must use an appropriate loop to traverse the list and may not use recursion or any additional functions besides sqrt. Example: if the list contains the points (1, 2) and (3, 4), the max distance returned should be 5.0 (= sqrt(3*3+4*4)). #include <math.h> // for sqrt #include <stdlib.h> // for NULL // return max distance from origin of any point in the list // (return 0.0 if the list is empty) double max_distance(ptnode * list) { Page 3 of 9

Question 1. (cont.) (b) (15 points) Complete the definition of function free_list below so that it frees (returns to the heap) all of the data belonging to the list that is its argument. After freeing the list nodes and points, the function should set the list pointer supplied as its argument to NULL. Restriction: as with part (a), for full credit your function must use an appropriate loop to traverse the list and may not use recursion or any additional functions besides free. #include <stdlib.h> // for free, NULL // free list and contents, then set *list to NULL void free_list(ptnode ** list) { Page 4 of 9

Question 1. (cont.) (14 points) One of the summer interns who has not taken CSE333 was asked to write a function to add the value of a new Point to the front of a list. Alas, the code has lots of problems. Indicate on the code below the changes needed so it will work exactly as specified. You may not change the specification of the function, the parameters, or the return type. You may add, remove, rearrange, or alter statements in the body of the function as needed. // Add a new Point to the front of the list lst, with the // new point s (x,y) values taken from parameter pt. // Update lst to point to the new node added at the front. // Return 1 if successful and 0 if not. int push_front(point pt, Ptnode ** lst) { Ptnode * node = (Ptnode*)malloc(sizeof(Ptnode*)); Point * npt = (Point*)malloc(sizeof(Point*)); node->pt = &pt; node->next = lst; lst = node; if (npt == NULL node == NULL) { return 0; return lst; Page 5 of 9

Question 2. (20 points) Yet another ghastly C program. As is traditional, it does compile and execute with no warnings or errors: void modifyptr(int *ptr, int **dblptr) { int *q = ptr; *ptr = 5; ptr++; *ptr = 6; *(q + 2) = 7; *((*dblptr) - 1) = 8; // -->HERE<-- // int main(int argc, char **argv) { int arr[4] = {1, 2, 3, 4; int *p = arr + 1; modifyptr(&(arr[0]), &p); printf("%d %d %d %d %d\n", arr[0], arr[1], arr[2], arr[3], *p); return 0; (a) Draw a boxes n arrows diagram showing state of memory when control reaches the comment containing -->HERE<--, right before executing the return statement in function modifyptr. Your diagram should have two boxes showing the stack frames for functions main and modifyptr. The stack frames should include values of integer variables and an arrow from each pointer to the location that it references. Then answer part (b) at the bottom of the page. (b) What output does this program produce when it is executed? Page 6 of 9

Question 3. (15 points) Make and program building. We have a program that consists of a header file f.h, an implementation file f.c, and a main program main.c. Normally we could use the following Makefile to build the program mumble from these source files: mumble: main.o f.o gcc -o mumble main.o f.o main.o: main.c f.h gcc -c -o main.o main.c f.o: f.c f.h gcc -c -o f.o f.c Unfortunately we need to build this program on a new experimental system that does not yet have a gcc command(!). Fortunately, it does have the three programs that gcc runs behind the scenes to build a program: the preprocessor cpp, the compiler itself cc1, and the loader ld. These commands work as follows: cpp outfile infile preprocess the program read from infile (say f.c) and write the preprocessed results to outfile (by convention named f.i if the input file is f.c). cc1 outfile infile compile the already-preprocessed file infile (say f.i) and write the compiled code to the object file outfile (a.o file, for example f.o) ld outfile infiles read the one or more.o files listed as infiles and write an executable file named outfile (mumble in our example above). For this problem, show the changes that would be needed in the above Makefile so it would build the program as before, but using cpp, cc1, and ld instead of gcc. You can add or delete Makefile rules and Linux commands as needed. The resulting Makefile should only recompile/preprocess/load files when necessary, as is the case with the original one. Note that cpp and cc1 only preprocess or compile a single file at a time, so you can t, for instance, preprocess all of the.c files with a single command. Also (if it helps), remember that a single Makefile rule can have more than one command on successive lines to bring a file up to date. Page 7 of 9

Question 4. (20 points) In one of the exercises we implemented a C++ class Vector that represented 3-D points and supplied various operations on them. A Vector had the following representation: class Vector {... private: double x_, y_, z_; // x, y, and z magnitudes of Vector ; The operations provided included constructors (Vector(), Vector(x,y,z) and copy constructor); assignment; addition and subtraction, which produced new Vector values; scalar and dot-product multiplication; and stream output. For this problem, give the declaration and implementation of a new operator+= for Vectors. If u is the Vector (a,b,c) and v is the Vector (i,j,k), then u+=v should modify vector u so it has the value (a+i,b+j,c+k). Note that += is almost the same as regular assignment, except it computes a new value for its left operand instead of copying the right operand without changes. Self-updates like u+=u, and chained updates like u+=v+=w should also work properly. (a) (6 points) Give the correct declaration (function prototype) of operator+= that should be added to class Vector in the header file Vector.h. (This is only one line) (b) (14 points) Give the full implementation of operator+= that should be added to file Vector.cc. Page 8 of 9

Question 5. (1 point) The best reason to take CSE 333 during the summer is (circle the letter of the best answer; all thoughtful, honest answers get full credit): (a) A 9:40 class makes me wake up in the morning instead of sleeping till mid-afternoon. (b) Hacking is much more fun than hiking. (c) Next year I ll get that Google internship after taking this class! (d) I d rather be in an air-conditioned, dark basement room instead of out in the bright, hot sunlight. (e) Learning C is better than sitting on the shore by the sea. (f) I get to finally figure out what all the funny symbols mean (*&::!->). (g) gdb is a better video game than anything else I ve got. (h) Gotta get it out of the way eventually, so why not now? (i) Best reason? Can t think of one, but please give me my free point anyway. (j) I do have a good reason, but I d rather not say. Please give me my free point. (k) You didn t include my reason, it s Page 9 of 9