ENEE150 Final Exam Review Topics: Pointers -- pointer definitions & initialization -- call-by-reference and Call-by-value -- pointer with arrays -- NULL pointer, void pointer, and pointer to pointer Strings -- string defined with pointer -- string manipulation functions: string searching, string comparison, string copy, string concatenation, string tokenizing Dynamic memory allocation (malloc, free, calloc, realloc) Structure -- structure template and variable declaration -- typedef -- Access members (using dot or arrow) -- nested structures Linked List -- understand pointer to pointer sptr -- understand how to insert/delete a node, and display the list. -- stack: push and pop (LIFO) -- queue: enqueue and dequeue (FIFO) ADT -- definition of ADT, advantage of using ADT -- be able to give the abstract view of an ADT given specifications (.h file) -- understand concept of constructor and deconstructor Review Problems: (Please review all the practice problems in the midterm review sheet) 1. What is displayed by the following program segment if all variables are of type int and the input data is the data shown? st = scanf("%d%d%d", &n, &m, &p); printf("n=%d m=%d st=%d\n", n, m, st); Input data: 10 12 hello 2. What will be the output of the program? #include<stdio.h> 1
int x=30, *y, *z; y=&x; /* Assume address of x is 500 and integer is 4 bytes */ z=y; *y++ = *z++; x++; printf("x=%d, y=%d, z=%d\n", x, y, z); 3. Given the following declarations typedef struct char name[20]; int id; double salary; emprec_t;... emprec_t emp1, emp2; char ch1; int flag1, flag2; (1) Which of the following could not be a valid statement? a. emprec_t.id = 87654; b. emp1.salary *= 1.05; c. ch1 = emp2.name[0]; d. printf("%.2f", emp1.salary); e. All of the above are valid. (2) Which of the following could not be a valid use of the variables shown? You may assume that print_employee is a user-defined function. a. emp2 = emp1; b. flag1 = emp1 == emp2; c. flag2 = emp1.id == emp2.id; d. print_employee(emp1); e. All of the above are valid. 4. Given: typedef struct node_s double data; struct node_s *leftp, *rightp; node_t; 2
The name node_t is equivalent to. a. node_s * b. node_s c. struct node_s * d. struct node_s 5. Define a type named node_t that is appropriate for one node of a linked list in which each node's data consists of an integer and a 6-element array of type double. 6. If a linked list contains three nodes with values "him", "her", and "its", and start is a pointer to the list head, assume the following data structure, typedef struct node char pronoun[20]; struct node *next; Node; Node *np, *mp; What is the effect of each of the following statements? Describe how many nodes in the list and what the values of each node are. Each case is independent of each other. (1) np = start -> next; strcpy(np -> pronoun, "she"); (2) mp = start -> next; np = mp -> next; mp -> next = np -> next; free (np); (3) np = start; start = (Node *) malloc (sizeof (Node)); strcpy( start -> pronoun, "his"); start -> next = np; 7. Write a function that merges two linked lists. Assume the following data structure for the list: typedef struct node int num; struct node *next; NODE; 8. Give the output for the program below: #include <stdio.h> #include <stdlib.h> #define MAX 3 void init(int a[]) a[0] = 23; 3
a[1] = 56; a[2] = 17; void increase(int *p) p++; printf("%d\n", p[0]); int numbers[max], *other; init(numbers); other = (int*) malloc(max*sizeof(int)); init(other); increase(numbers); printf("%d\n", numbers[0]); other++; printf("%d\n", other[0]); 9. Write a function prototype for a function sum_n_avg that has three type double input parameters and two output parameters. n1 n2 n3 sum_n_avg sump avgp 10. Given the definition typedef struct stack_node_s char element; struct stack_node_s *next; stack_node_t; Draw the stack resulting from execution of the following code segments step by step. Assume you are working with a linked list implementation of a stack of individual characters. stack_node_t *sptr = NULL; push(&sptr, a ); push(&sptr, b ); pop(&sptr); push(&sptr, c ); 4
11. Given the definition in 10, write a push function to insert a character to the top of the stack (i.e. to the beginning of the linked list) void push (stack_node_t **sp, char c); 12. What will be the output of the following program if the starting address of x is 100, and the starting address of array a is 200? #include <stdio.h> int *p, x; int a[5]=100,200,300,400,500; int *p2; p=null; x=500; p=&x; printf("1) %d %d %u %u\n",x,*p,p,&x); p2=a; *(p2+1)=*p; *p= *p2 + *(p2+2); p2++; printf("2) %d %d %d %u\n",x,*p,*p2,p2); 13. What will be the output of the following program? #include<stdio.h> char *a[4]= "MC", "C Programming", "Data", "Structure"; char *p; int i; for (i=0;i <4;i++) printf("%s \n", a[i]); p = a[0]; printf("%c \n", *p); p=p+1; printf("%c \n", *p); p=&a[2][0]; printf("%c \n", *p); p=p+1; printf("%c \n", *p); 5
14. What will be the output of the following program? #include<stdio.h> struct course int courseno; char coursename[25]; ; struct course c[] = 102, "Java", 103, "PHP", 104, "DotNet" ; printf("%d ", c[1].courseno); printf("%s\n",(c+2)->coursename); 6
Review Problem Solution 1. n=10 m=12 st=2 2. x=31, y=504, z=504 3.1) a 2) b 4. d 5. typedef struct node_s int num; double list[6]; struct node_s *restp; node_t; 6 (1) It replaces "her" by "she". So we still have three nodes with values "him", "she", and "its". (2) The third node in the list is deleted. We only have two nodes with values "him" and "her". (3) A new node with value "his" is inserted in the beginning of the list. We will have four nodes with values "his", "him", "her", and "its". 7 void merge(node **first, NODE **second) NODE *end,*start; /* Find the current pointer to two lists */ end = *first; start = *second; /* Find tail of the first list */ while ( end -> next!= NULL ) end = end ->next; /* Tail of the first list points to the head of the second list */ end -> next = start; 8. Output of the program is: 56 23 56 9. void sum_n_avg (double n1, double n2, double n3, double *sump, double *avgp); 7
10. b c a a a a 11. void push (stack_node_t **sp, char c) stack_node_t *newptr; newptr =(stack_node_t *) malloc(sizeof(stack_node_t)); newptr -> element = c; newptr -> next = *sp; *sp = newptr; 12. 1) 500 500 100 100 2) 400 400 500 204 13. MC C Programming Data Structure M C D a 14. 103 DotNet 8