Você está na página 1de 8

ECE 190 Final Exam

HoChiMinh University of Technology


Sunday 7 August 2011

Name:

Student ID # :

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 and stdlib.h have been included in the code. Thus, library calls such
as printf, scanf , malloc, and so forth 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.
Dont panic, and good luck!

Problem 1

20 points

_______________________________

Problem 2

30 points

_______________________________

Problem 3

30 points

_______________________________

Problem 4

20 points

_______________________________

Total

100 points

_______________________________

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 (5 points): The following code has one error. Identify the error and indicate how
to fix the error.
void print_digit (int value)
{
char* name[10] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
if (0 <= value && 10 >= value) {
printf ("%s", name[value]);
}
}

Part B (5 points): The following code has one error. Identify the error and indicate how
to fix the error.
void player_free (player_t* p)
{
free (p);
free (p->name);
}

Part C (5 points): Assuming that our compiler uses 16-bit 2s complement numbers to
represent ints, give values for v1 and v2 such that v1 < v2, but the less_than
function below returns 0.
int less_than (int v1, int v2)
{
return (v1 v2 < 0);
}

Page 3

Name: ____________________________________________

Problem 1, continued:
Part D (5 points): The following code has one error. Identify the error and indicate how
to fix the error.
int* split_digits (int value)
{
int digits[12];
int cnt = 0;
if (0 <= value) {
while (10 <= value) {
digits[cnt++] = (value % 10);
value = (value / 10);
}
digits[cnt++] = value;
}
digits[cnt] = -1;
return digits;
}

Page 4

Name: ____________________________________________

Problem 2 (30 points): Two C Programs


Part A (15 points): Complete the function below to print a given string along the
diagonal 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.
Y
e
s

h
e
l
l
o

Proper Output for


s="Yes" (N=3)

E
C
E
1
9
0

Proper Output for


s="hello" (N=5)

void draw_diagonal (char* s)


{
int N, a, b;
N = strlen (s);

for (a =

}
}

; a++) {

Proper Output for


s="ECE190" (N=6)

Page 5

Name: ____________________________________________

Problem 2, continued:
Parts B, C, and D ask about the C function count below.
int count (char* s, int inside)
{
if ('\0' == *s) {
return inside;
}
if ('.' == *s) {
if (inside) {
return (count (s + 1, 0) + 1);
}
return count (s + 1, 0);
}
return count (s + 1, 1);
}

Part B (5 points): What value is returned by the following call?


count ("Hello!", 0);

Part C (5 points): What value is returned by the following call?


count ("What.does.count.count?.....", 0);

Part D (5 points): What value is returned by the following call?


count ("...A...big...red...fox...ate...a...chicken...", 10);

Page 6

Name: ____________________________________________

Problem 3 (30 points): Arrays and Stack Frames


This problem focuses on the function shown below.
int merge (int into[], int into_len, int from[], int from_len)
{
int i, j;
for (i = into_len, j = 0; from_len > j; j++) {
if (0 != from[j]) {
into[i++] = from[j];
}
}
return i;
}
int main ()
{
int base[4] = { 42, 0, 5, 10 };
int added[2] = { 0, 19 };
int sum;
sum = merge (base, 2, added, 2);
return 0;
}

Part A (10 points): Write the contents of the arrays base and added just before main
returns in the program above.
base:

added: {

_________ , __________ , __________ , __________ }

_________ , __________ }

Page 7

Name: ____________________________________________

Problem 3, continued:
Part B (20 points): The diagram below shows the state of the stack just before the
function merge returns (before execution of the return statement) in the program on
the previous page. Most of merges stack frame and some of mains stack frame have
been labeled, and some of the values have been filled in for you.
Fill in the rest of the stack frame for merge by writing both the values and the meanings
for the remaining locations into the boxes. For example, you need to write in the values
of the parameters passed to merge. If you can not know the value at an address or the
meaning of the value stored there, write unknown. Blank boxes will not receive points.
xC771

R6

xC772

R5

xC773

previous frame pointer

xC774

x3443

xC775

return address
return value

xC776
xC777
xC778
xC779
xC77A
xC77B
xC77C

x0013

xC77D

x0002

xC77E (unknown)
xC77F

42

(unknown)
base[0]

xC780

base[1]

xC781

base[2]

xC782

base[3]

xC783

added[0]

xC784

19

added[1]

xC785

sum

Page 8

Name: ____________________________________________

Problem 4 (20 points): Linked Lists


This problem makes use of a cyclic, doubly-linked list as discussed in lecture and in
discussion section. Each list element points to an object (the contents of this object are
not necessary to the problem) and to the previous and next elements of the list. A
sentinel (with obj field equal to NULL) serves as the starting point for a list.
The function reverse_copy creates a copy of a list in reverse order. The parameter
from points to the sentinel element for the original list, and the parameter to points to
the sentinel element for the list being created. The new lists elements must be allocated
using malloc, and should point to the same objects as do the elements of the old list.
The function should return 1 on success and 0 on failure.
Fill in the five blanks to complete the function correctly. You may not add nor remove
any other code.
void* malloc (size_t bytes); /* dynamically allocate memory */
typedef struct object_t object_t;
typedef struct list_elt_t list_elt_t;
struct list_elt_t {
object_t*
obj;
/* object referenced by list element */
list_elt_t* prev;
/* previous list element
*/
list_elt_t* next;
/* next list element
*/
};
int reverse_copy (list_elt_t* from, list_elt_t* to)
{
list_elt_t* cur_elt;
list_elt_t* tail;
list_elt_t* new_elt;
tail = to;
for (cur_elt = from->prev; ___________________________ ;
cur_elt = cur_elt->prev) {
new_elt = _______________________________ ;

if (NULL == new_elt) { _____________________ ; }


new_elt->obj = cur_elt->obj;
tail->next = new_elt;
new_elt->prev = tail;
tail = new_elt;
}
tail->next = __________________;

to->prev
return 1;
}

= __________________;

Você também pode gostar