You are on page 1of 5

End Semester Exam: Programming and Data Structures, Section 2; Autumn 2012 Date Nov 24th, 2012; Duration

3 hrs

Note: Please ensure that the answers have the appropriate question number, ie 1, or 4 a. etc) Please do not scribble or scratch, if it is difficult to read, marks will be deducted. All programs should be accompanied by an algorithm or flowchart, 15% credit will go to this, the main purpose of this is to ensure that your program has been thought through logically and perhaps correctly , there are 10 Bonus marks here (specified in bold) !

1.

Write the algorithm to convert an infix expression to postfix (just the algorithm, not code, should contain all logic you could augment this with a flow chart optionally, but must show how it works on ((15*3)/((3*1)+(5/1))) !

[5 marks] 2. Convert to the following missing bases: Decimal 36 Binary 1001000 154 90 Do you notice any patterns? (need to answer this, for that 1 extra mark) [5 marks] Hex Octal

3. (Please provide code snippets, do not say Yes/No this is not JEE! & several repeats here!) a. Define a self referential struct? And use the typedef synonym mechanism to support it. b. Use malloc() to assign memory and read in a value into a pointer to a struct, with one integer data type member and a string member? c. Provide a code snippet to determine the length of a string using pointers and no counter? d. Provide a code snippet to compare 2 strings using pointers, and return which is greater or less? e. Provide code snippets to traverse to the a[i] element of a struct array, using a pointer that is pointing to the first element, assume the pointer is *ptr, and print out its members value, assume each struct contains a string member called name (which is a character array) [10 Grace marks!]

4.

Output Interpretations (please attempt to read the code carefully!) ,

IIT BBS , Section 2, PDS Autumn 2012, End Sem Exam

Page 1

[10 marks]
a. (Test with a value of n=2)
// all about scope #include<stdio.h>

void setValue(int*); void printLoop(int x);

int n=5; int main() { int n=0; setValue(&n); printLoop(n); getchar(); // wait } void setValue(int *n) { printf("Enter value for n\n"); scanf("%d", n); getchar(); // consume new line if (*n <5) *n+=5; } void printLoop(int x) { int i=0; for(i=n; i<x; x--) printf("%d", i++); }

b.
#include <stdio.h> struct { int i; float f; }var; int main() { var.i=5; var.f=9.0001; printf("%.2f",var.i/var.f); getchar(); // wait

IIT BBS , Section 2, PDS Autumn 2012, End Sem Exam

Page 2

return(0); }

c.
#include<stdio.h> #include<string.h> char str1[80];

int main() { strcpy(str1,"Earth is 108 times moons diameter from earth-; and the sun is 108 time the earths diameter from earth; is it true ?"); printf("%s\n", strtok(str1, ";")); printf("%s\n", strtok(NULL, " ")); printf("%s\n", strtok(NULL, ";")); printf("%s\n", strtok(str1, " ")); getchar(); }

d.
#include<stdio.h> // remember contiguous allocations struct { int i,val[25]; }var={1,2,3,4,5,6,7,8,9},*vptr=&var; int main() { printf("%d %d %d\n",var.i,vptr->i,(*vptr).i); printf("%d %d %d %d %d %d",var.val[2],*(var.val+3),vptr ->val[4],*(vptr->val+4),(*vptr).val[4],*((*vptr).val+1)); getchar(); return(0); }

e.
#include<stdio.h> // remember padding rules struct A { int i; float f; union B { char ch; double j; }temp; }temp1; int main() { struct A temp2[5];

IIT BBS , Section 2, PDS Autumn 2012, End Sem Exam

Page 3

printf("Size of temp2 is %d",sizeof(temp2)); getchar(); return(0); } Hint: Assume 4 bytes for int..

