What Get Printed by the Following C Code

Total Page:16

File Type:pdf, Size:1020Kb

What Get Printed by the Following C Code

page 1 Eastern Mediterranean University Computer Engineering Department CMPE-231 --- DATA STRUCTURES FINAL EXAMINATION

Lecturers:

Dr. Marifi Güler (group 1) Dr. Erden Başar (group 2) Dr. Konstantin Degtiarev (group3) January 16, 2007 Duration: 120 minutes

Name, Surname ___SOLUTION KEY______Student ID # ______Group # ______

Please, check your exam sheet and make sure that it contains 9 questions. In case of any missing pages, inform the invigilator IMMEDIATELY !

Grading:

Q1. / 10

Q2. / 15

Q3. / 10

Q4. / 10

Q5. / 10

Q6. / 15

Q7. / 10

Q8. / 10

Q9. / 10

Total

If an Answer Box is provided in a question, you MUST give your answer (and nothing else) in the corresponding box. Otherwise, you will get NO MARKS from that question !!! page 2

QUESTION 1 [ 10 points]

Consider the following C program:

#include int fun1(int, int); /* prototype of the function fun1( ) */ int main(void) { int x, y;

x =10; y = 4; printf("1st output = %d\n", fun1(x,y)); /* CASE 1 – fun1(10,4) */

x = 1; y = 4; printf("2nd output = %d\n", fun1(x,y)); /* CASE 2 – fun1(1,4) */

x = 0; y = 0; printf("3rd output = %d\n", fun1(x,y)); /* CASE 3 – fun1(0,0) */ return 0; } int fun1(int i, int j) /* definition of the function fun1( ) */ { if(i > j) return -1; else if(i == j) return 1; else return(i*fun1(i+1, j)); }

What is printed when the function fun1( ) is called as

--- FIRST CALL ------SECOND CALL ------THIRD CALL ---

fun1(10,4) fun1(1,4) fun1(0,0) 1st output = -1 2nd output = 6 3rd output = 1

QUESTION 2 [ 15 points]

Consider the following C program – in the function main( ) a (singly) linked list containing 7 nodes is created, fields f1 and f2 of each node are initialized. Please, note that both call and definition of the function traverse_LL( ) are left unfinished for the time being…

#include #include #include #include page 3 #define NN 7 typedef struct node *NODEPT; struct node { int f1; double f2; int f3[5]; NODEPT next; }; void traverse_LL(NODEPT); /* declaration of the function traverse_LL( ) – its definition is not completed yet – see TASK (B) */ int main(void) { NODEPT header,tmp, tmp1; int i = 1; for( ; i <= NN; i++) { tmp = (NODEPT) malloc(sizeof(struct node)); if(tmp == NULL) { printf("Memory was not allocated (round %d)\n", i); exit(1); } if(i == 1) header = tmp1 = tmp; else tmp1 = tmp1 -> next = tmp;

printf("\n node [%d] is allocated at the address %p", i, tmp); tmp -> f1 = NN - i; tmp -> f2 = pow(NN+1-i, 2); } tmp -> next = NULL; /* ---- see the PROGRAM’S OUTPUT */

/* ------PART related to TASK (A) ------*/ printf("\n\n Result 1: %d\n", sizeof(struct node)*NN); /* CASE 1 */ printf(" Result 2: %d\n", (header -> next) -> f1); /* CASE 2 */ printf(" Result 3: %.2f\n", tmp -> f2); /* CASE 3 */ printf(" Result 4: %p\n\n", tmp1); /* CASE 4 */ /* ------End of PART related to TASK (A) ------*/

traverse_LL( ___ ? ? ? ___ ); /* CASE 5 */ /* call to the function traverse_LL( ) – see TASK (B) */ } void traverse_LL(NODEPT pp) /* CASE 6 */ { /* definition of the function traverse_LL( ) – see TASK (B) */ int count = 0;

___ ? ? ? ___

printf(" Traversal: [%d] nodes are found\n\n", count); return; } page 4

