0% found this document useful (0 votes)
15 views15 pages

C Solution 2005

Uploaded by

pal545490
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)
15 views15 pages

C Solution 2005

Uploaded by

pal545490
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
You are on page 1/ 15

PROBLEM SOLVINGTHROUGH

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

(i) Integral type -Th is is agai n divi ded in categories - prim


ary
data type s are inte ger, char acte r, floa ting poin ts and dou
ble
precision floating point. Man y of them also offer exte nded
data
types e.g. long int, long doub le.
(a) Integer type s - C has thre e clas ses of inte ger stor age,
namely shor t int, int and long int with signed and unsi gned form
s.
(b) Floating poin t type s - Real num bers are defi ned in C by
float whe n accuracy prov ides by a float num bers is not sufficien
t;
the type double can be used real num ber are stor ed in 32 bit.
A
double data type num ber uses 64 bits giving a precision of
14
digits.
(c) Character types - A single character as a char data type. It
is stored in 8 bits that is one byte.
Size and range of data type
-
Type Size (Bits)
Char 8
Range I
-128 to 127
Unsi gned Cha r 8 0 to 255
int 16 -32768 to + 32767
unsig ned int 16 0 to 6555
C
short int 8 -128 to 127
long int 32 -2147423648 to + 2147483648
float 32 3.4 E - 38 to 3.4 E + 38
_double 64 1.7 E - 308 to 1.7 E + 308
long doub le 80 3.4 E - 4932 to 1.1 E + 4932
2- User defin ---- - - - --
ed type declaration
(i) "Ty pe de finiti o n" tha t a ll o ws ust:!rS to d efi ne fin ide
that wou ld repr ntifie r
esen t a n existin g data type . The user d efi ned d ,1 ra
type iden tifier can la ter be used to d ccl .HIJ vnrin blcs . It'
5 ge nera l
fo-rm is

\ ty pt·de f ty p,· iJcn titil'r'$


l·or exam ple ty pc dcf float nMrk s,
type d e f int un its;
. e Solution to BCA-1
eomprebensiv

216 . ely in t & float.


.
res pe ctl V
bo ~s ,
. an d ma rks sy 111 . ble follow s :
He re uruts s lat er as
declare va na
be used to
Th ese ca n h2·
·t batch1, ba te ' .
Ulll ks name1 l50], name2l5
0], d as 50 ele me nt s flo ati ng
mar
1(501 & name2(::,
01 are declare
I'."

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

. 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

1. It is an entry control loop.


It is an exit contro l loop.
2. Statements will
executed if h . -~ot be State1 nents will be executed
false. t e condit ion gets at least once wheth er the
3. condi tion is true or false.
It is some what typica l thell
4. It's synt . While.
·1 ax is initialize·
w hI e (conct ·ti , It's syntax is initial ize;
f l On)
do
Statem ent·I {
increm ent/ D
} ecrell\e nt; statem ent;

L
--- Incre1 nent/ Decren1ent;
)
------ ------ - While (cond ition);
219
Problem Solving through CProgramming

5. For example For example


I I

main ( ) main ( )
I

{ int 1;
I

int i; i=l;
i=l; do
while (i<ll) {
{ printf ( "%d\n", i) ;
I I

printf ( "o/od\n", i) ; 1 = 1 +1;


i + = 1; }
} while (i<ll);
getch ( ) ; getch () ;
} }

(b) #include <stdio. h>


#include<conio.h>
main ()
{
void fibbo (int);
int num;
clrscr ();
printf ( "Enter number of terms");
Scanf ( "%d", & num);
fibbo (num) ;
getch () ;
}
vo id fibbo (int n)
{

inti, a=l, b=l, c;


printf ("%d\t", a) ;
printf ("%d\ t ", b);
for (i=l; i <=n-2; 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

'---~ -:- --= ~- --- arguments in the calling portl


- - ~ the program.
(ii) Global and Iocal 007 Q.S
variabI
e : Please refer Paper 2
Problem Solving through CProgramming
221

(iii) Automatic & Static varia ble


Sr. Automatic Static
No.
1. Automatic variable Static variables are local
know n to only to the variables which exists and
function in whic h it is retains its value even after the
declared control 1S transferred to the •
calling function
2. Auto lS default by It is necessary to put a prefix
default a variable is auto ''static" if any yri abl e lS
type declared as static
3. It used "Auto" key word It used 'Static" keyword
4. It is completely a local It can behave either a local
or
variable global variable
5. Auto variable can be A static variable is initialized
initialized more than only once it is never initialized
once again
6. It's lifetime is local and It's lifetime to global and
scope
scope is only upto the is also outside from the function
function that it 1S that it is declared
declared

(b) A function is called recursive if a statement with in the


body of a function calls in same function. Calculating factorial
of
over in·teger quantity.
#inc lude <std io . h>
long int fact oria l ( int n) ;
main ()
{

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 ()
{

cha r line (30) I s (10 ] •


,
lnt i • '
, JI C = 0 k fl
..c l rsc r {) ; ' , ag =O;

Pri ntf ("


en ter th l'
get s (li ne) ; e in e of te xt - u) ,·
Pr int f ("
en t er th
aca.nf ("\ e • e Word to se ar ch - ") ;
f or ( . ' &ta) ;
h,Q li
{ ne ( i) ::: ' \ 0'
, i++ )
if
( o lo l
'= 11:
( lin e ( i] )
~. ;.;. i ,
f ot ( j •
1,
,
s (j )
I ::: '\0 '; i+ +)
Problem Solving through CProgramming 223

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

11-M: then the first value will ~


~u d a value
· assigne
ber 15 lue
another nu~ the second va · h emory to hold the largest
0 veriapped Y_ r allocates enoug ~ry space is shared, this is a
The cornptle . Since the mem
f the union.
rnernber o the memory.
way to save
Syntax . e name;:,,
Union <UlllOn typ
;:,,·
> <variable names '
<type

} [~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

(b) To add two matrix here is the function


int addmat (int (*x) [3], int (*y) [3], int (*z) [3])
{
inti, j ;
for (i = O; i <3; i++)
for (i=O j<3; j ++)
{
* (*(z+i)+j) = * {* (X+l.I ) +JI ) + * ( * (y (y+1.' ) +]') ,•

}
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 ();
}

(b) #in clu de <st dio . h>


#in clu de <co nic . h>
voi d mai n ()
{

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") ;

pri ntf (" nUll\be:r ,


get ch {); .is Odd"}
}

You might also like