CSE 333 Autumn 2014 Midterm Key

Similar documents
CSE 333 Autumn 2013 Midterm

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Supporting Class / C++ Lecture Notes

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Arrays and Pointers in C. Alan L. Cox

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

CSE 303 Midterm Exam

CSE 303 Winter 2008 Midterm Key

Exercise Session 2 Simon Gerber

Exercise Session 2 Systems Programming and Computer Architecture

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

The University of Nottingham

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CS 1713 Introduction to Programming II

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

Overview (1A) Young Won Lim 9/14/17

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

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

Overview (1A) Young Won Lim 9/9/17

Final CSE 131B Spring 2004

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Coding Standards for C

Chapter 11 Introduction to Programming in C

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

Introduction to C. CS2023 Winter 2004

QUIZ. Source:

Overview (1A) Young Won Lim 9/25/17

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CSE 333 Lecture 6 - data structures

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

Arrays and Memory Management

QUIZ. What are 3 differences between C and C++ const variables?

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

CS 0449 Sample Midterm

Programming in C. Pointers and Arrays

EL6483: Brief Overview of C Programming Language

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Chapter 11 Introduction to Programming in C

ECE264 Spring 2013 Final Exam, April 30, 2013

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Chapter 11 Introduction to Programming in C

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Coding Standards for C

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

A Fast Review of C Essentials Part II

Friday, February 10, Lab Notes

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

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

CSE 333 Lecture 7 - final C details

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

Chapter 11 Introduction to Programming in C

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Homework 2 Answers. Due Date: Monday, April 29, 2002, at 11:59PM Points: 100

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Lecture 03 Bits, Bytes and Data Types

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CSE 333 Midterm Exam Sample Solution 7/28/14

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS 241 Data Organization Pointers and Arrays

Have examined process Creating program Have developed program Written in C Source code

CSE 333 Midterm Exam 7/22/12

HW1 due Monday by 9:30am Assignment online, submission details to come

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CSE 333 Lecture 6 - data structures

Chapter 11 Introduction to Programming in C

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

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

Lecture 04 Introduction to pointers

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Kurt Schmidt. October 30, 2018

CS102. Lecture 7. xkcd. xkcd. palindramas

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

ELEC 377 C Programming Tutorial. ELEC Operating Systems

CS61C : Machine Structures

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017

CSE 333 Midterm Exam 7/29/13

Homework 4 Answers. Due Date: Monday, May 27, 2002, at 11:59PM Points: 100. /* * macros */ #define SZBUFFER 1024 /* max length of input buffer */

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

PROGRAMMAZIONE I A.A. 2017/2018

ECE264 Spring 2014 Exam 2, March 11, 2014

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

***PRACTICE EXAM 02 for*** CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Actual exam scheduled for: 11/18/2009

Course organization. Course introduction ( Week 1)

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Tutorial - Using existing plugins in GCC

CS3157: Advanced Programming. Outline

Sums. Here's a (poor) program to add three numbers

Transcription:

CSE 333 Autumn 2014 Midterm Key