5 a. Write a program to implement a traditional queue, supporting, enqueue, dequeue, and show operations, use boundary conditions, and used the linked list method to implement the same. [10 marks] 5 b. Write a recursive function to calculate the Greatest Common Denominator (GCD) using only differences between the 2 numbers. (If done correctly, this would add another 5 to your score --- BONUS ) Algorithm or flow chart for that 15% credit! [5+5 marks] 6. a. a. Which sorting technique needs to scan through the entire array and select the current minimum required as you reduce the array size using invariants? (a) Bubble Sort (b) Selection Sort (c) Insertion Sort b. The number of moves required for sorting an array using the Bubble Sort algorithm is (a) O (n2) (b) O (Log (n) (c) O (n) c. The technique which involves taking advantage of a presorted sequence is called (a) Bubble Sort (b) Selection Sort (c) Insertion Sort d. Factors that help in deciding the sorting algorithm to be used are (a) Memory (b) Performance (c) Flexibility (d) All e. The dis-advantage of binary search is (a) Complexity (b) Needs a sorted list (c) Takes more time (d) I am lost! [5 marks] b. Write a program to dynamically allocate a 1 dimensional array (n), using malloc(), and use a pointer to this structure to assign random values from 1 to n, and print these values, and use the sequence sort technique to sort the array in ascending order, use of rand vs srand(time(NULL)) instead of an arbitrary fixing of a sequence of values will be given more credit. The random assignment need not be uniform! n to be obtained from user. Write the algorithm or flow chart for 15% credit! [10 marks ]

7. Write a routine for quick sort, remember to use 2 counters, and a pivot value, and keep swapping if required until the right pointer is less than the right pointer, at which point, you will call the same routine with 2 smaller ranges. This was shown to you in detail in the class! Write the algorithm or flow chart for 15% credit! [10 +5 marks]

8. a

IIT BBS , Section 2, PDS Autumn 2012, End Sem Exam

Page 4

1. The_________ of a vertex of an undirected graph is the no of edges incident plus the edges emanating from the vertex (a) degree (b) order(c) height 2. A n-vertex undirected graph with n*(n-1) edges is a_________ graph (a) subgraph (b) full graph (c) complete graph 3. If a graph has at least one direction edge it is also called (a) bigraph (b) digraph(c) directional graph (d) non IIT graph 4. An adjacency matrix of a directed graph is: (a) symmetric(b) asymmetric (c) could be either 5. The method of starting at a vertex and identifying all vertices reachable from each of its children recursively is called (a) Depth first search (b) breadth first search(c) Horizontal search [5 marks] b. 1. How do you define an algorithm, and what does its complexity depend on? 2. What is the best parameter to uniformly define the complexity of an algorithm? 3. What are the 3 measures of complexity; provide an example that illustrates all 3 measures? 4. Write a code snippet that shows that the order of complexity (worst case) that is O(3n^2 +3n +K), where K is a constant. 5. Will the shape of the complexity or time taken to perform a function for the same algorithm be similar across computers? If yes or no, explain [10 marks] c. Qualitatively explain why bubble sort is worse than insertion sort in terms of complexity as it involves the Big Oh, (ie ) in terms of the Big Oh (O) notation! [5 marks] 9. a 1. A binary tree with n nodes, n > 0, has exactly edges. (a) n (b) n - 1(c) n - 2 2. A binary tree of height h, h >= 0 has at least_________ elements in it (a) h (b) h 1 (c) h - 2 3. A binary tree of height h, h> 0 has at most_________ elements in it (a) 2 ^ h (b) 2 ^ h 3 (c) 2 ^ h- 1 4. The height of a binary tree that contains n, n >= 0, elements is at most (a) n - 1 (b) n (c) n + 1 5. The order of search an n element sorted binary tree is (a) O(n) b O (log n) c O(n^2) b. Prove by induction (include any drawings etc, for the base case, the k-th , the (k-1)th case etc): 1. The minimum tree height of tree (h) is of O (log(n+1) to the base 2, for a full binary tree, make assumptions on the height, h 2. The minimum number of leaf nodes in a complete binary tree is k+1 where k is the height of the tree? [5 marks] [5 marks]

IIT BBS , Section 2, PDS Autumn 2012, End Sem Exam

Page 5

You might also like