C Solution 2005
C Solution 2005
C PROGRAMMING
2005
Q.1 (a) Define a flowch art write the advan tages & limita tions
of a flowchart?
(b) \j'Vhat is debug ginrj Expla in the progra m verific ation
and progra m testing .
Ans.:
(a) See 1n 2006 Q.1 (6)
(b) After writin g the program, errors may occur. This error
may seen when a progra mmer either compile or run the progra m
so for capturing & solving these errors there one some metho ds
available. Methods are available for finding the location of execution
errors and logical errors within a progra m. Such metho ds are
generally referred to as "debugging techniques" some of the more
commonly used debugging techniques are described below progra m
can verified and tested by the following techniques.
1. Errors isolation.
2. Tracing.
3. Watch values.
4. Break points.
1. Error isolati on - Error isolation is useful for locating an
error resulting in a diagnostic message if the general location of
the error is not known it can frequently be found by temporarily
deleting a portio n of the progra m and then rerunn ing the progra m
to see if the error disapp ears. The tempo rary deleti on with
comment markers (/* and */),ca using the enclosed instructions
to become comm ents if the error messa ge then disapp ears, the
deleted portion of the progra m contains the source of the error.
A closely related techni que is that of inserti ng severa l uniqu e
Printf statements like :
printf (" debug ging - line ; 1 \n");
prinft (" debug~ing - line 2\n"); etc.
Comprehensive Solution to BCA-1
214
within the pro ~am nts to
At various places . 1 the use of prin tf stat eme
. - Tracing mvo ves tain key variables, or to d.1splay
2. Trac ing . d
s within
display the values assign~edt:~:;nally at vari ous location
the values that are cakfula ti' n serves sev eral pur pos es e.g. it
•
h This in ormallyO assigned to certain •
vari able s really
t e prog ram . tu I ·
1
verifies that the va ues ac a 1
. dtiff1s not
are the values that should be assigned to those va ues erent
f . d that the actu al assigned valu es are
uncommon to m
than those expected.
able or
3. Watch values - A watch value is the valu e of a vari
pro gra m
an expression which is display con tinu ous ly as the
e as they
executes. So a user can see the changes in a wat ch valu
a few
occur, in response to the program logic. By mo nito ring
ne where
carefully selected watch values, user can often dete rmi
the program begins incorrect or unexpected values.
g point
. 4.' Breakpoints - A breakpoint is a temporary stop pin
~ith in ~program.Each breakpoint is associated wit .
h a particular
instruction within the pro gram when th e pro gra m 1s executed·
~~ pro :a~ execution iswill temporarily stop at the bre akp din;
b
ore e instruction executed · Th e exe cuti. on may then e
resumed , unti"l th e next breakp · t ·
are often used in co . . om is encountered. Break points
current watch valu;Jautnctiohnbwifu po. watch values by observing the
eac reak int as the pro gra m executes.
Q.2 (a) De •be th .
sen e basic four d ta
the range of values th a types how cou ld we extend
(b) Expl • . ey represent?
a1n the increment &
examples . dec rem ent ope rato r with
Ans.:
(a) Ther e are four
l • Primary or f types of data
undament l
2. Use r defin ed data a data types.
3. Derived dat types.
4 s· ly data atytypes.
. imp
1 Pr' Pes.
· •mary data type s _
The re are basica ll
Y tw o ty
~ s of d
ata typ e_
Problem Solving throu gh CProgramming 215
name .
. t arr ay va ria b1es
pcm th us er define d a ta n, ..;
pe 15
ty e · An
(ii) En um era ted d:t ;t ispdefin o er tifi {value 1
ed as en um id en er
en um era ted da ta typ . '
v~lue 2,value 3, ... ,value n)
. kn ow n as en um er at ed
Th e va lue s en c1os e d in br ac es 1s
co ns tan t
en um da y {m on da y, tue sd ay ' dn es da y th ru sd ay ,
e.g. we '
friday ; sat urd ay)
en um mo nth Ua nu ary ,fe bru ary b€ }
, ... ,Decem r
3. Derived Data Ty pe :
Derived da ta typ es are arr ay s,
function an d po int ers .
(i) Fu nc tio n - A function is
a sel f co nt ain ed pr og ra m th
per for m a par tic ula r tas k at
Calling Function : A function
tha t is inv olv ing or ca lli ng an
function is ref err ed as cal lin g fun other
ction lik e ma in O fu nc tio n,
Fu nct ion Pro tot y pe : It is decla
ration of fun cti on me an s wh at ,vi
be the return data type, na me ll
& arg um en ts of a fu nc tio n :
Fu nct ion Definition : It is a blo
ck of sta tem en ts th at is de cla
the abo ve of the pTo gra m. It ha red i.fl
s a certain se t of sta tem en ts ·w
are used lo solve a specilic pro hic h
ble m.
(ii) AITay : lt is
sto red con tin uo usl y a cold
le ti
c ?n ° f
ho rn og ou s data ele me nts
un er a sm gle na me . Iten ca n be
1 . Simple arr ay (on e dim en
s·
. 1ona l)
2. Multi dim en sio na l arr a
y. .
(iii) PointeTs : A po ·, , ,,. -
0
sto re the ad dre ss of som e ~r 1s a s ·
o th pe cia l va ria ble tha t is u s ed to
to sto re the ad d res s of so moer
lvhariable . A pointer ca n be us
use d to sto re the ad dre ss of"" 0 e r v · b ed
a . ari a le . A po int er ca n be
un ion or ev e n a po inte r.
s ,ng le va ria ble , arr ay str uc
tuJ e,
Problem Solving through CProgramming 217
(d) Empty data type: Empty data types are used in functions .
We can extend the range of data type by applying long, short
as a prefix e.g. long int or long double.
(b) Increment and decrement operator
The increment operator (++) adds one (1) to the operand .
The operator (--) subtracts one (1) from the operand. The
increment or decrement can be either post or pre.
A prefix operator first adds 1 to the operand and then result
is assigned to the variable on the left.
e.g. int m = 5;
y = ++ m;
The second statement shows that first the value of m is used
then it increase by 1 and then the value will assign tom & then y.
Program
main ()
{
int a= 5, b = 4;
a++ ;
b -- .
'
printf ( "%d %d", a, b);
}
Output 6, 3.
(a) Postfix
main ()
{
int a= 5, b;
b = a ++.
'
Pr i ntf ("%d %d", a' b) ;
}
output 6, 5
I, •
Comprehensive Solution to BCA.J
218
(b) Prefix
main ()
{
int a= 5, b,
b = + + al'
print f ("%d, %d", a, b);
}
Output 6,6
Q.3 (a) Differentiate betwe en while loop & do while loop with
example
(b) Write a progra m using a do while loop to calcul ate print
the first 50 fibonacci numb er.
Ans.:
(a)
Sr. While Do Whil e Loop
No
L
--- Incre1 nent/ Decren1ent;
)
------ ------ - While (cond ition);
219
Problem Solving through CProgramming
main ( ) main ( )
I
{ int 1;
I
int i; i=l;
i=l; do
while (i<ll) {
{ printf ( "%d\n", i) ;
I I
c==a+bi
printf ("%d \t ", c ) 1
a =b1
b=c ;
Comprehensive SolutiontoBCA--I
120
. h betw een the tolJO"' tng
.
(a) Disnngu1s f I argu men t
Q ·4
(i) Actual and orma
(ii) Global and loca l va riab le
· ·abl e
omatic & StattC van .
(iii) Aut
use rec ursion.
(b) What is recursion? Write any program
Ans.:
(a) (i) Actual and formal argument
Formal
Sr. Actual
No. The argum ents are called form al
1. The corresponding
m the arguments, becaus e they
arguments
function refe rence are represen t the names of data
called actual arguments. items tha t are transferred into
the fu nction fr om the calling
portion of the pro gram.
wn so
2. These are simply kno wn These are also kno
or formal
as arguments are actual parameters
parameters. _
arguments. 0
nam es f
3• . e define the data These represent the
Thes
items iliat are actual by data items that are
transferred
11:~
~~ fur r~ · .
mto the function from the ca w•g
por tion of the program. __
4 Th
. ese . ed These are a copy of actual
are fnotthreco gruz
outside 11ot
0 e functi arg ume nts so cha nge s
behave lik on
variable. e a local directly affect on the aclllal
parameters. __.---;
5. Actual a - al
. rguments of the foJ1l l
havmg unique name are
fo The names ,.,.,,e as
r arguments nee d not be S,iw l
th
em. th acttla
e names of the ·ofl
int n;
long int fact oria l (int n);
prin tf ( "n =");
scan f ( "%d", &n);
prin tf ( "n\=% ld \n", fact oria l {n)) ;
} .J
Comprehensive Solution to BCA-t
} 1 , (in t n)
, nt fac tor ia
long l
{
if (n<=l)
ret urn (1);
els e
ret urn (n *f ac tor ial (n- 1) ;
}
Q.5 (a) Write program which will rea
d a tex t an d co un t the
occurrence of a particular wo rd.
(b) Describe the two ways of pa ssi
ng pa ram ete rs of a
functions. When do you prefer to use
ea ch -of the m
Ans.:
(a) iinclu de <s tdi o. h>
ihn clu de <c on io. h>
voi d main ()
{
if (s [0] -- lin e [ i j)
{
fl ag = l;
bre ak;
}
}
i f (f l ag -- 0)
C + +;
}
}
if ( C 0) ==
pri nt£ ("n ot fou nd" );
els e
rpi ntf ( "co unt ing of a wor d =%d", c) ;
get ch ();
}
(b) Please refer Paper 2007 Q 6. (b ).
Q.6 (a) How does a stru ctur e differ from an arra y.
(b) Wh at is mea nt by the nest ed stru ctur e & arra
y of
structures? Give an exam ple of use of each of them .
Ans.:
(a) Please refer Pap er 2007 Q 7. (a).
(b) A stru ctur e can be defined as a deri ved data type
whi ch
can represent several different types of data in a single
unit. Each
indi vidu al data item with in the stru ctur es is refe rred
to as
members structure is the met had of packing the data of
diffe rent
types of organize the data in a more mea ning ful way . A
stru ctur e
definition creates a format that may be used to declare
stru ctur e
variable.
,, A stru ctur e defi nitio n is spec ified by usin g the
key wor d
Structure". This is followed by braces enclosin g the mem
there data types.
bers &
)
Problem Solving through CProgra
mming
225
st ru ct ad dr es s ad dr ;
} ;
m ain ()
{
st ru ct pe rs on P;
cl rs cr () ;
Pr in t£ ( "E nt er th e name
& ag e" ) i
Sc an £ ("%s % d" , &p. na
me , & p. ag e) ;
pr in t£ ( "e nt er th e ho us
e no , s t ru e t no , ci ty &
st at e" ) ;
Sc an £ ("%s %d"; p. ad dr
es s. ci ty , p. ad dr .
st at e) ;
pr in t£ (" pe rs on de ta il
s \n \n ") i
pr in t£ ( "Name : \t % s
\t Age : \ t %d \n ",
p . na me , p . ag e) ;
pr in t£ ( 'A dd re ss : % s"
, p. ad dr . h_ no );
pr in t£ ("%d \n ", p. ad
dr .s _n o) ;
pr in t£ (" \t % s, % s \n
", p. ad dr es s. ci ty ,
p. ad dr es s, st at e) ;
ge tc h () ;
}
Q.7 (a) W ha t is a union? Ho
w is it dif fer en t fro m a str
W ha t is the scope of the me uc tu re ?
mb er s of a un io n?
(b) Write a function using
po int ers to ad d tw o ma tri x
the calling function. & to
Ans.:
(a) Union are also de riv ed da
ta types. Th ey ar e also us ed
group together a nu mb er of to
variables. But the difference
all the members of the un ion is th at
sh ar e the sa me me mo ry loc
therefore only one member ati on ,
of the un io n is ca n sto re va
at one time. A un ion is a va lid va lu e
riable th at is us ed to sto re
different types at different tim da ta of
es.
The un ion is mu ch like a str
uc tur e. Th e di ffe re nt va ria
defined in the union are calle bl es
d me mb ers of the union. Th
not be of the same da ta type. ey ne ed
If one me mb er contains a va
lu e an d
)
eomprehensive Solution to BCA-i
} [~union variables>];
e.g.
union data
{
char c;
inti;
long l;
float f;
} dl, d2;
Scope of the members
. h because
In the above example dl 7 d2 will get 4 bytes eac e
O
the highest members of the union is of 4 bytes. If there . ; be
more than one highest byte member than only one wt
considered
Difference between structure & union
Sr. Structure Union
No.
1. It uses keyword stauct . Jl
. It uses a keyword un10 t
2. Ob1ect of a structure Will b . . will ge,
get memory equal t bJect of a union fl'\ol')
th
sum of memory of a~l e memory equal to the ill~ u1e
0
members lhe of highest member
3. All the mcmbe~ union ~ -
..____._u_sc_~i~~ llancously be 0nly one member call
-- - valid value
U.1
Problem Solving through CProgramming
}
return (*(z)) ;
}
Q.8 (a) Explain the significance of the break statement in the
switch case construct. What would result in its address?
(b) Write a program to determine whether a given number
is odd or even.
Ans.:
(a) When any user wants to exit from a loop before it reach
to its last staten1ent then break statement or go to statement.
When a break statement is encountered inside a loop, the loop is
executed immediately exited and the control goes to the statement
followed by the loop a go to staten1ent can make errors in nested
loop but a break statement cannot make errors so it is mainly
used in nested if statement or switch case statement.
The switch statement tests the value of a given variable against
a list of case values and when a match is found a block of statement
associated with that case is executed after execution of statements
control must go outside from the switch case construct so we use
a break statement if this break statement is not used then all
c~ses will evaluated one by one. Here is an example to check
given character is vowel or not.
main ()
{
char ch;
clrscr () ;
}
Comprehensive Solution to 8
CA,1
228 \ n" );
, tf ( "en ter a ny ch ara ct er
pri n
( " 1 c" ; & ch) ;
sca n f
swi tch (ch)
{
cas e 'e' : cas e 'i ' : cas e , 0 ,
cas e 'a'
cas e 'u'
cas e \0 I
cas e ' A' : cas e ' E' : cas e 'I'
:
cas e 'U'
pri ntf ( "It i s a vowe l" ) ;
bre ak;
def aul t:
pri ntf ("I t i s a con s on ent " ) ;
bre ak;
}
get ch ();
}
int a;
clr scr {) ;
pri nt£ ( "en ter the
sca nf ("%d" num ber ") ;
I & a} •
'f /
l. (a % 2 == 0}
pri ntf ( "nu mbe r ,
els e l.s eve n") ;