1. [3 points] Imagine we have the following function declaration: void sub(uint64_t A, uint64_t B[], struct c_st C); An experienced C programmer writes a correct invocation: sub(a, b, c); Briefly explain what values in the calling code the programmer might reasonably expect could be changed by that call. For instance, might the value of a change? None of the variables a, b, or c can have their values changed, because C is purely call-byvalue. On the other hand, the caller can examine things that b refers to (e.g., b[3]), and the callee can modify those things, so they may be changed on return. (Additionally, c is a struct, and we don't know what it's fields are. If any of them are pointers, what they point to can be changed by the callee, and that change will be visible in the caller. There were bonus points for mentioning this, but it wasn't required to get full credit.) 2. [4 points] This statement defines a variable, Q2: struct { int intfield; char charfield; void *pointerfield; Q2; (A) What is the value of each of the following? sizeof(q2.intfield) sizeof(q2.charfield) sizeof(q2.pointerfield) 4 1 8 (64-bit) or 4 (32-bit)_ (B) Briefly explain why sizeof(q2) might not be equal to the sum of those three terms. The compiler must lay out the fields in the ordered declared. It may choose to put a field at an address that is a multiple of that field's length, for performance reasons. (This is called alignment.) After laying out the int and the char, the compiler may insert padding so that the pointer field is at an address that is a multiple of the pointer's length (8). The struct therefore can include padding, so its total length can be larger than the sum of its fields' lengths.

3. [8 points] Imagine we're implementing a variable sized array of integers as class Array. An Array has a capacity, which is how many elements it can currently hold, and a size, which is how many it is currently holding. Here's a definition of the Array type and the bit of its implementation that creates a new instance: typedef struct { int capacity; int size; int *values; *Array; Array array_new() { int values[10]; Array a = (Array)malloc(1 * sizeof(array)); a >capacity = 10; a >size = 0; a >values = values; return a; The array_new function above compiles fine but is buggy. Write a bug-free version here. Array array_new() { Array a = (Array)malloc(1 * sizeof(*a)); if ( a == NULL ) return NULL; a >values = (int*)malloc(10 * sizeof(int)); if ( a >values == NULL ) { free(a); return NULL; a >capacity = 10; a >size = 0; return a; 4. [2 points] It's almost always an error to put code (for example, the definition of a subroutine) in a.h file because (pick one): (a) unless you go way out of your way, the preprocessor won't recognize the code and will generate errors (b) modern compilers simply won't allow code that comes from.h files (c) unless you're lucky, the linker will generate errors

(d) the code will preprocess, compile, and link, but you'll get run time errors when you try to execute because the code in the.h will be in the static data segment, which isn't executable The answer is (c). (a) isn't right because the preprocessor processes #xxxxx directives but simply passes all other text through. (b) isn't right because the compiler can't tell where the code came from. (c) is right because it's common for there to be multiple source files, and if each one #includes some code that code will have multiple definitions. (The compiler won't detect that because it compiles a single file at a time. The linker will notice when it tries to put all the.o files together to build the executable.) (d) isn't right because everything about it is wrong. 5. [3 points] Suppose you have the following variable declaration: typedef struct element_st { struct element_st *next; int val; Element; Element *p[4]; And suppose that you know that the data currently looks like this: p 111 222 333 Write a C statement that will change the value 222 to 0. p[2]->next->val = 0; p[2] is a pointer to an Element. Take that element's next field and dereference it (because of the ->). That's an Element. Now take that element's val field and set it to 0. 6. [2 points] Suppose I've written method sub() in file sub.c, and I want to call it from code in file main.c. In that case I can either: (a) put a prototype declaration for sub() near the head of main.c, or (b) put a prototype delcaration for sub() in file sub.h and #include sub.h near the head of main.c Briefly, but precisely, explain why (b) is a much better solution than (a). We want the definition of sub (in the.c file) and the declarations of sub (required by the uses of sub) to either be consistent or for the compiler to detect that something is wrong. If each file using sub has its own declaration simply typed into that file, if the sub interface changes (e.g., a parameter is added), the source files using sub will still

compile fine even if their declarations of sub are not changed the compiler will just believe the declaration typed into those files and generate code assuming they're right. If both the uses of sub and the definition of sub share a single declaration of sub by #include'ing the.h file, then if they disagree about sub's interface at least one of them will get compile time errors because the use (or definition) of sub won't match the declaration. 7. [4 points] I'd like to be able to write statements like this: #include isvowel.h... if ( isvowel(c) ) printf ( %c is a vowel, c); where c is of type char. isvowel returns true if the character is a vowel (a, e, i, or u) and false otherwise. For performance reasons, I want isvowel to be implemented as a preprocessor macro. Write a version of the isvowel.h file that satisfies my requirements. This questions asks you to write the file (so #ifndef guard code is needed): #ifndef ISVOWEL_H #define ISVOWEL_H #define isvowel(c) ((c)=='a' (c)=='e' (c)=='i' (c)=='o' (c)=='u') #endif // ISVOWEL_H Note that the preprocessor is going to substitute the string value #define'd for isvowel(c), substituting whatever the calling code has supplied for 'c', directly into the calling code. You cannot have a return statement, or any statements, in that string value if you want the example code to compile. (If you do have a return, you'll end up with if (. return...)... as what the compiler sees for the example code, which wont be good.) 8. [8 points] Use an assumed correct version of isvowel.h to write a subroutine that counts the number of vowels and non-vowels in a file whose name is passed as the only argument. Your subroutine should return those two counts by returning a single struct of this type: typedef struct isvowel_result_st { int iscount; int isntcount; IsVowel_result; (If any errors occur, you should return counts of -1.) IsVowel_result countvowels(const char *filename) { IsVowel_result result = {.iscount = 0,.isntCount = 0 ; FILE *f = fopen(filename, "r"); if (f == NULL) { result.iscount = result.isntcount = 1;

return result; char c; while ( (c=fgetc(f))!= EOF) { if ( isvowel(c) ) result.iscount++; else result.isntcount++; fclose(f); return result; (IF you wanted to be really client code friendly, you'd check that filename!= NULL before trying to use it for anything, and return an error if it were. This code (likely) just causes a segfault in a way that's harder for the client to debug.) 9. [6 points] Now build on the code you wrote in the last two question to write an application that prints out the number of vowels and the number of non-vowels in the files whose names are given as command line arguments, e.g., $./countvowels countvowels.c isvowel.h somefile.txt (Assume that the solution to question 8 provides a.h file with a suitable prototype declaration to make use of your subroutine implementation.) int main(int argc, char *argv[]) { // Note that I don't have to check argc here... for (int i=1; i<argc; i++) { printf("\nfile: %s\n", argv[i]); IsVowel_result result = countvowels(argv[i]); if ( result.iscount < 0 ) printf("some error: probably couldn't open %s\n", argv[i]); else printf("\tiscount / isntcount = %d %d\n", result.iscount, result.isntcount); return 0; There's the issue of whether the spec is ambiguous should the app report separate results for each file listed on the command line (as this solution does), or should it report a total for all the files? Modularity suggests the former. The user can always cat multiple files into one if that's what they want. If the app effectively does the cat, it's awkward to use if that isn't what you want. Aside: Typical Unix apps will allow a file name of '-' to mean stdin. So, I could write something like this: $ cat countvowels.c isvowel.h somefile.txt./countvowels -

if I want the total number of vowels in a set of files. (Sometimes the absence of any file name will mean read from stdin, rather than having to type the '-'. cat itself does that, for instance.)