hcmut2010 final

8
ECE 190 Final Exam HoChiMinh University of Technology Tuesday 27 July 2010 Be sure your exam booklet has 8 pages (4 sheets). You have THREE HOURS to complete this exam. Wherever necessary, even if not explicitly specified in the code, assume that stdio.h has been included in the code. Thus, printf and scanf statements may be used in the code. Write your name at the top of each page. This is a closed book exam. You may not use a calculator. You are allowed THREE handwritten A4 sheets of notes (both sides). Absolutely no interaction between students is allowed. Show all work. State all assumptions. Don’t panic, and good luck! Problem 1 20 points _______________________________ Problem 2 20 points _______________________________ Problem 3 30 points _______________________________ Problem 4 30 points _______________________________ Total 100 points _______________________________ Name: Student ID # :

Upload: ace-nhat

Post on 24-Jan-2016

214 views

Category:

Documents


0 download

DESCRIPTION

Electrical & Electronic Engineering, Introduction to Computer Engineering

TRANSCRIPT

Page 1: HCMUT2010 Final

ECE 190 Final Exam HoChiMinh University of Technology

Tuesday 27 July 2010

• Be sure your exam booklet has 8 pages (4 sheets). • You have THREE HOURS to complete this exam. • Wherever necessary, even if not explicitly specified in the code, assume that

stdio.h has been included in the code. Thus, printf and scanf statements may be used in the code.

• Write your name at the top of each page. • This is a closed book exam. • You may not use a calculator. • You are allowed THREE handwritten A4 sheets of notes (both sides). • Absolutely no interaction between students is allowed. • Show all work. State all assumptions. • Don’t panic, and good luck!

Problem 1 20 points _______________________________ Problem 2 20 points _______________________________ Problem 3 30 points _______________________________ Problem 4 30 points _______________________________ Total 100 points _______________________________

Name: Student ID # :

Page 2: HCMUT2010 Final

Page 2 Name: ____________________________________________

Problem 1 (20 points): Short Answer Questions Be concise: if your answer contains more than 10 or 15 words or a simple picture, it is probably wrong. Part A (4 points): Write the value of variable j after the code below executes. int i, j; for (i = 0; 10 > i; i++) { if (42 == i) { j = 18; } } Part B (4 points): The following code has one error. Identify the error, state whether or not it will be caught by the compiler, and indicate how to fix the error. void player_free (player_t* p) { free (p); free (p->name); } Part C (4 points): The following code has one error. Identify the error, state whether or not it will be caught by the compiler, and indicate how to fix the error. int pascal_triangle (int n, int m) { if (n = 0) { return 1; } if (n – 1 == m) { return 1; } if (m == 0) { return 1; } return pascal_triangle (n – 1, m – 1) + pascal_triangle (n – 1, m); }

Page 3: HCMUT2010 Final

Page 3 Name: ____________________________________________

Problem 1, continued: Part D (8 points): A friend writes the following code, which has two errors. Identify both errors, state whether or not each will be caught by the compiler, and indicate how to fix each error. typedef struct { int top; double data[]; } stack_t; /* return 0 on success, -1 on failure */ int stack_push (stack_t* s, double val) { s->data[--s->top] = val; return 0; }

Page 4: HCMUT2010 Final

Page 4 Name: ____________________________________________

Problem 2 (20 points): A Simple C Program Complete the function below to print a given string along the upper and right sides of a square of size N, where N is the length of the string in ASCII characters (not counting NUL). Examples are shown in the figure below. The rest of the squares are filled with spaces (' '). Assume that N is a positive integer. void draw_string_box (char* s) { int N, a, b; N = strlen (s); for (a = ; ; a++) { } }

hello l l e h

Yes e Y

Proper Output for s="Yes" (N=3)

Proper Output for s="hello" (N=5)

ECE190 9 1 E C E

Proper Output for s="ECE190" (N=6)

Page 5: HCMUT2010 Final

Page 5 Name: ____________________________________________

