Upload
vempadareddy
View
254
Download
0
Embed Size (px)
Citation preview
7/27/2019 C Lab Programs 1
1/49
LAB 1
ALGORITHMS AND FLOWCHARTS
A typical programming task can be divided into two phases:
Problem solving phaseo produce an ordered sequence of steps that describe solution of
problemo this sequence of steps is called an algorithm
Implementation phase
o implement the program in some programming language
Pseudocodeis an artificial and informal language that helps programmersdevelop algorithms. Pseudocode is very similar to everyday English.
Write an Pseudocode & A!orit"# to deter#ine a student$s %ina !rade and
indicate "et"er it is 'assin! or %aiin!( T"e %ina !rade is cacuated as t"ea)era!e o% %our #ar*s(
Pseudocode+
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50Print FI!"
else Print P##"
Detaied A!orit"#
Step 1: nput !1"!#"!$"!%
Step #: &'A(E )!1*!#*!$*!%+,%
Step $: if )&'A(E - /+ then Print 0A23 else Print 0PASS3 endif
7/27/2019 C Lab Programs 1
2/49
E4ample
,PASS-
Step 1: nput !1"!#"!$"!%
Step #: &'A(E )!1*!#*!$*!%+,%Step $: if )&'A(E -./+ then
Print 0A23
else
Print 0PASS3
endif
START
In'ut
M1.M/.M0.M
GRAD2 3M14M/4M04M56
IS
GRAD2789
,FAIL-
STOP
:N
Dra a %oc"art to %ind t"e su# o% %irst 89 natura nu#;ers(
7/27/2019 C Lab Programs 1
3/49
Dra a %oc"art to %ind t"e ar!est o% t"ree nu#;ers A.B. and C(
3Correct t"e ;eo one5
7/27/2019 C Lab Programs 1
4/49
Write an a!orit"# and dra a %oc"art to con)ert t"e en!t" in %eet to centi#eter
Pseudocode+
Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM)
7/27/2019 C Lab Programs 1
5/49
A!orit"#
Step 1: nput 2ft
Step #: 2cm 2ft 4 $/
Step $: Print 2cm
START
In'ut
L%t
Lc# L%t < 09
Lc#
STOP
Fo&c"art
Write an a!orit"# and dra a %oc"art t"at i read t"e to sides o% a rectan!eand cacuate its area(
Pseudocode
Input the width (W) and Length (L) f a rectangle
Calculate the area (!) by multiplying L with W
Print !A!orit"#
Ste' 1+ In'ut W.L
Ste' /+ A L < W Ste' 0+ Print A
FLOW CHART
7/27/2019 C Lab Programs 1
6/49
Write an a!orit"# and dra a %oc"art t"at i cacuate t"e roots o% a=uadratic e=uation
Hint+ d > s=rt 3 5. and t"e roots are+ "1 > 3?b4 d56/a and"/ > 3?b? d56/a
Pseudocode+#$ Input the cefficient% (a& b& c) f the 'uadratic e'uatin/( Calculate d0( Calculate "1( Calculate
7/27/2019 C Lab Programs 1
7/49
Dra a %oc"art %or co#'utin! %actoria N 3N@5
5here 67 8 1 9 # 9 $ 9 6 .
START
In'uta. ;. c
d s=rt3b " b? < a
7/27/2019 C Lab Programs 1
8/49
Assi!n#ent +
A!orit"# and %oc"art to %ind t"e roots o% =uadratic e=uationa
7/27/2019 C Lab Programs 1
9/49
IF?TH2N?2LS2 STRCTR2 ;he algorithm for the flowchart is as
follows:
If !)* then
print !
el%e
print *
endif
isAB
Print BPrint A
: N
Write an a!orit"# t"at reads to )aues. deter#ines t"e ar!est )aue and 'rints t"ear!est )aue it" an identi%in! #essa!e(
ALGORITHM
Ste' 1+ InputAL21. AL2/Ste' /+ if (AL21 AL2/5 then
MAE AL21el%eMAE AL2/endif
Ste' 0+ Print +The large%t ,alue i%-& M!
7/27/2019 C Lab Programs 1
10/49
Write an a!orit"# t"at reads t"ree nu#;ers and 'rints t"e )aue o% t"ear!est nu#;er(
ALGORITHM
Ste' 1+ Input N1. N/. N0Ste' /+ if (N1N/5 then
if (N1N05 thenMAE N1 N1N/. N1N0
el%eMAE N0 N0N1N/
endifel%e
if (N/N05 then
MAE AL21
Print+The large%t ,alue i%-&
M!
STOP
: N
START
In'utAL21.AL2/
MAE AL2/
isAL21AL2/
7/27/2019 C Lab Programs 1
11/49
MAE N/ N/N1. N/N0 el%eMAE N0 N0N/N1
endifendif
Ste' 0+ Print +The large%t number i%-& M!
IF?TH2N?2LS2 STRCTR2
;he algorithm for the flowchart is asfollows:
If !)* then
print !
el%e
print *
endif
is
AB
Print BPrint A
: N
Dra %o c"art to t"e a;o)e e
7/27/2019 C Lab Programs 1
12/49
9 "ours09 ;ut 9 "ours
/9 ;ut 09 "ours
19 ;ut /9 "ours
19 "ours
89909/919
Ste' 1+ InputNAM2.O2RTIM2.ABS2NTSte' /+ if 3O2RTIM2?3/605ABS2NT 95 then
PA:M2NT 89 el%e if (O2RTIM2?3/605ABS2NT 095 then
PA:M2NT 9el%e if (O2RTIM2?3/605ABS2NT /95 then
PA:M2NT 09el%e if (O2RTIM2?3/605ABS2NT 195 thenPA:M2NT /9el%e
PA:M2NT 19endif
Ste' 0+ Print ,Bonus %or-. NAM2 ,is -. PA:M2NT
Dra %o c"art to t"e a;o)e e
7/27/2019 C Lab Programs 1
13/49
int #ain3)oid5 'rint%3QQHeo ord@n$$5return />>>>>>>>>>>>>>>>>>>>>>>>>$include %stdio&h'
main()
* int sum6 value/
sum 1 30/value 1 35/
printf(+d-td-n+6 sum6 value)/
7/27/2019 C Lab Programs 1
14/49
Pro!ra# out'ut oo*s i*e1/ 1
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
$include %stdio&h'
main()*
int value36 value76 sum/
value3 1 85/value7 1 39/sum 1 value3 2 value7/
printf(+he sum of d and d is d-n+6 value36 value76 sum)/
JJJJJJJJJJJJJJJJJJJJJJJJJ
$include %stdio&h'
main()*
int count/char letter/
printf(+Count 1 d-n+6 count)/printf(+!etter 1 c-n+6 letter)/
Sa#'e 'ro!ra# out'ut=ount 8 #>%?%2etter 8 f
t can be seen from the sample output that the values which each of the variablestake on at declaration time are noJero. n =" this is common" and programmersmust ensure that variables are assigned values before using them.
f the program was run again" the output could well have different values for eachof the variables. 5e can never assume that variables declare in the mannerabove will take on a specific value.
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
7/27/2019 C Lab Programs 1
15/49
include %stdio&h'
$define :;/
balance 1 ?7&30/ta> 1 balance @ :; on &7f is &7f-n+6 balance6 ta> )/
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
6 Printin! Lar!e Boc* Letters6
Kincude 7stdio("
66)oid ;oc*!3)oid5 6Protot'e %or ;oc*! %unction 6
int #ain 3)oid5 'rint%3n5 ;oc*!35 'rint%3n5
6 Print out t"e Boc* etter ! 6)oid ;oc*!3)oid5 'rint%3!!!!!!n5 'rint%3! !n5 'rint%3!n5 'rint%3! !!!n5 'rint%3! !n5 'rint%3!!!!!!n5
6 It 'rints out+
!!!!!!! !!! !!!! !
http://www.cis.temple.edu/~ingargio/cis71/code/homework1.chttp://www.cis.temple.edu/~ingargio/cis71/code/homework1.c7/27/2019 C Lab Programs 1
16/49
!!!!!!
6
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
Write a 'ro!ra# to 'rint su# and a)era!e o% t"ree nu#;ers@
@include -stdio.h
int main)+ ,, no need to have int if no returnBfloat a"b"c"sum"avgCprintf)DPlease enter $ numbers and press enterD+Cscanf)DfffD"Fa"Fb"Fc+sum 8 a * b * cC
avg 8 sum,$Cprintf)DSum: f Avg: fD"sum"avg+Creturn /C /)a return 9
6 It 'rints out +
n /Un
>>>>>>>>>>>>>>>> 9 1 1 /
/ 0 1V 8 0/ V V X 1/ /8V
Y 81/19 19/11 /91/ 9YV10 1Y/1 1V018 0/XV1V V880V
6
6 add/(c JJ Add to nu#;ers and 'rint t"e# out to!et"er
it" t"eir su# ATHOR+ ZNTC2H DAT2+ 1/6906/919
6
Kincude 7stdio("
int #ain3)oid5
7/27/2019 C Lab Programs 1
18/49
int %irst. second
'rint%32nter to inte!ers 5 scan%3d d. &%irst. &second5 'rint%3T"e to nu#;ers are+ d dn. %irst. second5
'rint%3T"eir su# is dn. %irst4second5
6 addn(c JJ Read a 'ositi)e nu#;er N( T"en read N inte!ers and 'rint t"e# out to!et"er it" t"eir su#(6
Kincude 7stdio("
int #ain3)oid5 int n 6 T"e nu#;er o% nu#;ers to ;e read 6
int su# 6 T"e su# o% nu#;ers aread read 6 int current 6 T"e nu#;er [ust read 6 int c) 6 Loo' contro )aria;e. it counts t"e nu#;er o% nu#;ers aread read 6
'rint%32nter a 'ositi)e nu#;er n 5scan%3d.&n5 6 We s"oud c"ec* t"at n is rea 'ositi)e6
su# > 9 %or 3c)>9 c) 7 n c)445 'rint%3n2nter an inte!er 5 scan%3d.¤t5
6 'rint%3nT"e nu#;er as dn. current5 6 su# > su# 4 current 'rint%3T"e su# is dn. su#5 return 9
Assi!n#ent+
Na#e )arious "eader %ies 'resent in C
W"at are 're'rocessor co##ands and t"eir use \eords in C
Loca )s Go;a )aria;es in C
Data t'es in C 3in detai5
,H addn.c II 'ead a positive number 6. ;hen read 6 integers and
7/27/2019 C Lab Programs 1
19/49
H print them out together with their sum.H,
@include -stdio.h
int main)void+ B int nC ,H ;he number of numbers to be read H, int sumC ,H ;he sum of numbers already read H, int currentC ,H ;he number Just read H, int lcvC ,H 2oop control variable" it counts the number of numbers already read H,
printf)DEnter a positive number n D+Cscanf)DdD"Fn+C ,H 5e should check that n is really positiveH,
sum 8 /C for )lcv8/C lcv - nC lcv**+ B
printf)DKnEnter an integer D+C scanf)DdD"Fcurrent+C ,H printf)DKn;he number was dKnD" current+C H, sum 8 sum * currentC < printf)D;he sum is dKnD" sum+C return /C dn. su#5
return 9
Kincude directi)es+incudes decarations o% %unctions %ro# "eader %ies %or standardi;raries( For e> Sunda ]] T"eDa >> Saturda5'rint%3Hurra it is t"e ee*endn5ese'rint%3Curses sti at or*n5
return 9
7/27/2019 C Lab Programs 1
24/49
,HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH H 2nu#eration e
7/27/2019 C Lab Programs 1
25/49
6 2nu#eration e
7/27/2019 C Lab Programs 1
26/49
printf)D ;rue has a value of dD" ;'WE+Ca4;'rint%3d.c5!etc"3566getch)+ statement is a get character statement. t pause the output untiluser presses a key from the keyboard.
c 'ro!ra# to !enerate nu#;er ;eteen 1 to 199
@include-stdio.hmain)+Bint a81Cclrscr)+Cwhile )a-81//+
Bprintf)DKndD"a+Ca8a*1C