Вы находитесь на странице: 1из 8

ECE 190 Final Exam

HoChiMinh University of Technology


Tuesday 14 August 2012

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

30 points

_______________________________

Problem 2

30 points

_______________________________

Problem 3

20 points

_______________________________

Problem 4

20 points

_______________________________

Total

100 points

_______________________________

Page 2

Name: ____________________________________________

Problem 1 (30 points): Two C Programs


Part A (15 points): Write the rest of the C function below to count the number of pairwise
matches in an array of integers. For example, if the array contains the values {0, 5, 3, 2, 3,
5}, your function should return 2 (the two 5s match, and the two 3s match). If the array
contains the values {7, 7, 0, 7}, your function should return 3, since there are three distinct
pairs of 7s in the array.
Use two loops to implement your function. Do not attempt to sort the array.
int count_matches (int array[], int num_values)
{
int N, a, b;
N =
for (a =

}
return N;
}

; a++) {

Page 3

Name: ____________________________________________

Problem 1, continued:
Parts B, C, and D ask about the C function seek below.
char* seek (char* s, char* t, int n)
{
if ('\0' == t[n]) { return s; }
if ('\0' == s[n]) { return NULL; }
if (t[n] != s[n]) {
return seek (s + 1, t, 0);
}
return seek (s, t, n + 1);
}

Part B (5 points): Assuming that string s starts at address x4000, what value is returned by
the following call?
seek ("bat bate bet!", "ate", 0);

Part C (5 points): Assuming that string s starts at address x4000, what value is returned by
the following call?
seek ("applesauce", "Banana", 5);

Part D (5 points): Briefly explain what the function does when called with n equal to 0.

Name: ____________________________________________

Page 4

Problem 2 (30 points): Structures and Stack Frames


This problem focuses on the structure and function shown below.
typedef struct complex_t complex_t;
struct complex_t {
int real;
int imag;
};
/* multiply complex numbers A and B; store the product in C */
void complex_mult (complex_t* A, complex_t* B, complex_t* C)
{
complex_t product;
product.real = A->real * B->real A->imag * B->imag;
product.imag = A->real * B->imag + A->imag * B->real;
*C = product;
return;
}
int main ()
{
complex_t one, two, three;
one.real = 10;
one.imag = 2;
two.real = 5;
two.imag = 8;
complex_mult (&A, &B, &C);
return 0;
}

Part A (10 points): Write the contents of the structures one, two, and three just before
main returns in the program above.

one:

real: _________ , imag: __________ }

two:

real: _________ , imag: __________ }

three:

real: _________ , imag: __________ }

Page 5

Name: ____________________________________________

Problem 2, continued:
Part B (20 points): The diagram below shows the state of the stack just before the function
complex_mult returns (before execution of the return statement) in the program on the
previous page. Most of complex_mults 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 diagram below 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 complex_mult. 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.
xC675

R6

xC676

R5

xC677
xC678

previous frame pointer


x3443

xC679

return address
return value

xC67A
xC67B
xC67C
xC67D
xC67E

10

xC67F

xC680 (unknown)

(unknown)

xC681

three.real

xC682

three.imag

xC683

two.real

xC684

two.imag

xC685

10

one.real

xC686

one.imag

xC687

Page 6

Name: ____________________________________________

Problem 3 (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 do_transform replaces each of the dynamically allocated objects with a
transformed object. The transformed object is produced by calling transform_object on
the current object and a newly allocated object. If the call to transform_object returns
any value other than 0, the node is instead deleted from the linked list, and all associated
objects as well as the list node must be freed. 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.
(The code is on the next page.)

Page 7

Name: ____________________________________________

Problem 3, continued:
void* malloc (size_t bytes); /* dynamically allocate memory */
/* Although the object structure is not shown here,
assume that the compiler has it. */
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 do_transform (list_elt_t* head)
{
list_elt_t* cur_elt;
object*
new_obj;
list_elt_t* elim_elt;
tail = to;
for (cur_elt = head->next; ___________________________ ;
cur_elt = cur_elt->next) {
new_obj = malloc ( sizeof ( *new_obj ) );

if (NULL == new_obj) { _____________________ ; }


if (transform_object (cur_elt->obj, new_obj)) {

cur_elt->prev->next = _____________________;
cur_elt->next->prev = cur_elt->prev;

elim_elt = _______________________;
cur_elt = cur_elt->prev;
free (elim_elt->obj);
free (elim_elt);
free (new_obj);
continue;
}
______________________________;
cur_elt->obj = new_obj;
}
return 1;
}

Page 8

Name: ____________________________________________

Problem 4 (20 points): Debugging Exercises


Be concise: if your answer contains more than 10 or 15 words or a simple picture, it is
probably wrong.
Part A (10 points): The following code has two errors. For each of the two errors, identify
the error and indicate how to fix the error.
/* Returns 1 if the value is prime, or 0 otherwise. */
int is_prime (int value)
{
int divisor;
for (divisor = 1; value > divisor; divisor++) {
if (value == value * divisor / divisor) {
return 0;
}
}
return 0;
}

Part B (5 points): The following code has one error. Identify the error and indicate how to
fix the error.
/* Allocates a new player structure and initializes it. Returns
* NULL on failure, or a pointer to the new structure on
* success. */
player_t*
create_player_structure (char* nm, char* passwd, char* profile)
{
player_t p;
/* player_init is the version used in class; it returns 1 on
success, and 0 on failure. */
if (!player_init (&p, nm, passwd, profile)) { return NULL; }
return (&p);
}

Part C (5 points): The following code has one error. Identify the error and indicate how to
fix the error.
/* Returns 1 if
int is_odd (int
{
int mod_two
if (mod_two
return 0;
}

the value is odd, or 0 otherwise. */


value)
= (value % 2);
= 1) { return 1; }

Вам также может понравиться