Problem 3 (30 points): Recursion and Stack Frames This problem focuses on the recursive function shown below. int to_number (char* s, int* pos) { int digit, idx, value; if ('\0' == *s) { *pos = 0; return 0; /* Part C refers to this line */ | if ('-' == *s) { *pos = 0; return –to_number (s + 1, pos); } digit = (*s) – '0'; value = to_number (s + 1, &idx); /* Part B stack frames shown at this point in code. */ *pos = idx + 1; while (0 < idx--) { digit = digit * 10; } return (digit + value); } Part A (10 points): Write the values of variables x and y after the code below executes. int x, y; /* x: ______________ */ x = to_number ("--42", &y); /* y: ______________ */ Part B (10 points): The to_number function is called and has just returned from its third recursive call to its second (see the point marked in the code). Based on the two stack frames shown below, write the contents (not the address) of the string originally passed to the function (that is, the first call to to_number, which corresponds to the bottom-most stack frame). s = ____________________________ xC771 x0000 digit ← R6

xC772 x0003 idx xC773 x0030 value (48 in decimal) ← R5 xC774 xC77B previous frame pointer xC775 x31F5 return address xC776 x0175 return value xC777 x45A4 s xC778 xC77A pos xC779 x0002 digit xC77A x9732 idx xC77B x632A value xC77C xC786 previous frame pointer xC77D x31F5 return address xC77E x1920 return value xC77F x45A3 s xC780 xC792 pos

Page 6: HCMUT2010 Final

Page 6 Name: ____________________________________________

Problem 3, continued: (The code is replicated here for your convenience.) int to_number (char* s, int* pos) { int digit, idx, value; if ('\0' == *s) { *pos = 0; return 0; /* Part C refers to this line */ | if ('-' == *s) { *pos = 0; return –to_number (s + 1, pos); } digit = (*s) – '0'; value = to_number (s + 1, &idx); /* Part B stack frames shown at this point in code. */ *pos = idx + 1; while (0 < idx--) { digit = digit * 10; } return (digit + value); } Part C (10 points): The to_number function is called on the string "9" (starting at address x4187) and with pos=x8887. The “return 0” statement marked for Part C executes only once; draw the state of the stack just before the stack frame is torn down (immediately after execution of “return 0”).

• Based on the function and the stack frames shown in Part B, fill in whatever values are known, and mark unknown values as “unknown.”

• Clearly indicate the values of the top of stack (R6) and frame pointer (R5). • Do NOT leave any entries below the top of stack blank.

xD571 digit

xD572 idx

xD573 value

xD574 previous frame pointer

xD575 return address

xD576 return value

xD577 s

xD578 pos

xD579 digit

xD57A idx

xD57B value

xD57C previous frame pointer

xD57D return address

xD57E return value

xD57F s

xD580 pos

Page 7: HCMUT2010 Final

Page 7 Name: ____________________________________________

Problem 4 (30 points): Linked Lists This problem makes use of a linked list based on the structure below and a comparison function, obj_smaller, that provides a total order on the objects. typedef struct obj_t obj_t; struct obj_t { int id; char* name; obj_t* next; /* points to next node */ }; /* returns 1 if the first object is smaller than the second; otherwise returns 0 */ int obj_smaller (obj_t* first, obj_t* second); obj_t* sort_list (obj_t* list) { obj_t* rval = NULL; obj_t* current; obj_t** insert; while (___________________________________) { current = list; /* explain the effect of these */ list = list->next; /* two lines for Part A */ for (___________________________________; NULL != (*insert); insert = &(*insert)->next) { if (obj_smaller (current, *insert)) { break; } } ___________________________________; *insert = current; } return rval; } The function sort_list takes a pointer to a linked list of obj_ts and returns a list sorted according to the order defined by the function obj_smaller. The original list is destroyed in the process; in other words, the obj_ts in the list are simply re-ordered using their next fields). Part A (5 points): Briefly explain the effect of the two lines marked in the code for this part of the question. Part B (15 points): Fill in the three blanks to complete the function correctly. You may not add nor remove any other code.

Page 8: HCMUT2010 Final

Page 8 Name: ____________________________________________

Problem 4, continued: typedef struct obj_t obj_t; struct obj_t { int id; char* name; obj_t* next; /* points to next node */ }; /* returns 1 if the first object is smaller than the second; otherwise returns 0 */ int obj_smaller (obj_t* first, obj_t* second); Part C (10 points): Implement the function list_is_sorted using the node structure and comparison function (replicated above from the previous page). You must not modify the list! /* Check whether or not the given list is sorted. Return 1 if the list is sorted, or 0 if it is not sorted. */ int list_is_sorted (obj_t* list) { }