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

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

CSE 333 Midterm Exam July 24, Name UW ID#

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

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE 333 Midterm Exam July 24, 2017 Sample Solution

CSE 333 Midterm Exam 2/14/14

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

CSE 333 Midterm Exam 5/10/13

CSE 333 Midterm Exam 5/9/14 Sample Solution

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 374 Final Exam Sample Solution 3/17/11

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

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

CSE 333 Midterm Exam 7/22/12

CSE 303 Final Exam. March 16, 2009

CSE 333 Final Exam June 6, 2017 Sample Solution

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CSE 333 Final Exam 3/19/14

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

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

CSE 303 Midterm Exam

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

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Final Exam Sample Solution 3/19/14

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

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

CSE 333 Midterm Exam 7/29/13

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CA341 - Comparative Programming Languages

CSE 374 Final Exam Sample Solution 6/10/10

CSE 303, Autumn 2006, Final Examination 12 December 2006

ESC101N: Fundamentals of Computing End-sem st semester

Name. CPTR246 Spring '17 (100 total points) Exam 2

The make Utility in Unix SEEM

CSE 333 Autumn 2013 Midterm

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

The make Utility in Unix SEEM

Do not turn the page until 12:30.

C for C++ Programmers

Question 1. Part (a) CSC 209H1 S 2017

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

PRINCIPLES OF OPERATING SYSTEMS

CSE 333 Midterm Exam Sample Solution 7/29/13

CSE 303: Concepts and Tools for Software Development

CSE 374 Final Exam Sample Solution 3/15/12

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

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

CSE 303, Spring 2006, Final Examination 6 June Please do not turn the page until everyone is ready.

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

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;

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

Fast Introduction to Object Oriented Programming and C++

Signature: ECE 551 Midterm Exam

There are, of course, many other possible solutions and, if done correctly, those received full credit.

Kurt Schmidt. October 30, 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

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

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

W3101: Programming Languages C++ Ramana Isukapalli

CS24 Week 3 Lecture 1

Memory Organization. The machine code and data associated with it are in the code segment

The University of Nottingham

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

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

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Object Oriented Design

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Question 1. [15 marks]

Homework Assignment #2 (revised)

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

C Programming Basics II

Pointers, Dynamic Data, and Reference Types

CSE351 Winter 2016, Final Examination March 16, 2016

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

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Why VC++ instead of Dev C++?

CSE 374 Midterm Exam 11/2/15 Sample Solution. Question 1. (10 points) Suppose the following files and subdirectories exist in a directory:

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

The University of Nottingham

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Final Intro to C Review

CS61C : Machine Structures

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

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

Short Notes of CS201

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Transcription:

Question 1. (10 points) Preprocessor. Suppose we have the following two files: defs.h: #define DIV(a,b) a / b #define INCDIV(c,d) DIV(c + 1, d + 1) main.c: #include <stdio.h> #include "defs.h" int main() { int n = INCDIV(5, 1); printf("%d\n", n); return 0; (a) (7 points) Show the result produced by the C preprocessor when it processes file main.c (i.e., if we were compiling this file, what output would the preprocessor send to the C compiler that actually translates the program to machine code?). You should ignore the #include <stdio.h> directive since that includes library declarations that we do not have access to. Write the rest of the preprocessor output below. int main() { int n = 5 + 1 / 1 + 1; printf("%d\n", n); return 0; (b) (3 points) What output does this program print when it is executed? 7 Page 1 of 8

Make and related things. We have the following diagram that describes the dependencies between the various files that are involved in building an executable program named gadget. widget.c widget.h gadget.h gadget.c widget.o gadget.o gadget Question 2. (9 points, 3 each) What can we conclude from the above diagram? In the statements below, circle the number of the best choice in each group of possible answers: (a) The widget.c source file: (i) Definitely contains a #include "widget.h" preprocessor directive. (ii) Might contain a #include "widget.h" preprocessor directive, but might not. (iii) Does not contains a #include "widget.h" preprocessor directive. (b) The gadget.c source file: (i) Definitely contains a #include "widget.h" preprocessor directive. (ii) Might contain a #include "widget.h" preprocessor directive, but might not. (gadget.h might #include widget.h to create the dependency.) (iii) Does not contains a #include "widget.h" preprocessor directive. (c) The main function for this program: (i) Definitely is implemented (defined) in the gadget.c source file. (ii) Might be implemented (defined) in the gadget.c source file, but might not. (iii) Is not implemented (defined) in the gadget.c source file. (more about this diagram on the next page) Page 2 of 8

Question 3. (17 points) Making stuff. Write an appropriate Makefile that will compile and link the files described by the dependency diagram on the previous page to build the executable program named gadget. Your Makefile should only recompile or relink files if needed to bring targets up to date. The default target in the Makefile should be the executable program gadget, i.e., if make is run with no arguments, the gadget program (target) should be built. You should use gcc with appropriate options to compile the program. To save a little time and writing, you may omit -std=c11, but you should include -Wall and -g in appropriate places. In addition, you may find the following options to be useful: -o outputfilename specify gcc output file -c only create a.o file and then stop without linking. (If -c is given, then the option -o filename.o is assumed as the default, where filename.c is the input file.) Reminder: the format of a makefile rule is: target: sources command gadget: widget.o gadget.o gcc Wall g o gadget widget.o gadget.o widget.o: widget.c widget.h gcc Wall g c widget.c gadget.o: gadget.c gadget.h widget.h gcc Wall g c gadget.c Note: This solution is about the minimum possible. Solutions that included other features like o options or Makefile variables did, of course, receive full credit if those features were used properly. Page 3 of 8

