0% found this document useful (0 votes)
22 views10 pages

C Programming Exam Questions 2012

Uploaded by

Ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

C Programming Exam Questions 2012

Uploaded by

Ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Departipent of Computer Science & Engineering, liT l):haragpur

CSllOOl Programming & Data Structure

Endterm, Spring 2012, Time: 3 hours, Date: 25th April, 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.

Roll Number Section

Name

Question: 1 2 3 4 5 Total Marks

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 isalpha ( int c ) ; Checks if parameter c is an alphabetic letter (uppercase or


lowercase).

• int isupper ( int c ) ; / ( int islower (int c) ; ) Checks if parameter c is an up-


percase / (lowercase) alphabetic letter.

• 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 ;

A string is an array of characters terminated by he character '\0'


(a) Write a function member ( . ) , which takes a character (which is an English al- [3]
phabet) x and a set S of type CSET as parametrs, and returns 1 if x (or its lowercase
equivalent if xis uppercase) occurs in the set S, otherwise it returns 0.
(b) Write a function adjoin ( . ) which takes a character x and a character setS as [3]
parameters, and adds the character x to the set S, and returns the resulting character
set.
(c) Write a function intersect ( . ) which takes as parameters two character sets [3]
(type CSET), computes the set intersection, and returns the intersection set.
(d) Write a function makeset ( . ) which takes as parameter a string (containing [3]
English characters),and returns a character set (CSET) that contains those characters
as members.
(e) ~rite a main ( ) which takes two strings as command line arguments. You may [3]
assume that the strings contain alphabetic characters only. It should call the above
functions to get a set that contains the characters that are common to both the strings.
Print the common characters.

Page 2
:

2. (a) Conside(the'following program segment: [4]


#inclUde <[Link]>
iJ1t fo9 (int a.1 int b, .int *vl), { '
i f (a>b) { t
~
.*vf = a; return b;
}
*vl = b; :returp. a;
}
'iD;t main, C) { "
int nl,, n2, n3
int vl, v2, tmp;
scanf ( "%d%d%d ";, &ni, &n2, 8m3)
· v2 = foo (nl, n2, &vl) ;
v2 = foo (v2, n3', &tmp) ;
tmp = fop ( v1 >" ']13, ~yi) ;
'printf ("vi %d·, v2 %d\ii'\ vi, v2) ==
return 0;
}
In ,the above program, state what will be the values of vl and v2 that will be printed
in terms of the values of the inputs nl, n2 and n3,

v1: • • • . • • . • • ·• 0 •• ·• • • • ·-· • • • • • • • •.• 0 ••• •·.• 0 0 ••• •''" .,. ~ ...... ~ ~ ~·,''; ..~
• •, .. • • • •• •:~' • •. ;, •'• •• •"• .- • • • 0 0 •• • -.,• • • 0. •.• . - .•

v2; .... ! ~. 0 ••• 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 ~

~;

··- 0' ; 0 • • -,~ ~


•.• • • • •• 0 • •' ~; ..• • ~"- .• • • • • '. • • 0 : "' • • 0 • • • • "" •., ~~ 0 .•. 0 0 0 0-. _:it~ .t.• • '•' • ~ 0 0 • "' ~ • • • "• -:. • • 0 • '. • • 0 ~ 0 • ,, • • •, •

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 H ~ .• • , o o .o o ~· • '• o- o' ;' o ,• .• o "' '' o o o o o ·o ~- o o o o o o o o o o o o o ·~ 0 o ~ 0 o;, •., o 0 0


o ,, 0
o
0
;. o
0
~ o ~ _•. Co
0
·~ 0 0 0 0 0
o ,- o , o o· o ,o o o o, o o o
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.. . . ' ~

int, myster:yFunct;ion(int *a, int n). {


if, (11==0)
':ieturn 0;
if (a [0] %2; ! = 6)
return,)+ myste:):'yFililction(a+t, n;-1);
·iHse
·[Link] mysteryFun~ti'on(at;l, Ii-1);
..
~

functio~ is_~mbedcfed.. ~heck


)'
[4]
(d). Write: a:· reGursive (char s1 []>;. char 's2 LD to titne
~characters of, s1'·are embedded in string·. s2 (occur ip the same ord~bn s2, but ·:[Link]
necessarily Qontigu<?us). . . '', . · •·· _, . . . .
Example: is_embedded C' 'ant' ' ,. '., anarc}1ist' ') should retm:n 1, but· i,[Link]
'r
C'"' 'a:har''
,._,
'· ' 'anarchist' ')" should retutii 0.
·' . ' ,,. ' '
·· 't

int ts..:embed<;led (char s 1 [] , char s2 U }. :{

' I I Base· cases . }' ,.· ~

·if,, ~s1[0] --, '\0'')

· ....• •'• ..•.•.. ··.: .•• ·.:,; ~--~···;. : ... '·' •. ·> ., ..... ·.·.:. ·,_.", •.. ·.· •. ·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) ;
}

i. correctly works ii. correctly works iii. Minimum no iv. Maximum no of


for any array for sorted array of '*'s printed '*'s printed

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
'\

initia:l vah;es 42 88 13 ·~}: 27


.,

I''
after 1st swap· ,, '
I


c
.,. <
after 2nd· swap '
I
'" '•

aftet ·3rd:swap
I

2
'

' after. 4th .swap ,g 13 27 42 88


'
"
(finf).l array) i ..
.. .: "

[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)

••••••••• 0 •• 0. 0 ••• 0 ••• 0 0. 0 •••••• 0 •••• 0 .......... • · ....................... 0 •• 0. 0 0. 0 •••• 0.

Page 13

You might also like