Programming and Data Structures
Programming and Data Structures
Time: 09:00-12:00pm
Marks: 100
(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
II
DS,jRSC,AM
- 4 of 22 na~YP.c: -
Sec:
Roll:
PDS (CSllOOl)
employee[i]
--~--<=60
---~>=25
employee[i] . _____<=40
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) ..{
"
t1
'
)'
14+9
)
DS, RSC,.AM~
..
~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.
'
.~
in f.T.
""'
'
;.
&.
!:
l'
'
...
"""' ..
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
--+
JO, after,:
- Endsem,
AutUmn~ 2012-13
'
.
- 8 of 22 narr~c: -
r-.
PDS (CSUOCH)",
Roll:
,,
'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 (____
~--
_______,)
)'
____ );
- - - -. 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
(__,
_____) )
- - "-
------
s =
DS.RSC,AM
- 11 of 22 pages -
PD.S. (CSllOOl)
''Roll:
,.
Sec:'
: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: .
'-~.,..,..,"""""'',\"''""'""'-....-,
.,..,.,.. .,.,
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),
U~.TINT.
n.s,_~sc,
AM,-
) .
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):
Round number:2
Round number:3
Round number:4
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
DS,RSC,AM
- 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
ptr->val
i++;
n--;
II end while
II
ptr->next =
II
II
end while
printf("%d\n", ptr->val);
II
II
cop~
)
DS,RSC,AM
16 of 22 oaQ:es -
.j
,. ,,_J
,i'
-:'.
~~
Roll:
"t~ ~
,;
~ ~ "'
'-,\
:..
-t~
~,.,
'',:r:, ,(''
; J.
1,
nt. "1~.
~
''
~ !l,..
~:s':~F-:
.. _
. '4.,...
~~;:-~rouild ~
~if~ -<>!'~,c
k
.....1,.-.
..
,_
,,
,.,
'"
t~.;~r Sk:l~
~ ~~-
.,
.,
~-
. ~
.. '
"'l
pt r
'
t' ....,,
;.~".
"'-
-w,
pt~--?>
'
,
... ,
-:
'
:MI.
""
i"'
ttw
"'
~of.,.
node .a{t'er',.:pfrl.
-~
-~'
.,
~ .~::..~ '~'
~~
t'
~'
>'
,y;;..'
~.,
,,"'<.
.~;
to
~;.
'
., ''
'
..-.,
~ ""' ~
,,./
,,
'"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
....
i(
:to
'
.~~ ''~~-~
'~ ~
..
'
>
'fre~(ptr;tJ;
~,
_,,.~
,.pt.r.:=
' ~l
'}.'/lend .~hfle
~- ~. v-~--~~/~ "'' t
'
l~~
~ -~~
'
,.. ;, 'l ~
./
Sf -
,..._
,' __ '_,,_,',,--::--'-:"-.,=-[~-"'.~....:,=-~-:'--,---~---1
:7
: :;:
!!t;ib
~.!
f:..f~
~
''
"!'
':;
~~
,.
,,
~ ~
~:."
</'
" '~
Entlserri,Autumn; 201;2713 .
"''"""'"'"'-'"""'"
'
~~
'
. .-.
-:1torr22 p:~ges:~
~'
..
,.
PDS' (CSllOOl)
Roll:
Sec:
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
'
,,
"
' ,,
'-
DS,_RSC,
AM, , _
,
",~ ~,-,~.....,~ ,..,.:.;.. - ,,. ""
1
.,.,.
'
'
6