Question 4. (22 points) C programming! This question involves adding a function to the code from HW2. Copies of the HashTable.h and memindex.h header files, which are the ones needed for this problem, are provided on separate pages. You should assume that those headers, and any other necessary header files and declarations, have already been included in the source files. After building the inverted index data structures in HW2, we would like to scan the index and, for each word we find, print out the number of different documents where the word appears. For example, here is the inverted index diagram from the HW2 assignment: The output of the function should list each word in the inverted index and the number of documents where it appears, using a separate line for each word. For the above data the output might be: recommend 1 love 3 course 1 The words may appear in any order, and the exact spacing doesn t matter. The output should be written to stdout (i.e., feel free to use printf). On the next page, implement function PrintWordDocCounts to produce this output. (Hint: Don t panic! The solution isn t particularly long the sample solution is less than 15 or 20 lines of code. Of course, yours isn t required to be any particular length.) Page 4 of 8

Question 4. (cont.) Write your implementation of function PrintWordDocCounts below. // For each word in MemIndex mi, print on stdout a line // containing that word and the number of documents where // it appears. void PrintWordDocCounts(MemIndex mi) { Verify333(mi!= NULL); // add your code here HTIter iter = HashTableMakeIterator(mi); while (!HTIteratorPastEnd(iter)) { HTKeyValue kv; int res = HTIteratorGet(iter, &kv); if (res) { WordDocSet *wds = (WordDocSet *) kv.value; char *word = wds->word; int count = NumElementsInHashTable(wds->docIDs); printf("%s: %d\n", word, count); HTIteratorNext(iter); HTIteratorFree(iter); Grading note: There were a few solutions that processed the hash table by calling HTIteratorNext repeatedly until the number of calls matched the table size, without actually using HTIteratorPastEnd. We allowed this on the exam without deducting points, but it is poor style since it mixes a counting loop with an iterator. It would be much better to use the iterator interface by itself. Page 5 of 8

Question 5. (20 points) Bugs R Us. One of your friends has been trying to learn C and has been experimenting with a linked list of strings, using dynamically allocated nodes and string values. The list allows duplicates and strings are not stored in any particular order. The problem is that the code is badly broken, and, in fact, doesn t even compile. Make changes needed in the code below so that function new_node allocates a properly initialized new node and insert successfully adds it to the front of the list. If you want to rewrite the code instead of modifying it here, you can use the next page. (Sample solution given below as additions and changes in bold green type. The main bugs are missing dynamic allocation for strings and nodes. Given that this is an exam question we did not require full error checking of malloc results as would be done in production code.) #include <stdlib.h> #include <string.h> // needed for strlen, strcpy struct strnode { // node in a string linked list: char * str; // copy of the string (on the heap) struct strnode * next; // next node or NULL if none ; struct strnode * str_list; // global pointer to list of strings // return a new node with copy of string s and next == NULL struct strnode * new_node(char * s) { struct strnode * node; node = (struct strnode *)malloc(sizeof(strut strnode)); node->str = (char *)malloc((strlen(s)+1); strcpy(node->str, s); node->next = NULL; return node; // add a new node with a copy of s to the front of str_list void insert(char * s) { struct strnode * p = new_node(s); // * missing in decl. p->next = str_list; str_list = p; // order of these 2 lines reversed Page 6 of 8

Question 6. (20 points) Not quite the traditional what-does-it-print question. Consider the following C++ program, which, as is usual, compiles and executes with no errors. #include <iostream> using namespace std; int f(int &n, int *pa, int &k, int *pb) { k = pb[1]; pb[2] = pa[1]; n = *pb**pa; return k+1; int main() { int a = 1; int &b = a; int ray[4] = { 10, 11, 12, 13 ; int *p = ray; int *q = &ray[1]; *p = f(ray[2], p, b, q); cout<< "a = " << a << ", b = " << b << ", *q = " << *q << endl; cout<< "ray = "; for (int k = 0; k < 4; k++) cout << ray[k] << " "; cout << endl; return 0; What output does this program produce when it runs? (You are not required to draw a boxes-n-arrows diagram, but you might find doing so to be very helpful, and it might help us if we need to assign partial credit to a not-completely-perfect answer.) a = 12, b = 12, *q = 11 ray = 13 11 110 11 Page 7 of 8

Question 7. (2 free points) (All reasonable answers receive the points. All answers are reasonable as long as there s something written here. J) (a) (1 point) What question were you expecting to appear on this exam that wasn t here? Implement a C++ compiler. Be sure to include all library functions and templates plus a complete set of tests for all of your code. (b) (1 point) Should we include that question on the final exam? (circle or fill in) Yes No Heck No!! $!@$^*% No!!!!! None of the above. My answer is Sure, why not?. Page 8 of 8