TASK A. Consider a group of 4 (four) printf( ) statements that are marked in the program by comments /* --- CASE 1 --- */ … /* --- CASE 4 --- */. Assume that before executing the first printf( ) from this group, the program’s output looks as follows:

node [1] is allocated at the address 00C317C0 node [2] is allocated at the address 00C31760 node [3] is allocated at the address 00C31700 node [4] is allocated at the address 00C316A0 node [5] is allocated at the address 00C31640 node [6] is allocated at the address 00C315E0 node [7] is allocated at the address 00C31580

What are the exact outputs of each of marked printf( ) statements:

printf( ) STATEMENT --- EXACT OUTPUT ---

/* --- CASE 1 --- */ Result 1: ______252 ______

/* --- CASE 2 --- */ Result 2: ______5 ______

/* --- CASE 3 --- */ Result 3: ______1.00 ______

/* --- CASE 4 --- */ Result 4: ______00C31580 ______

NOTE: Please, be informed that WRONG answers will be awarded with negative grades…

TASK B. Consider function traverse_LL( ) that is NOT completed at the moment (see comments /* --- CASE 5 --- */ and /* --- CASE 6 --- */ in the code). Complete

(1) call to the function traverse_LL( ) (see /* --- CASE 5 --- */ ), and

(2) definition of the function traverse_LL( ) (see /* --- CASE 6 --- */ ) to ensure the following output:

At the address 00C317C0: value 6 At the address 00C31760: value 5 At the address 00C31700: value 4 At the address 00C316A0: value 3 At the address 00C31640: value 2 At the address 00C315E0: value 1 At the address 00C31580: value 0 Traversal: [7] nodes are found

/* --- CASE 5 --- */ traverse_LL( ______header ______); /* CASE 5 */ page 5

void traverse_LL(NODEPT pp) /* --- CASE 6 --- */ { /* definition of the function traverse_LL( ) */ int count = 0;

while ( ______pp!=NULL ______) {

/* --- CASE 6 --- */ printf(" At the address %p: value %d\n",&(pp->f1), pp->f1); count++; pp=pp->next;

} printf(" Traversal: [%d] nodes are found\n\n", count); return; }

NOTE: Please, be informed that while completing definition of the function traverse_LL( ) you MUST use names of variables (pp, count) that are shown in the function’s template, i.e. NAME CHANGES are NOT ALLOWED!

QUESTION 3 [ 10 points]

Consider two Singly Linked Lists (SLL) as shown below (X denotes NULL pointer value):

Pointers header1 (upper one) and header2 Pointer temp

What would happen if we apply the following algorithm (pseudocode) to these two lists:

1 set temp to header1 2 loop (temp link not null) 1 set temp to temp link 3 end loop (2) 4 set temp link to header2 5 if (temp not null) 1 set header2 to temp 2 set temp to null 6 end if (5) 7 loop (header2 link not null) 1 set header2 to header2 link 8 end loop (7) 9 set header2 link to header1 page 6

TASK 1. Depict (draw) the final state of both lists and pointer variable temp shown above when all steps of algorithm are done:

NOTE: Please, make your drawing accurate and unambiguously understandable (!)

TASK 2. Explain in 1-2 short sentence(s) what this algorithm does:

ANSWER BOX

This algorithm combines two Linked Lists together, thus creating a circular structure (Circular Linked List), in which pointer header1 points to the first node, whereas pointer header2 – to the last one.

QUESTION 4 [ 10 points]

Consider the following arithmetic expression (sign $ stands for the power operator) :

(A + B) $ ((C - D) * E - (F + G))

Draw the binary tree representing the expression. Give also the postorder traversal of the tree. $

+ _

A B * +

_ E F G

C D

Postorder Traversal : A B + C D – E * F G + - $ page 7 QUESTION 5 [ 10 points]

What is the output of the following program ?

