C Programming Exam Questions 2012
C Programming Exam Questions 2012
Answer the questions in the spaces provided on the question sheets. You may use
the Extra Page in th~s answer booklet for answers/ rough work. No other
supplementary sheets will be given to you.
Name
Points: 15 15 15 15 15 75
Score:
A list of some useful library functions is given below defined in string.h and ctype.h:
• char * strcat ( char * destination, char * source ) ; Appends a copy of the source
string to the destination string. The terminating null character in destination is over-
written by the first character of source, and a new null-character is appended at the end
of the new string formed by the concatenation of both in destination.
• char * strcpy ( char * destination, const char * source ) ; Copies the C string
pointed by source into the array pointed by destination, including the terminating null
character.
• int tolower ( int c ) ; / int toupper (int c);) Converts parameter c to its lower-
case/ (uppercase) equivalent if cis an uppercase/ (lowercase) letter and has a lowercase
/ (uppercase) equivalent. If no such conversion is possible, the value returned is c un-
ch;:mged.
1. Consider the following data type to represent a character set that represnets a set of English
alphabetic characters (maximum size 26). The characters are stored in lower case form.
typedef struct {
char elems[26]
int size;
} CSET ;
Page 2
:
v1: • • • . • • . • • ·• 0 •• ·• • • • ·-· • • • • • • • •.• 0 ••• •·.• 0 0 ••• •''" .,. ~ ...... ~ ~ ~·,''; ..~
• •, .. • • • •• •:~' • •. ;, •'• •• •"• .- • • • 0 0 •• • -.,• • • 0. •.• . - .•
(b)
#include <stdio.h>
#:include <string.h>
#include <stdlib.h>
int maJnO {
char' *·s = (char*Yinalloc(12*sizeof(char));
char *t =
(char*)malloc(15*sizeof(char));
strcpy,Cs, "gc:>od");
strcpy(t, "hi there");
char *P = t+4;
t = s;
strcat('s, "bye");
pr~p,t:t;("s = %s\n", s);
'printf ("t - %s\n", t);
p:riiltf( 1'p· %s\n", p);
return 0;
}
i. What 'is·the output ofthe above. program?
Page 4
---~
..
-
' '
:. ,
r· 1 '!ot ~
~;
l- :~ ~ ~- ~ t':r
t~.!.• •••••• ·• ·>":. ~ ~~· •.• ·:·. 0 0 0. 'f.!.-....~. 0. •.•. ·.·~· \" •••• ·~· •• ~ .~ ••• 0 .; • • ; •• • :.• ..... 0. ~' :. • •<• •• 0 ·-.·:·.
''
ii. This program allocatefl memory without free~ng)t. Suppose -you acid the following
· cOde right before return 0; . .
free,(s); :''
free(t); ,
·0nce' this c~de·. is added, dQes it free the dynamic storage pr9perly?·. Why or ;why
not?'
O 0 0 0 o.i::. 0'~,~0 0 0~0 0~0 0 0;0 0~0. 0 -~ 0 0,0•0 0~'0 0'10 0 0~0 0 0 0,0 0 0•0 0 0 0 0 .. 0 0° 0<0 •• : : . 0'0 0 0+0,>0 0 0.-0 0 0 ~ 0 0 0 0 0 OwO 0"0 0 0 0 0 .,. 0 0 0 ~ 0
o''o:o o ; o e-a. •·•• •''a o"o • ' • ' • oOo 'o~o •',o •.• o o 0 o o •·o 0 o ~ ~ o •••.• o o o o~o o .- o o !,,~:• o'o o •,• o,o o o. o ~ o • ~ 0 0 •, 0 0 ° 0 ° •'o •';; --''
0 0 0 0 0 0 0
. •,• •'• ....·. y.,,, ... ; .....• ,', ~. ,, .... ···.: ::·.' •<;' •.••• '" •,• .-. ,., • ··: ••••••• ,•• •,• •• ··:.' '.'.......: .•• •·•'•
(e) Corisi,derthe following rec\lrsive function. which :takes ,an' ·array of integers a, and the [2]
length of ~hat arfay n as·parameters, and returns an integer.·· State de,ftrly [Link]'this
function ~ompuies.. . . ' ~
· ....• •'• ..•.•.. ··.: .•• ·.:,; ~--~···;. : ... '·' •. ·> ., ..... ·.·.:. ·,_.", •.. ·.· •. ·t :•: .. ',;' ,,........ ·.· .. .. :.
if (s2 [0] '\0 ')
• • • • • • • • • • • • 'i .•• • , • • • • ~ •"• • • • • ·~· • • • • ', • • • • • • •·•,• .; • • • • • ;·,.. •'.• •• ··~·. • • • •.,•.· • • • • • · • • • ·~· .,. •"
·;I ,Rectirsi
·~ v.e cases ' o' ( ,N ,.
.J'
. . .• -; • • • , • • •. ~, • • • ... • • • • • • •. ,. • ': • • • • • :. ~ • : ...1• • • • '• • ' • • ·• -~.' • • ~ •• • • • • • • •• • ••• • • • • • '•' .~' • ' •• ~. • • :. • • • • • • • :-, • • • ' • '. • • • .• • • :i• ••
• • ... • , •••· . • • . . • · .• ! • > •••••••• ' ••••••• •••••••• ' .......; • ~· ••••, •••••• • ' • ' •••••••. • •• ·' •• ' ••••. • " •.•• :. ••.••••• ..... .
.,.
0,0 o,,o 0 o o 0 o ~ O'o 0 0 0 o,O 0 0 o 0 o 0 0 0 0 o'o o\'o 0,0•0 0 0 o'o 0 o'o 0 o 0 0 0 'o'o 0 0 0 ~.,o 0 o Oj_-;•, ·~·~· 0 o_,o ~:o --·~~'·;o o'o o,O o o,,,~_:o 0 0 0 o'o 0 o•·,o 0 'o 0 0 0
.... : :· . ......... ..,... ; •'• ,•. ·... ' ... : . .. .. ...·. :........ :'.·/· . ·'· "' . ........ ......... .....· ......... .
•, • · ." • • .. "'•
. . . :, ......· ...................... :· ... ··· .. ,.,. :.···.·.··· ·.,........ '......... ,....... ,,,. ,. ··•'• ......... ..·.. _.,: .... ·.... . ;,
[Ro:ligh ·work]
.,
''.1
,> I
,,""-
,,
.,
Page6
,yr..
3. in the following problem we conside the representation of a black-and-white rectangular
image. The image has height h and width w pixels. The image is stored as a 2d-array,
where the 2d-array index (i,j) denotes the coordinate of the pixel in 2d-space. Each pixel
has the value: 0 or 1 (0 indicates background, and 1 indicates object point).
Implement the following functions:
(a) int ** alloc( int h, int w) : Dynamically allocate space for storing an array of [4]
pixels of dimension h x w. Return the address of the dynamically allocated memory.
(b) void read(int **mat, int h, int w): Readthevaluesofthehxwarrayofpixels. [3]
Assume each pixel value could be given as either 0 or 1, and the input is read row-wise,
and at each row column-wise.
(c) int distance(int x1, int y1, int x2, int y2) : Compute the Manhattan dis- [3]
tance between two points (xi, yl) and (x2, y2) in the x - y plane. The Manhattan
distance is defined as lxl - x21 + jyl - y2!.
(d) int neighbours (int **mat,int h, int w, int x, int y, int k): Findthenum- [5]
ber of object points in the array mat of size h x w that are within a Manhattan distance
of k from point (x, y).
Page 7
----,
4. (a) Consider the implementation of a linked list for which every i:Qsertion takes place at
the end of the list. The starting element of the list is pointed by a variable head and
the list is terminated by a NULL pointer.
struct node {
int key;
struct node *next;
}; // Structure of a node of the list.
Implement the following functions:
i. struct node * insert (struct node * head, int val): Insert a node with the [5]
key value as val at the end of the linked list. Returns the head of the list.
ii. int printsecond (struct node * head ) : Print the second element of the [2]
linked list. Return -1 if the second element does not exist, and 1 if it exists.
iii. int count (struct node * head) : Return the number of elements in the list. [2]
iv. void printreverse ( struct node * head) : Take as input a linked-list of in- [3]
tegers and prints the elements of the list in reverse order (i.e. last element first)
using recursion. (The list is not to be changed.)
v. Write a ma:i,n( ) function, that [3]
• creates/ initializes a linked list,
• calls the function insert ( ) 3 times to insert nodes containing the following
values to the linked list: 20, -10, and 8.
Page 9
I
5. (a) Cons_ider the following functions that search for an element x in an array A containing n [6)
elements. The functions are called as search!/ search2 I search3 (A, 0, n, x).
For each of these functions
i. State whether the function returns the correct result when called on any array of
integers.
ii. State whether the function works correctly when called on an array of integers
sorted in ascending order.
iii. What is the minimum number of '*'s that will be printed when the function is
called on an array of n integers (which is the same as the number of times the
function is called in the best c'ase)?
iv. What is the maximum number of '*'s that will be printed when the function is
called on an array of n integers (worst case number of function calls)?
The answers may be given by filling up the table below.
int search! (int A[], int left, int right, int x) {
printf (' '*' ') ;
if (left == right) return 0;
if (A[right-1] == x) return 1;
return search! (A, left, right-1)
}
int search2 (int A[], int left, int right, int x) {
int m;
printf (' '*' ') ;
if (left == right) return 0;
m = (left+right)/2;
if (A[m] == x) return 1;
if (x < A[m]) return search2 (A, left, mid,· x)
return search2 (A, mid+1, right, x)
}
int search3 (int A[], int left, int right, int x) {
int m, v1, v2~ v3;
printf (' '*' ') ;
if (left == right) return 0;
m = (left+right)/2;
if (A[m] == x) v3= 1;
v1 = search2 (A, left, mid, x) ;
v2 = search2 (A, mid+1, right, x)
return ((v1 I I v2) I I v3) ;
}
searchl
search2
.search3
Page 11
'.
(b) Suppose t~~~ ~you are J?:erforl:niJ?-g the -selectzon so~t algorithm as d~~~~ibed 'below:\·
After ~)1e entire §Ort, ele. ments .will be in order from smallest-to ·largest Each pass
through the array "seh~cts" the largest .element fro!E, ~he smaller arr~y, and swap~; it
into place. W,i th [Link] in m\nd, indicate the V:a!ues' of each elem~nt. of the array ~fter
the f~t'.2J14 a~,9 '3rd pass. ' "
. ..
'
•. ·""
"<to,
'·
a[oJ a[l] a[2] . ~
a[3J a[4] l
..
.
'
'· I
'\
I''
after 1st swap· ,, '
I
.·
c
.,. <
after 2nd· swap '
I
'" '•
aftet ·3rd:swap
I
2
'
[Ro11gh Work]I
..
Pa,ge 12
~--.-
----·----
.
(c) What will be printed when the following program executes? [2]
int partition( int a[], int low, int high) {
int left, right, pivot, ~emp, pivot_item;
pivot_item = a[low];
pivot left = low;
right high;
while ( left < right ) {II Move left while item< pivot
while( a[left] <= pivot_item ) left++;
while( a[right] > pivot_item ) right--;
if (left< right) {II swap a[left] with a[right];
temp= a[left];
a[left] = a[right];
a[right] = temp;
}
} II right is final position for the pivot
a[low] = a[right];
a[right] = pivot_item;
return right;
}
int main ( ) {
int pos, i;
int A[10] = {22, 17, 46, 12, 82, 5, 19, 80, 61, 20}
pos = partition (A, 0, 9) ;
printf ("pos = %d\n", pos) ;
for (i=O; i<10; i++) printf ("%d, " A[i]) printf ("\n")
return 0;
}
(d) Compl~te the function qsort ( ) that calls the partition ( ) function from the [4]
previous question, and uses the quicksort algorithm to sort an array of integers
void qsort (int a[], int low int high) {
int pos;
if (low >= high) return;
pos = partition (a, low, high)
Page 13