Академический Документы
Профессиональный Документы
Культура Документы
Name:
Student ID # :
Problem 1
30 points
_______________________________
Problem 2
30 points
_______________________________
Problem 3
20 points
_______________________________
Problem 4
20 points
_______________________________
Total
100 points
_______________________________
Page 2
Name: ____________________________________________
}
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
Part A (10 points): Write the contents of the structures one, two, and three just before
main returns in the program above.
one:
two:
three:
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
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: ____________________________________________
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 ) );
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: ____________________________________________
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;
}