






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The final exam for the ece 199 course at the university of waterloo, fall 2003. The exam covers various topics in computer engineering, including programming, logic structures, recursion, i/o, and c language. It consists of five problems, each worth 20 points, and includes instructions for the exam takers.
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Part A (8 points): Briefly describe the error that this program makes. The comment reflects what the program is supposed to do. Note that there are no syntax errors.
/* Copy array a to array b. Each has 25 elements. */ i = 0; while( i++ < 25 ) b[ i ] = a[ i ];
Part B (12 points): Using the basic logic structures discussed in lecture, design a circuit to convert an 8-bit 2's complement number to its absolute value (also an 8-bit 2’s complement number).
Part A (12 points): Write a function to evaluate a parse tree that contains only integer operands and the multiplication and addition operators. Your function must return the result of the expression. For example, the following parse tree should produce the result: (6 + 2) * 3 = 24
typedef struct node Node; struct node { Node *left, right; int type; / 0 = Operator, 1 = Operand / int value; / If type is 0, then 0 = Addition, 1 = Multiplication If type is 1, then value is just the operand's value */ };
int Evaluate( Node* root ) {
}
Problem 3, continued:
Part B (8 points): The function below reads an expression from a file and builds a tree structure for the expression. The function takes a file and a pointer to a place in which the root node of the resulting expression can be stored. The function returns 0 on success and -1 on failure. For simplicity, the function does not properly free dynamically allocated memory; ignore this problem. Read the function, then write the contents of a file representing the expression: ( 2 * (4 * 3) ) + 18.
typedef struct node Node; struct node { Node *left, right; int type; / 0 = Operator, 1 = Operand / int value; / If type is 0, then 0 = Addition, 1 = Multiplication If type is 1, then value is just the operand's value */ };
int parse_expression_file( FILE *file_in, Node **n ) { char buf[200]; int num;
(n) = (Node ) calloc( 1, sizeof( Node ) ); if( n == NULL || fgets( buf, 200, file_in ) == NULL ) { return -1; } if( sscanf( buf, "%d", &num ) == 1 ) { (n)->type = 1; (n)->value = num; return 0; } (n)->type = 0; if( buf[0] == '+' ) { (n)->value = 0; } else if( buf[0] == '' ) { (n)->value = 1; } else { return -1; } if( parse_expression_file( file_in, &(n)->left ) == -1 || parse_expression_file( file_in, &(*n)->right ) == -1 ) { return -1; } return 0; }
Problem 4, continued:
*****Part B** (10 points):
typedef struct node Node; struct node { int value; Node *next; };
void FunctionB( Node *head, Node **headA ) { Node *temp = head; Node **tempA = headA; int num;
tempA = NULL; if( head != NULL ) { num = temp->value; tempA = (Node ) malloc( sizeof( Node ) ); (tempA)->value = num; (tempA)->next = NULL; tempA = &((tempA)->next);
while ( temp != NULL ) { if ( temp->value > num ) { num = temp->value; tempA = (Node ) malloc( sizeof( Node ) ); (tempA)->value = num; (tempA)->next = NULL; tempA = &((*tempA)->next); } temp = temp->next; } } }
Part A (12 points): Write a function that takes two lists of numbers, each of which is sorted in increasing order, and determines if the first list is a subset of the second list, returning 1 if this relationship holds, and 0 otherwise. (Recall that a list is a subset of a second list if the second list contains all elements in the first list.) You may use either a recursive or iterative style, but your function must take advantage of the fact that the lists are sorted for full credit.
typedef struct node Node; struct node { int value; Node *next; };
int sorted_list_is_subset( Node *sub, Node *super ) {
}