SPR 08 exam2_solution

SPR 08 exam2_solution - WARM / March 27, 2008 Name ECE264...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: WARM / March 27, 2008 Name ECE264 Advanced C Programming Exam 2 Solve the following problems. The number of points for each problem is shown next to the prob— lem and in the table below. The outcomes corresponding to each question are also shown. Use only the space provided to solve each problem. Problem Points Outcome 1 / 50 3 2 (a) / 10 (b) / 15 3 (a) / 15 (b) / 10 Total r / 100 Problem 1 (50 points) The following program prints the data stored in a linear linked list every time the function printlistO is called. The calls to printlist() are marked with asterisks. Specify what the program will print. Note that there are 10 calls to printlist() during the runtime of the program. Specify what the function will print in every call. #include <stdio.h> #include <stdlib.h> #include <assert.h> #define N 5 struct list { int data; struct list *next; }; int main(void) { int 1; struct list *head; struct list >“insertlist(struct list *, int); struct list >"alldeletelist(struct list *); void printlist(struct list *); head=NULL; for(i=l ;i<=N;i++) { head=insertlist(head,i); printlist(head); }; alldeletelist(head); printf("\n\n"); } void printlist(struct list *head) { struct list *p; printf("\nlist: "); f0f(p=head;p!=NULL;p=p—>next) printf(" %d",p->data); } struct list *insertlist(struct list *head, int data) { struct list >*‘p; p=malloc(sizeof(struct list)); assert(p !=NULL); p—>data=data; p->next=head; head=p; 1'etu1'n(head); } struct list *alldeletelist(struct list *head) { struct list *p,*q; while(head !=NULL) { if(head->next==NULL) { free(head); head=NULL; } else { for(p=head;p—>next ! =NULL;p=p->next) (1:13; fl'ee(P); I q—>next=NULL; }; printlist(head); /**************/ } return(head); } Problem 2 (25 points) This problem deals with programs that create sorted binary trees. (a) The following program is similar to the one we saw in class. Specify what this program will print. #include <stdio.h> #include <stdlib.h> #include <assert.h> struct node { int data; struct node *left; struct node *right; }; int main(void) { Int i; int data={5,3,10,2,3,4,8,12,8 },ndata=9; struct node *I‘OOt; void inorder(struct node *); struct node >*‘addnewnode(struct node *, int); root=NULL; for(i=0;i<ndata;i++) root=addnewnode(root,data[i]); printf("\n\ntree elementsz"); inorder(root); printf("\n\n"); struct node *addnewnode(struct node * root, int data) { struct node *p,*pnew,*newnode(int); pnew=newnode(data); if(root==NULL) { root=pnew; 1‘eturn(root); }; for(p=root;1;) { if(data<p->data) { if(p—>left==NULL) { p—>left=pnew; 1‘etu1‘n(root); } ; p=p->left; } else if(data>p->data) { if(p->right==NULL) { p~>right=pnew; return(root); } ; p=p~>right; } else { free(pnew); return(root); } ; } ; } struct node *newnode(int data) { struct node *p; p=malloc(sizeof(struct node)); asse1t(p !=NULL); p->data=data; p—>left=NULL; p->right=NULL; return(p); } void inorder(struct node >*‘root) { if(root!=NULL) { inorder(root->left); printf("\n%d",root—>data); inorder(root->right); 6 k _ W‘em ‘ } UL } Z The program will print: (3063wa (b) The function addnewnodeO in the program above was changed as shown below. The changes are marked with asterisks. No other changes were made. Specify what the program from part (a) will print with the modified function addnewnode(). struct node *addnewnode(struct node * root, int data) { struct node *p,*pnew,*newnode(int); pnew=newnode(data); if(root==NULL) { root=pnew; return(root); }; for(p=root;1;) { if(data>p—>data) /*’"*’*< changed ****/ { if(p—>1eft==NULL) { p—>left=pnew; return(1‘00t); }; p=p~>1eft; } else if(data<p->data) / changed >“>"**/ { if(p—>right==NULL) { p—>right:pnew; return(root); }; p=p—>right; } else { free(pnew); return(root); }; }; } The program will print: i Problem 3 (25 points) The following program creates a linked structure from elements of type struct elem. #include <stdi0.h> #include <stdlib.h> #include <assert.h> #define NNEXT 4 struct elem { int data; struct elem >knext[NNEXT]; }; int main(void) { Int 1; struct elem *root; void preorder(struct elem *); struct elem *createlistO; r00t:createlist(); printf("\n\nlist elementsz"); preorder(root); printf("\n\n"); -10- struct elem *CreatelistO { int data={5,3,10,2,3,4,8,12,8},ndata=9; int i,j,found; struct elem *root,*p,*pnew,*newelem(int); r00t=NULL; for(i=0;i<ndata;i++) { pnew=newelem(data[i]); if(1‘oot==NULL) { root=pnew; printf("\ninserting data=%d root",data[i]); } else for(p=root,found=0; lfound;) { j=rand()%NNEXT; printf("\ninserting data=%d j=%d",data[i],j); if(p->next[j] !=NULL) p=p~>next[j]; else { p—>next[j]=pnew; found=1; }; }; }; retu1‘n(root); } struct elem *newelem(int data) { int i; struct elem *p; p=malloc(sizeof(struct elem»; assert(p !=NULL); p—>data=data; f01‘(i=0;i<NNEXT;i++) p—>next[i]=NULL; 1'eturn(p); -11- void preorder(struct elem *root) { int i; if(root ! =NULL) { printf("\n%d",1‘oot—>data); for(i=0;i<NNEXT;i++) preorder(root->next[i]); } } The output of the program is as follows: inserting data=5 root inserting data=3 j=2 inserting data=10 j=2 inserting data=lO j=l inserting data=2 j=3 inserting data=3 j=3 inserting data=3 j=3 inserting data=4 j=2 inserting data=4 j=3 inserting data=8 j=0 inserting data=12 j=2 inserting data=12 j=1 inserting data=12 j=3 inserting data=8 j=0 inserting data=8 j=0 list elements: [the rest of the output is omitted] _12_ (a) Draw the linked structure that the program creates. Represent an element of the structure as follows: (b) Specify what the program will print after the words "list elementsz” fist q/Qmwfit I S% 7) 3 \0 lZ “~23 (mew Jew: We ...
View Full Document

Page1 / 12

SPR 08 exam2_solution - WARM / March 27, 2008 Name ECE264...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online