#define SIZE 4 #define NULL 0 struct node { int info; Deleted location=3 and 44 struct node *link; }; Deleted location=0 and 10 typedef struct node *NODEPTR; Deleted location=1 and 22 struct queue { int items[SIZE]; 22 int front,rear; } ; 10 44 // prototypes of functions in use int delqueue(struct queue * ); NODEPTR getnode(void); int empty(struct queue *pq) {return pq->front==pq->rear?1:0;} void main(void) { NODEPTR p, head,save; struct queue q = {10, 22, 33,44}; q.front=2;q.rear=1; head = NULL; do { p = getnode( ); p ->info = delqueue(&q); p ->link = NULL; if(head == NULL) head = p; else { p->link = head; head = p; } } while (!empty(&q)); // end of do-while loop save=head; do {printf("%d\n",save->info); save=save->link; }while (save!=NULL); } // end of the function main( ) int delqueue(struct queue *pq) // definition of the function pop( ) { int x; if (empty(pq)){ printf("Queue Underflow"); exit(1); } if (pq->front==SIZE-1) pq->front=0; else (pq->front)++; x=pq->items[pq->front]; printf("Deleted Location = %d and %d\n",pq->front, x); return x ; }

NODEPTR getnode( ) { NODEPTR q; q = (NODEPTR)malloc(sizeof(struct node)); return(q); } page 8 QUESTION 6 [ 15 points]

Assume that we have the following circular sorted linked list structure already populated (created by a function createList()), with a head node head which points the address of the last node(linked field of the last node points the first node of the list).  Write an efficient function named delnode() which will delete a given name from the given linked list structure. If given name is not in the list give an error message “Name is not in the list”.

head ALI DENIZ ZAFER ......

Node and general solution structure given as follows.

struct node{ char name[15]; struct node *link; }; typedef struct node *NODEPTR; void delnode(char *, NODEPTR *); // Fill in this..

void main(void) { char given[15]; NODEPTR head,…; createList(head);/*assume that this function already created circular sorted linked list structure */ while(gets(&given) !=EOF) delnode(given , head); }

void delnode(char given[], NODEPTR *head ) // Fill in this.. { NODEPTR prev , save; prev = head; save = head->link; while (save != head) { if (strcmp(given , save->name) > 0) { prev = save; save = save->link; } else if (strcmp(given , save->name) < 0) break; else{/* faund */ if (prev->link==head) head=prev; prev->link = save->link; free(save); exit(1); } } printf(“%s is not in the list “,given); } page 9

QUESTION 7 [ 10 points]

The following code describes the linked list implementation of the queue. However, some parts of the code are left incomplete (indicated by ____ ). Fill in those incomplete parts. struct node { int info; struct node *next; } ; typedef struct node *NODEPTR; struct queue { NODEPTR front, rear; };

struct queue q;

/* Inserts item x into the queue q when called as insert(&q, x); */ void insert(struct queue *pq, int x) { NODEPTR p;

p = getnode(); /* Creates a node */ p -> info = x; p -> next = NULL; if(pq -> rear == NULL) /* empty queue */ pq -> front = p;

else _ pq -> rear -> next = p___;

__ pq -> rear = p _____; }

QUESTION 8 [ 10 points]

The Fibonacci series is defined by F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2 for n=2,3,…

The function given below calculates and returns Fn in a non-recursive manner. However, some parts of the code are left incomplete (indicated by ____ ). Fill in those incomplete parts. long int fib(int n) { long int i=0, j=1, z; for(;__n > 0 ______; n--) { z = i + j;

___ j = i ______;

___ i = z ______; page 10 } return i; } QUESTION 9 [ 10 points]

Consider the following binary tree:

A

D B

E C F

K J H

G M

a.) Give the preorder traverse of the tree. ADEKJGBCHMF

b.) Give the inorder traverse of the tree. KEGJDACHMBF

c.) Give the postorder traverse of the tree. KGJEDMHCFBA

Recommended publications