You are on page 1of 9

ECE 190 Final Exam

HoChiMinh University of Technology


Friday 17 July 2009

Name:

Student ID # :

Be sure your exam booklet has 9 pages (5 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.
Dont panic, and good luck!

Problem 1

20 points

_______________________________

Problem 2

20 points

_______________________________

Problem 3

20 points

_______________________________

Problem 4

20 points

_______________________________

Problem 5

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 (4 points): Write the output of the following code.
int i = 0;
printf ("i");

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 increment_value (int value)
{
value++;
}

Part C (8 points): The following code has two errors. Identify both errors, state whether
or not each will be caught by the compiler, and indicate how to fix each error.
int sum_of_squares (int N)
{
/* return sum of squares from 1 to N */
int sum = 0
int i;
for (i = N; i-- > 0; ) {
sum += i * i;
}
return sum;
}

Page 3

Name: ____________________________________________

Problem 1, continued:
Part D (4 points): A given call to a C function named mystery generated the stack
frame shown and memory below. Based on the information shown in the stack frame, fill
in the declaration for the function mystery.
x6178
x6179
x617A

'H' (x0048)
'i' (x0069)
NUL (x0000)

xDEF0
xDEF1
xDEF2
xDEF3
xDEF4
xDEF5
xDEF6
xDEF7

i = 42
j = x6179
previous frame pointer
return address

_____________ mystery (

return value (float)


a = x6178
b = 2

);

Page 4

Name: ____________________________________________

Problem 2 (20 points): A Simple C Program


Complete the function below to print a right triangle with sides of size N, as shown in the
figure below. In the figure, blank spaces ( ) are represented by hyphens (-) for
clarity. Assume that N is a positive integer.

--*
-**
***

Proper Output for N=3

----*
---**
--*-*
-*--*
*****

Proper Output for N=5

void draw_triangle (int N)


{
int a, b;
for (a = 0;
; a++) {

}
}

-----*
----**
---*-*
--*--*
-*---*
******

Proper Output for N=6

Page 5

Name: ____________________________________________

Problem 3 (20 points): Strings and Dynamic Allocation


You must write a string tokenizer function, which takes a string and a separator character
as parameters and returns a dynamically-allocated array of substrings delimited by the
separator value, also known as tokens. The function creates the array of pointers to the
substrings in the original string by turning the separator values in the original string into
NUL characters. For example, if the parameters are the string mississippi and the
separator character s, the function tokenizer returns the following array.

NOTE: The second and fourth pointers point to empty strings (#characters = 0)

Fill in the blanks in the function on the next page to complete the tokenizer function.
You may or may not need all the lines provided to you. Assume that the original string
is not an empty string.

(the function is on the next page)

Page 6

Name: ____________________________________________

Problem 3, continued:
char** tokenize (char* str, char separator)
{
int

= 0;

char* ptr = str;


/* Declare any additional variables that you need here. */
____________________________________________
____________________________________________

/* Count the number of separators in the string. */


while ('\0' != *ptr) {
____________________________________________
____________________________________________
____________________________________________
}

/* Allocate the array of tokens.

Call it token_array. */

____________________________________________________

/* Populate array of tokens; turn separators into '\0'. */


token_array[i++] = str++;
while ('\0' != *str) {
if (separator == *str) {
____________________________________________
____________________________________________
____________________________________________
}
str++;
}

/* Set last location in array to NULL. */


token_array[i] = NULL;
/* Return the array of tokens. */
return token_array;
}

Page 7

Name: ____________________________________________

Problem 4 (20 points): Linked Lists


This problem makes use of a doubly-linked list based on the structure below, as
illustrated by the example figure.
typedef struct node_t node_t;
struct node_t {
node_t* prev; /* points to previous node */
node_t* next; /* points to next node
*/
char id;
};

prev=NULL

next
id

prev
next
id

prev
next
id

prev
next=NULL

id

start
void remove (node_t* start, char rmID)
{
node_t* current;
node_t* temp;
while (1) {
temp = current->next;
if (rmID == current->id) {
if (NULL != current->prev) {
current->prev->next = temp;
}
if (NULL != temp) {
temp->prev = current->prev;
}
free (current);
return;
}
current = temp->next;
}
}

Part A (10 points): The function remove takes a node_t pointer start and a character
rmID. The parameter start points to the first node in a doubly-linked list. The function
is supposed to remove every node with id field equal to rmID from the list, but the
function given does not behave as intended. Changing at at most three lines, correct
the code above. To modify a line, cross the line out and write a new version to the right
(or write /* blank line */ if the line is simply unnecessary).

Page 8

Name: ____________________________________________

Problem 4, continued:
Part B (10 points): Implement the function list_length using the node structure from
the previous page. You must not modify the list!
/* Return the number of nodes in the list pointed to by start. */
int list_length (node_t* start)
{

Page 9

Name: ____________________________________________

Problem 5 (20 points): File I/O


The function below reads data from a file, processes it, and writes the result back to
another file. Each line in the input file contains a non-negative integer and a character.
The last line of the file contains the number -1. Use the code below to answer the
questions. Assume that p_data has been allocated using calloc with enough space
to fit all data in the input file and a few extra characters.
00 int func (const char* i_name, const char* o_name, char* p_data)
01 {
02
FILE* in_f;
03
FILE* out_f;
04
char temp_str[2];
05
int
temp_num;
06
07
in_f = fopen (i_name, "rb");
08
while (1) {
09
fscanf (in_f, "%d%1s", &temp_num, temp_str);
10
if (-1 == temp_num) { break; }
11
p_data[temp_num] = *temp_str;
12
}
13
fprintf (out_f, "-*-*\n");
14
fprintf (out_f, "%s", p_data);
15
fprintf (out_f, "-*-*\n");
16
return 0;
17 }

Part A (8 points): The output file is never written. Why not?

Part B (12 points): Given the input file in the table below, and assuming that the program
is fixed to read/write the output file, write the contents of the output file.
Input File
Line Contents
4 m
0
6 r
1
5 e
2
0 S
3
2 m
4
1 u
5
-1 !
6

Output File
Line
0
1
2

Contents

You might also like