You are on page 1of 18

~------------------------~-

INDIAN INSTITL::JTE OF TECHNOLOGY, KHARAGPUR


Department of Computer Science & Engineering
Programming and Data Structures (CSllOOl)
Endsem (Autumn, 1st Year)
Date: Nov. 22, 2012
Students: 660

Time: 09:00-12:00pm
Marks: 100

Answer ANY FIVE questions.


Write you name and roll number on ALL sheets.
Only the answers written in the space provided against the questions will be evaluated.

Roll no: _ _ _ _ _ _ __ Section: _ __ Name=----------------------~--------1.

(a) Let the normalized representation of any non-zero real number be (m, e), where the range of the absolute
value of the mantissa m is 1 :S lml < 10 and the exponent e is an integer power of 10. Thus, the
nor~alized representation of 432.27 is (4.3227, 2) and that of -0.011537 is ( -1.1537, -2).
The following recursive C function is meant to take as parameters pointers to the mantissa u and
exponent e of a floating point number u x 10e and transforms the number to the normalized form.
It is invoked first time with e = 0 and a non-zero mantissa u. Write the function.
void normalize (float *U, int *e)

- 1 of 22 pages -

..
PDS (CSllOOl)

... '

Sec:

'"

'

.-

(5) 'Let a recurring .deposit: ~Cherne in a b~ provide I percent :~omp.oupd iilterest'pe:r annuq~.~ computed'
. monthly. (Note.that,~or,this sche~e,adeposit PbecomesPx{l +(I/l2)/100}1 2 JSk ~fter-kyears.) The
scheme fun~ for a term of n. years and permits depp$it of any ampunt to the. scheme or'lce every month
throughout these .n- years. Let Pi, 0 :::;., i < 12 x n, be the ~mount depos~ted.rn the f-th.mo~th ;of:b,p~ning
of such an,accourit (with the firstinstalment of P ),

'
'
'

i. Give the expression for tpe maturitY' amount 'pm of !he account, at t4,e end ofthderm.
I
.
..
,
'

I
ii. Write. a function which. takes as parameters the monthly deposits .Pi, 0 :::; i <:: 12 x n, the
annual interest r11te and the term nand' return~ the,iiiaturity amount Pm as a real valued quantity:.
The function should" not use. any ma (:;-'}1/ibrary .funcdon or any'fun,ctioh written by yourself for
exponentiatiqn.
2.+ 5

..
'

'

;;,'"

'

'

,.

.,
)

DS, -SC,~AM

J
,: Endsem, Autumh, 20l2-13

"

'

'

..

.
..

' ~

.,

pbs (C~-tlOOi) ~
'
-

~,Sec.:

.Roll:
~ (~~-
~ ,_~

.,.,.

li.;

,1

.~

,,

.~:~

-.~~=

&

""'

.-\~''f<

f: .l

~:"'~

't

~oAs.ide~:th~, f~lo~in~:e~pressionj~ Rever~e. Eoiish: Nqtati?n (~Nt 12.4 75; 6 s + eu~lrne ~lie
step-by-s,tep co.mputatiO.n 9fthe value. of this :RPNexpress10n, !:e., show for. ~acli.step, the~n1pu_t,~em~
, prQtessed;ana il),~ stack:s,onfi~ur'!:ti~.n'af~enfia't:step?.
, ~.~ -~

I~ "

'"'"

,.\ '

~-~

"'

,
r,, "
~

ot'<J I

.*,.+ ..

....

'

"

'I

'

...
'';lo_,

,.

'
,.,

.~

:t

-... 1, ...

~\ ~~

~ '

_J,...._.,_.

''f. :f

J!"

"

: ~,

'\,

.,

,."'"
'hi

,.

\
~.

:-r ~~
""'".,.

~ ,j~

.,.

'\

j~
[\:,,.,

"''

"!

'\

"'

.j

DS/RSC,AM

-,Eni:lserri,
Aut~~uf, ):to
f2-13
'
'J "

PDS (CS11001)

Roll:

Sec:

2. The program segment below defines two structures and then implements the following functions:
(a) a function to read information about the employees of a company (such as employee name, id, sex, date
of joining etc.)
~

(b) a function to count and display (print) the number of male employees in the retirement age group (55 to
60) and the number of female employees in the maternity age group (25 to 40)
(c) a :function to calculate and display the employee records that have salary above the average salary
(d) a function to display the employee records sorted (using bubble sort) by the date of joining of the
employees.
Fill in blanks/parts of the program segment to complete the desired functions; correct syntax errors, if any.

II

struct date{
int day;
II day
int month;
II month
int year;
II year
date _of_join;
struct company{
int employee_id;
char sex[S];
int age;
float salary;
doj;
COMPANY;

II
II
II
II
II
II

Defining date structure

Defining company structure


Employee identification number
Sex
Age
Salary
Date of joining

II

read records of n number of employees


void readEmployee(COMPANY employee[], int n) {
int i;
for(i=O;i<n;i++) {
printf("Enter information for employee no: %d\n", i);
print ("Employee ID: ");
scanf("%d",&employee(i] .employee_id);
printf("Sex: ");
scanf("%s ",employee[i] .sex);
print ("Age: ");
scanf("%d",&employee[i] .age);
printf("Salary: ");
scanf("%f",&employee[i] .salary);
printf("Date of Joining:\n");
print ("Day: ");
scan ( "%d", _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ );
printf("Month: ");
scan ( "%d", _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ) ;
printf("Year: ");
scanf ( "%d", _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ) ;
)

DS,jRSC,AM

Ends em, Autumn, 20 p-13

- 4 of 22 na~YP.c: -

Sec:

Roll:

PDS (CSllOOl)

II count number of males in retirement age group


II and number of females in maternity age group
void displayCount{COMPANY employee[], int n) {
int i, int countRetire, int CountMaternity;
countRetire=O;
countMaternity=O;
for{i=O;i<n;i++) {
if{employee[i] . ______ >=55

employee[i]

--~--<=60

_ _ _ _ _ {employee [i] . ______ ,"MALE")==____ )) countRetire____ ;


if{employee[i]

---~>=25

employee[i] . _____<=40

_ _ _ _ _ {employee [i] . _____ ,"FEMALE")==_____ )) countMaternity____ ;


printf{"Number of males in the retirement age group is: %d\n",
countRetire);
printf("Number of females in the maternity age group is: %d\n",
countMaternity);

II display employee records who have more than average salary


void displayAboveAvgSalary(COMPANY employee[], int n) {
int i;
flo?t avg_salary=______
for (i=O; i<n; i++) {
avg_salary_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __

avg_salary=-------------------------for (i=O; i<n; i++) {


if( _______________________________~----------->{
printf{"Printing the record for employee: %d\n",i);
printf{"Employee Id: %d\n",employee[i] .employee_id);
printf("Sex: %s\n",employee[i] .sex);
printf("Age: %d\n",employee[i] .age);
printf("Salary: %f\n",employee[i] .salary);
printf("Date of Joining:\n");
print f ("Day: %d", - - - - - - - - - - - - - - - - - - - - - - - - - - > ;
print f ("Month: %d", --------------------------"--->

print f {"Y~ar: %d", --------------------)--------> ;

DS,RSC,AM

- Endsem, Autumn,

~012-13

5 of 22 pages -

Roll: .

Sec:

,,
" dat'e pf joining
J I display employee records sorted (bubble sort) by ther
void. displ~ySortbyJ.oininc{(COMPANY .employee[], int n) ..{

II complete the' ent<ire 'function.

"

t1

'

)'

14+9
)

DS, RSC,.AM~

-~ndsem, A.utumn, 2012-13

..

~i!""!c~' ~

"''

~~~

..

'}

Roll:
~,,

~.

'

.fro..

~~

B -.'T,

~~~. ~ ~

~
c,'

~;

"

~ ~' ~~:fu

'

. .
..:

,"~t,.

'

;3. A squ.m;e N~~,N:~atr~,]\~aid)o .b'e~orthQnormal if AA:~l."YhereA! ls";.thy,ttahspose of.:tpf l:llatr!~A aqd '
l i~ the 1denti,t)~
wap:lX.:. ImpiemenJ the foll;:wing
functionS.t
check;, wnetlietJqe:in~{rjk
A .ston~d
:[n a two
",~"~~,
,,'"~
} ~ ; ;-.,
':: \l;\~ ''<~'\
'l
d!m~nsional ar;,ayA:JNr Ll'!J is.brtlio~orfna1.
"
.. . ''.
' ...
'

'

,-.,.;

"".

C'

"l'

'

~,;

.<

to.

'

.~

(a) :vo1ct,'matTrR:.n~pc;)'se ( f1c;a::t :A::[~J tN J, fld~t


stores it

in f.T.
""'

'

;.

&.

!:

l'

'

...

F}JtJ' [NJ'). ~c-al~ul~tes'the:tr.,ans~p-~~oi 1A:'at}d' .


5
..
,., .,:
.. ., . "': "'
,.. , .., "\
{\,-

{~). iht; 'rth.e>NP~rnal((toat :At]":.[l'JJ, f"ioat 'lf.T rDNJ:)-.mak~~;~s~of.'m~!:~~t.'~.nspC?~e-~na


fin_alJy"'returils an appro.f>riate sJgnal indicating whetlie.r 6rhoLkis'ortnovoflmil:~;"' '~!~.:;,~.~
~

"""' ..

li~

,_t;

.,"

. I

.,

,,
'I

.,

'"
"'1':."

j .

'

,.t:

kY,~

.;
><;:

'\,

'

,,
jl.

i'

..

... ~

vOU.'

.' !

.;

PDS (CS11001)

'.~oil:

Sec:

(c) Suppose you have a linked list, with the data field of each node Qf the linked lj,st .contaiillng an integer.

i; Declare (by t,ypedef) a C structure qilled LListNcfde to describe .a sfug;le nqde of the linked
fuL
.ii. Write. a C function :P.emoveDuplicate to remove nbdeshaving.dtiplicate numbers in the linked
list. For ex(\mple, ifthe linked list contains 10 --+ 20 --+ 25 --+ 20 ~ '10 --+ 1.5
processing' by RemoveDuplicate, itshould contain 10 7 20--+ 25---,+ 15.
I
...

.
,
,
I,
Note: resel\Ce of a duffi!11y"header nosf!f" at the starLQf thelinked-list_is OptionaL

DS, RSC, Atv1;

--+

JO, after,:

- Endsem,
AutUmn~ 2012-13
'
.

- 8 of 22 narr~c: -

r-.

PDS (CSUOCH)",

Roll:

(a) Writ~ a. c;: furi~t~ort,void rev~rse (char *.s.r char


parameter string' ( s) in: the second parameter string ( t).
~ ~

,,

'kt)

"~

.,

,,

. "
,

.,,-'

Endsem,
Autumn,' 2012-13
.
"
~

"

-? of 22.pages.-

(,,

PD_S (CS'll.OOl)

sec:

Roll:

{b) The followmg recursive. C function is to compare two strings s and t and return a negative integer 1f s
< t,- zero if s = t and apositive integer ifs > t. Fill in the blanks so that the above ftJnctionality is
achieved; correct s~ritax errors, if any,
int compRec (canst char *Sl, c()n_st char *S:2)
. __
if ( *Sl 1
II end of at least one of th~m reached
{eturn *Sl - *~2~

if (____

~--

_______,)

)'

I I their first characters. are same

____ );
- - - -. II

otherwise

''

...

pnds~rp,

Autumn,-2012713

10 of22 n~D',.~;,-

'

Sec:

Roll:

PDS (CSllOOl)

(c) The following C function is to replace all the occurrences of "more" in the parameter string s by "no
more". Fill in the blanks using the following string library function- correct syntax errors, if any:
unsigned int st r len ( cs) -returns the length of cs not including the termination (null)
character, ,
char *Strcpy (s, ct) -copies string ct to strings; returns s.
int strcmp ( cs, ct) -compares string cs to string ct; returns< 0, if cs < ct, 0 if cs = ct,
1
'
or> 0; if cs > ct
char * strcat ( s, ct) -concatenates string ct to the end of string s; returns s.
char * strstr (cs, ct) -returns pointer to the first occurrence of string ct in string cs,
or NULL if not present.
void soften (char *S)
{ II assumed that string is ensured by the caller to have
II sufficient space to accommodate the resulting expansion
char toReplace[6] = "more", by[lO] = "no more";
char *t, buff[lOO];
if ( (t

(__,

_____) )

- - "-

------

11 any occurrence of "more"


*t = '\0';
__________ , t + strlen <--~---------> );
II copy the suffix string beyond "more" in buff
s =

(s, ___ ); //concatenate "no more"


II overwriting "more"

s =

( s' ____ ); II concatenate the copied suffix


+__________

___________ }); II call yourself

DS.RSC,AM

Endsem, Autuillll:, 2012-13

- 11 of 22 pages -

PD.S. (CSllOOl)

''Roll:

,.

Sec:'

5. . (a) Give a suitable type definition.(qsipg typ~d~ f) for stacks oHnteg'ers.


(l:>)

:Foreachofthe,following.furi~tions on,stacks, first give. the header ex;platning th~ Parawet~rs. passed and
the value returned, if.any, and then give tlie function body (in C) using the type defirution of part'(a):
push'forpushing an integer onto astack;
'
pop for 6btajning the top of a stack arid remoying}t ffomJhy.stack;
isEmpty.forchecking'whether a "stl!~k'ls empty'or not.
m.

'

.-~, ~

't

'I

',

,,

D~.

RSC, i\M: .

'-~.,..,..,"""""'',\"''""'""'-....-,

.,..,.,.. .,.,

. Endsem,. Autumn, 'Z012-B,

- 12 of:i2 naP"e~ --'-'-

i2

'

'.:/

"

Sec:

.Roll:

's

i>jj

,.

.,

J'

~::-~,;; ;

...,,

~\ ~~

~~

;',

'
,
i.

.'

...

\,"'=

'

..-t

'\.

'

.,
. ffi

'

...

'

.,

'

'
~,.

~'

:It'

'

''!?' .

.,

...

.,

''\!:

.,
\'

''f

\"

'
. !,

'"":.]

~I

'~

.,
1i-4 "'-

,'

\~

~ "'
r!.

~,

'%~

...
-pns (CSllOOl)
RoU:

Sec:

(c) Let there be a data type LIs TIN'i (defined by type de f) for lists Of integers; let' there be the 'fo!iowing
functions, for manipulating such li~ts.ofintegers:
LIs
TINT
t a i1 (LIsT I NT I Which returns the tail of the parameter lis('(i.e. th(parameter list with
its
head
removed),

in:t heag (LISTINT) which-returns the first elemeht oftheparameter list,


LIs T.INr' a PPend r (LIs TINT, intI ,which ap]lends the second parameter at the end Of the first
parameterlist,
LIs TINT reVerse (LIs TIN)') which returns a list which isreverse of the pararneterlist.
It is. required to define a data type for stacks of integers using
Give a suitable type-definition
(using type !-let). Give the function body (in C) of the stack functions push and pop.

U~.TINT.

n.s,_~sc,

AM,-

. Endsem, Autumn~ 2012-13

) .

__:,.14 of22 n>'l<>on

6.

Sec:

Roll:

PDS (CS11001)

(a) A circular linked-list is one in which the last node of the linked-list is connected with the first node to
form a cycle. the linked-list is represented by a pointer to the first node. An incomplete C program is
given below which works on a circular linked-list as follows. In the program, a circular linked-list with
N (=5) nodes is formed, with the nodes containing the integers 1 ----? 2 ----? 3 - 4 ----? 5 ( - 1). Since
the linked-list is circular, the node containing 5 is connected to the ~node containing 1. Then, starting
from the node containing 1, M (=1) nodes (including the starting node) are skipped over and the next
node eliminated, resulting in a circular linked-list 1 ----? 3 ----? 4 ----? 5 ( ----? 1). The node following the
eliminated node becomes the new starting node, and the operation continues until there is only a single
node remaining in the linked-list. After every node elimination, the contents of the linked-list are printed.
The desired output of the program on the screen is as follows ( - the repetition of the last integer with
the first one in each line indicates only the circularity of the list):

Current circular linked list: 1 2 3 4 5 1


Round number:1

Current circular linked list: 3 4 5 1 3

Round number:2

Current circular linked list: 5 1 3 5

Round number:3

Current circular linked list: 3 5 3

Round number:4

Current circular linked list: 3 3

Fill-in the blanks to complete the program; correct syntax errors, if any. Note that in the linked-list
implementation shown in the program, no dummy "header node" is present at the beginning of the
linked-list.

#include <stdio.h>
#include <stdlib.h>
#define N 5
#define M 1

II A node of the linked list


typedef struct
int val;
struct _node *next;
node;
II Define "list" to be a global pointer variable of type "node*"
*list;
I**************** Function prototypes *************************I

II Function to create a new circular linked list 1, 2, 3, 4, 5


void create_circular_list (int n);
II Function to display the contents of the global circular linked list
void display_circular_list (node *head, int n);
II Elimination Game with circular linked list
void game_with_list (node *head, int n);
1**************************************************************1
I/ main()
int main ()

DS,RSC,AM

Endsem, Autump., 2012-13

- 15 of 22 pages -

10

PDS (CSllOOl)

Roll:

Sec:

(N);
display_circular_list (list, N);
game_with_list(list, N);
return 0;
create~circular_list

II Function to create a new circular linked list


void create_circular_list (int n)
int i=l;
node *ptr;
while (n) {
if (i _________ l) { II the first node
ptr
(node*) malloc (sizeof(node));
list= ptr; II mark the head node
else {
ptr->next = (node*) malloc (sizeof(node));
ptr = ptr->next;

ptr->val
i++;
n--;
II end while

II

ptr->next =

make the list circular

II

Function to display the contents of a circular linked list


void display_circular_list (node *head, int n)
{

node *ptr =head; II make a copy


printf("Current circular linked list: ");
while (n) {
printf("%d ", ptr->val);
ptr
ptr->next;
n _ _ __

II

end while
printf("%d\n", ptr->val);

II
II

circularity depicted by printing the


first one again

II Elimination Game with circular linked list


void game_with_list (node *head, int n)
{

node *Ptr = head; II make a


node *ptrl, *Oldptr;

cop~
)

DS,RSC,AM

Endsem, Autumn, 2012-13

16 of 22 oaQ:es -

.j

,. ,,_J
,i'

-:'.

~~

Roll:

"t~ ~
,;
~ ~ "'

'-,\

:..
-t~

~,.,

'',:r:, ,(''

; J.

1,

nt. "1~.
~

''

~ !l,..

.,, int~r~Uhd = ...p:; II ~gm -~nt:eger"'~ ~:W:ni.l{ ,(ptr_:_>next ! "".'pt~)


~~:-,.r

~:s':~F-:
.. _
. '4.,...
~~;:-~rouild ~
~if~ -<>!'~,c
k

.....1,.-.

..

,_

,,

,.,

'"

.~'" ~- .M=-:rt~;t~~t:c;;~n:Rou.nd, riu~ber:'%~.~ .t~~i';~~d>';' '

t~.;~r Sk:l~

oyer:: t~e r.equired


~
f-o'r' (i~l-i i <M? i;f+)
~

~ ~~-

.,

.,

~-

. ~

.. '

"'l

pt r

'

t' ....,,

;.~".

"'-

-w,

pt~--?>

'
,

... ,

-:

'

:MI.

""

i"'

ttw

"'
~of.,.

node .a{t'er',.:pfrl.

-~

-~'

.,

~ .~::..~ '~'

~~

t'

~'

>'

,y;;..'

~.,

oldptr->nex-8\ ;;:~ ~---f'-'-----'--'-~>D~.~.t_; '


11 Free ptbl
,..

,,"'<.
.~;

to

~;.

'

., ''

'

\c;~1:l '0~1>~ 91~p~:~>~ext Poirit


"

..-.,

~ ""' ~

,,./

,,

'"j,,~(Next fibae

'

:}. \

'

'

'has
-to' p~ delet:ed'
,,
,; (f. Ge~ th,e' n~fi~ .,:
:.'ptrl = ptr:->neX:t-;
-~i/ :M~k-e a: ,copy~ ~.{)t ptr
' "
~ .~ .;:orctptr ,;, "pt r_.::. M" '

,:, -,

.>!:..

'

~u~~-~ o~-:-~o.d~sL

....

. :'"} ':t , ;:}~nd 'f,or


~

i(

:to
'

.~~ ''~~-~

'~ ~

..

'

>

'fre~(ptr;tJ;

~,

_,,.~

,,, ~(I Ma'k~' pt r R'?~nt' t:o. t'Q~ ,cne~:r; sDtaf( ~ode'


~
olctptr~>:q~)ct;

,.pt.r.:=

' ~l

' P:J'sp ray....:~ ix cu 1 a r _1 is tfpt t

'}.'/lend .~hfle

~- ~. v-~--~~/~ "'' t

'

l~~

~ -~~

'

,.. ;, 'l ~

./

Sf -

,..._

,' __ '_,,_,',,--::--'-:"-.,=-[~-"'.~....:,=-~-:'--,---~---1

:7

: :;:

!!t;ib

~.!

f:..f~

~
''

"!'

':;

~~

,.

,,

~ ~

~:."

</'

" '~

Entlserri,Autumn; 201;2713 .

"''"""'"'"'-'"""'"

'

~~

'

. .-.

-:1torr22 p:~ges:~

~'

..
,.

PDS' (CSllOOl)

Roll:

Sec:

(b) ,Answer the foUowfug questions:

a'' Queue containing mtegers. You can assume any implemehtatio11


(array-based or linked-list-based).
'

i. Write the C declaration for


iL

Bas~d on the aboveimplemertta:tion for a queue, write two C functions to implement the enqueue

(adding one. element at the back ofthe queue) and dequeue (r~moving and returning, the front
element of the queue)
functionalities..
' I
I
.

iiL Use the above two fi.mctl.ons to write a C function tb remove the current value from the front of the
'

queue and ii;dd it to its, back.

,,

"

' ,,

'-

DS,_RSC,
AM, , _
,
",~ ~,-,~.....,~ ,..,.:.;.. - ,,. ""

1
.,.,.

Endsem; Autu.Qlll, 2012:-13 ..

'

'
6

You might also like