Fundamentals of Computing and Programming BP 2023-24
Fundamentals of Computing and Programming BP 2023-24
Back paper Exam; Total Marks 41, Maximum marks 40, Time Limit 3 hours
December, 2023
1. Below a struct, a union and a type are defined. It is followed by a list of 10 function calls.
Write the output when these function calls are made, by considering the function definitions
that follow. Assume also that the appropriate #include statements exists. Note that in case
of 1(j) there is a printf after the function returns. [10x1=10]
1
// ---------- 1 (g) ------- // --------- 1 (i) --------------
void fun1g(){ void fun1i(){
char a[10]= int i=3, j=0;
{’\0’,’a’,’b’,’c’,’\0’,’d’}; do {
printf("%d",strlen(a+4)); int i=0;
} j++;
i++;
} while ( j < 2 );
// --------- 1 (h) ------------- printf("%d %d",i,j);
void fun1h(){ }
int a[5]={0,10,20,30,40}; // --------- 1 (j) --------------
int i=4; void fun1j(int n){ //note: question calls
do { // this with n=3
printf("%d ",a[i]); if ( n == 0 )
i++; return 0;
} while ( i < 4); return ( funj(n-1) * n);
} }
2. Write functions for the following assuming the structure defined in Q1, and the provided
print() function below. [4+4+2=10]
3. Write the function int merge(int a[], int n, int b[], int m, int c[]);. [4]
It merges two sorted arrays: a[] with n integers and b[] with m integers into a single sorted
array c[]. You may assume that c[] has enough space.
2
(a) using malloc() create two smaller arrays a1[] and a2[] each capable of holding half the
elements of a[]. Copy one half of a[] to a1[] and the other half to a2[].
(b) recursively call merge_sort to sort these two arrays a1[] and a2[] independently.
(c) Call the merge() function of the previous question to now merge a1[] and a2[] together
into the original array a[].
5. Write the function voidinsertion sort(int a[], in n); Here is the basic idea : [5]
(a) We note that initially just the element a[0] can be imagined to be a sorted array of one
item.
(b) if a[0]...a[k] is sorted then we can sort a[0]..a[k+1] by simply finding the correct
index position for the value of a[k+1] (call that v) among a[0]...a[k]. If that index
position is p , then we move all existing elements from a[p] to a[k] up by one position
and then put v in a[p].
(a) Declare a structure called struct book. It has two fields: an array of 10 characters
called title. An integer called npages. Also declare an array of 20 such structures
called books[] .
(b) Write a function called struct book * search(struct book a[], int n, char *
x) that takes an array of n books a[] and a character string x. It searches the array
and returns the pointer to the unique structure in the array whose title matches the
given string in x. If there are no structures with the title matching x or if there
are multiple matches, then it returns the NULL pointer.
(c) Assume the array books[] mentioned in part (a) exists and has 10 books in it already.
Show how you will call search() to search for a book titled ”Forgotten” and use the
returned value to print the number of pages in that book if it is unique.