MidtermSolutionCS3214F11

# MidtermSolutionCS3214F11 - CS 3214 Fall 2011 Midterm...

This preview shows pages 1–4. Sign up to view the full content.

CS 3214 Fall 2011 Midterm Solution 1/14 CS 3214 Midterm Solution 67 students took the midterm. The table below shows who graded which problem. If you have questions, contact the person who graded the respective problem first. Students who scored below 40 are at risk of failing the class even if they otherwise meet minimum requirements; they will need to show improvement taking the final exam. 1 2 3 4 Total Median 11 11 15 17 55 Average 11.0 12.5 14.7 18.4 56.7 StDev 3.3 6.5 5.8 8.1 17.8 Min 4 2 2 3 22 Max 18 25 24 33 98 Grader Parang Jake Godmar a,b) Ruslan c) Godmar d) Jake Solutions are shown in this style. Grading Comments in this style. 0 2 4 6 8 10 12 14 16 18 21 30 31 40 41 50 51 60 61 70 71 80 81 90 91 100 CS3214 Midterm Fall 2011 n=67, med=55

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 3214 Fall 2011 Midterm Solution 2/14 1. Compiling and Linking (18 pts) a) (14 pts) Separate Compilation. Consider the following two .c files which both include the same .h file: // a.h (1) static int inc(int x) { return x + 1; } (2) static int x; (3) extern int y; // or empty (4) int z; (5) extern void b(void); // or empty // a.c #include "a.h" #include <stdio.h> (6) int y = 1; (7) extern int w; // or empty (8) static int v = 5; int main() { x = inc(0); z += 4; b(); printf("x = %d y = %d z = %d " "w = %d v = %d\n", x, y, z, w, v); return 0; } // b.c #include "a.h" (9) int w = 4; (10) int v = 5; // or static void b() { x = inc(x); y++; z--; v++; } When compiled, linked, and executed, the following output results: \$ gcc -Wall a.c b.c -o a \$ ./a x = 1 y = 2 z = 3 w = 4 v = 5 Assuming that this program compiled and linked successfully, and based on the output shown above, add static and/or extern modifiers to the blank lines (1) through (10)! Leave them blank if neither modifier would be appropriate! Any correct solution that results in successful compilation and the output shown above will be accepted. Explanation: (1) Needs to be static, or else linking fails due to 2 conflicting strong symbols ‘inc’ with an “already defined” error (2) x must be static. If it weren’t, and a.c and b.c shared the same x, its value would be 2, not 1. (3) y should be extern (it must be shared between a.c and b.c, else its value wouldn’t be 2.) However, leaving it without modifier works also (fwiw, int y;
CS 3214 Fall 2011 Midterm Solution 3/14 followed by int y = 1; compiles and the strong definition is used.) It cannot be static. (4) z’s modifier must be blank so that ‘z’ becomes a common symbol (shared, but weak). If extern were used, z would be nowhere defined (undefined reference error). If static were used, it would have the value 4, not 3. Note that defining ‘z’ in a header file is generally considered bad practice. (5) Should be extern (or left blank, which has identical semantics for

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/31/2011 for the course CS 3214 taught by Professor Staff during the Fall '11 term at Virginia Tech.

### Page1 / 14

MidtermSolutionCS3214F11 - CS 3214 Fall 2011 Midterm...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online