criteria used for judging an algorithm. 1. Step by step Procedure to solve a Particular problem is called ALGORITHM 2. Alorit!m ca" also be called as Pseudo # $ode %. Alorit!m ca" be de&"ed as a 'ell#de&"ed computatio"al procedure t!at ta(es si"le value or a set o) values as i"puts a"d produces a si"le or multiple values as output. *. T!us it is a se+ue"ce o) steps '!ic! tra"s)orms a" i"put i"to a" output. ,. A si"le problem may !ave multiple alorit!ms. Also- a" alorit!m is al'ays a la"uae#)ree computatio"al steps. .. I" order to compare alorit!ms- 'e must !ave some criteria to measure t!e e/cie"cy o) our alorit!ms. Suppose a" alorit!m !as m i"put data. T!e time a"d space used by t!e alorit!m are t!e t'o mai" measures )or t!e e/cie"cy o) t!e alorit!m. 0. T!e time is measured by cou"ti" t!e "umber o) (ey operatio"s1 i" sorti" a"d searc!i" alorit!ms- 2. T!e space is measured by cou"ti" t!e ma3imum o) memory "eeded by t!e alorit!m. Characteristics of an Algorithm 4ac! a"d every Alorit!m s!ould satis)y t!e )ollo'i" $riteria5 a. I"put b. Output c. 6i"ite"ess d. 7e&"ite"ess e. 48ective"ess !"am#le Write an algorithm to $nd the roots of %uadratic e%uation for all the cases. Step 1: Input A, B, C Step 2: Set D = B * B 4 * A * C Step 3: If D > 0 then : (a)Set X1 = (-B + SQRT(D))/ 2 * A (b)Set X2 = (-B - SQRT(D))/ 2 * A (c) Print X1, X2 Else if D = 0 then: (a)Set X = -B / 2 * A (b)Print X Else Print Roots are imaginary End If Step 4: Exit 1 b) What is a &o'chart? !"#lain the di(erent s)m*ols used in a +,-WC.A/T. 1. A 9o'c!art is t!e pictorial represe"tatio" o) a" alorit!m. 2. T!e various steps i" a" alorit!m are dra'" i" t!e )orm o) prescribed symbols. %. T!e 9o' o) t!e alorit!m is mai"tai"ed i" a 9o'c!art. *. T!e various symbols used i" a 9o'c!art are as belo'. !"am#le 0. a. What is a 1aria*le2 .o' it can *e declared2 initiali3ed 'ith e"am#les. *. Write a C #rogram to s'a# 0 varia*les using third varia*le and 'ithout using third varia*le 1. A :ariable !as a "ame a"d type - used to !old some data temporarily. 2. T!e value o) a variable ca" be altered; modi&ed later at a"y time i" t!e proram %. 7eclaratio" o) a variable5 Sy"ta35 <data type = <variable#"ame=> 435 # i"t a- b- c> I" t!e above declaratio" a-b a"d c are i"teer variables '!ic! ca" store some i"teer data 9oat c-d-e> I" t!e above declaratio" c-d a"d e are 9oat variables '!ic! ca" store some real data 2. I"itiali?atio" o) a variable5 i"t a-b-c> ;; a- b- c are declared a@1A> b@2A> c@%A> ;; a-b-c are i"itiali?ed 'it! some values - t!is (i"d o) i"itiali?atio" is called as static i"itiali?atio" . a-b-c ca" also be i"itiali?ed dy"amicallyB at ru" timeC sca")BDEdEdEdF-Ga-Gb-GcC> ;H proram to s'ap 2 variables usi" t!ird variable H; Ii"clude<stdio.!= Ii"clude<co"io.!= ;; i"clusio" o) !eader &les void mai"BC ;; e"try poi"t )or e3ecutio" J i"t a-b-temp> pri"t)BD4"ter t!e values )or a a"d bK"FC> sca")BDEdEdF-Ga-GbC> ;; readi" a-b values at ru" time
temp @ a> a @ b> b @ temp> pri"t)BDvalues o) a a"d b a)ter s'appi" Ed EdK"F-a-bC> etc!BC> L ;H proram to s'ap 2 variables 'it! out usi" t!ird variable H; Ii"clude<stdio.!= Ii"clude<co"io.!= ;; i"clusio" o) !eader &les void mai"BC ;; e"try poi"t )or e3ecutio" J i"t a-b> clrscrBC> pri"t)BD4"ter t!e values )or a a"d bK"FC> sca") BDEd Ed F-Ga -GbC> ;; readi" a -b values at ru" time a @ aMb> b @ a#b> a @ a#b> pri"t)BDvalues o) a a"d b a)ter s'appi" Ed EdK"F-a-bC> etc!BC> L 2 $C. i 43plai" Constants and Different data types. ii In How many ways constants can be defined
b. I" t!ree 'ays co"sta"ts ca" be de&"ed i" a $ proram iC usi" I de&"e e35# Ide&"e PI %.1* Ide&"e SIN4 2AA Ide&"e OAPP4R DQ4L$OM4F Q!e"ever 'e use t!e above co"sta"ts t!e occurre"ce o) eac! PI 'ill be replaced 'it! its value. iiC usi" (ey'ord co"st co"st i"t si?e@1AA> co"st 9oat pi@%.1*%2> iiiC usi" (ey'ord e"um e"um dayJmo"-tue-'ed-t!u-)ri-sat-su"L> e"um Ooolea" Jtrue-)alseL> 4. 5istinguish *et'een the follo'ing 6i) Actual and formal arguments Actual argument1t!ese are t!e arume"ts- '!ic! are speci&ed i" t!e )u"ctio" call or calli" )u"ctio" 435# a @ cd B3- yC. Here 3 a"d y are called t!e actual arume"ts +ormal arguments1t!ese are t!e arume"ts used i" t!e u"ctio" declaratio". 43 5# co"sider i"t cd Bi"t m- i"t "C J ;; code )or &"di" t!e cd )or m-" L Here m a"d " are called t!e )ormal arume"ts 6ii) 7lo*al and local varia*les 7lo*al varia*le1t!e variables- '!ic! are declared outside all t!e )u"ctio"s Bi"cludi" t!e mai"C is- called as lobal variable. T!ey are available to all t!e )u"ctio"s. 435 # i"t a> 9oat b> void mai"BC J i"t c> c!ar d> L I" t!e above e3ample a-b are lobal variables- c-d are local variables. ,ocal varia*le1t!e variables- '!ic! are declared 'it!i" a )u"ctio"- are called local variables. T!ey ca" be used o"ly 'it!i" t!e )u"ctio" i" '!ic! it is declared. 6iii) Automatic and static varia*les Automatic varia*le1t!ey are created '!e" t!e )u"ctio" bloc( is e"tered a"d removed '!e" t!e )u"ctio" is termi"ated. Oy de)ault- all t!e variables are automatic variables. 435 mai"BC J i"t a> static i"t b> L I" t!e above e3ample i"t a ca" also be co"sidered as auto i"t a '!ic! mea"s all local variables are automatic by de)ault as t!ey are created a"d deleted automatically.
T!e variable b is a static variable '!ose value 'ill be persiste"t i" bet'ee" t!e )u"ctio" calls 8tatic varia*les1t!ey are variables- '!ic! are declared local to a )u"ctio". Ho'ever t!ey are available eve" outside t!e )u"ctio" i" '!ic! t!ey are declared. *. Orie9y 43plai" $o"trol Structures available i" $ la"uaeR This deals with the various methods that C can control the flow of logic in a program. Control statements can be classified as un-conditional and conditional branch statements and loop or iterative statements. The Branch type includes: . !n-conditional:
goto brea" return continue #. Conditional:
if if $ else %ested if switch case statement &. 'oop or iterative: for loop while loop do-while loop Conditional (tatements: (ometimes we want a program to select an action from two or more alternatives. This re)uires a deviation from the basic se)uential order of statement e*ecution. (uch programs must contain two or more statements that might be e*ecuted+ but have some way to select only one of the listed options each time the program is run. This is "nown as conditional e*ecution. if statement: (tatement or set of statements can be conditionally e*ecuted using if statement. Here+ logical condition is tested which+ may either true or false. If the logical test is true ,non -ero value. the statement that immediately follows if is e*ecuted. If the logical condition is false the control transfers to the ne*t e*ecutable statement. The general synta* of simple if statement is: if ,condition. statement_to_execute_if_condition_is_true/ or if ,condition. 0 statement / statement #/ 1 1 1 1/ } Flowchart Segment: if $ else statement: The if statement is used to e*ecute only one action. If there are two statements to be e*ecuted alternatively+ then if-else statement is used. The if-else statement is a two way branching. The general synta* of simple if - else statement is: if ,condition. statement_to_execute_if_condition_is_true/ else statement_to_execute_if_condition_is_false/ 2here+ statement may be a single statement+ a bloc"+ or nothing+ and the else statement is optional. The conditional statement produces a scalar result+ i.e.+ an integer+ character or floating point type. It is important to remember that an if statement in C can e*ecute only one statement on each branch ,T or 3.. If we desire that multiple statements be e*ecuted on a branch+ we must block them inside of a { and } pair to ma"e them a single compound statement. Thus+ the C code for the flowchart segment above would be: 3lowchart (egment: (tatement if ,Conditio n. T 3 Example: main,. 0 int num/ printf,4 5nter a number : 4./ scanf,46d7+8num./ if ,num 6 # 99 :. printf,4 5ven %umber 4./ else printf,4 ;dd %umber 4./ < %ested if statement: The AS! standard specifies that "# le$els of nesting must be supported% !n &' an else statement alwa(s refers to the nearest if statement in the same block and not alread( associated with if% 5*ample: main)* 0 int num/ printf,4 5nter a number : 4./ scanf,46d7+8num./ if, num = : . 0 if, num 6 # 99 :. printf,45ven %umber4./ else printf,4;dd %umber4./ < else 0 if, num > : . printf,4%egative %umber4./ else printf,4 %umber is ?ero4./ < (tatement (tatement # if ,Conditio n. T 3 < Flowchart Segment: (tatement Condition # Condition T 3 T 3 if-else-if 'adder: 2hen faced with a situation in which a program must select from many processing alternatives based on the value of a single variable+ an analyst must e*pand his or her use of the basic selection structure beyond the standard two processing branches offered by the if statement to allow for multiple branches. ;ne solution to this is to use an approach called nesting in which one ,or both. branch,es. of a selection contain another selection. This approach is applied to each branch of an algorithm until enough additional branches have been created to handle each alternative. The general synta* of a nested if statement is: if ,expression. statement1 else if ,expression. statement2 %% %% else statement3 Example: @include >stdio.h= void main ,void. 0 int %/ AB Cenu Choice BA printf ,DC5%! ;3 T5EC(FnFnD./ printf ,D. (ingleFnD./ printf ,D#. DoubleFnD./ printf ,D&. TripleFnD./ printf ,DG. HuadrupleFnFnD./ printf ,D5nter the numbe ,-G.: D./ scanf ,D6dD+ 8%./ if ,% 99 . printf ,DoneD./ else if ,% 99 #. printf ,DtwoD./ else if ,% 99 &. printf ,DthreeD./ else if ,% 99 G. printf ,DfourD./ else printf ,D5EE;ED./
< The I : operator ,ternary.: 5EE;E 3;!E THE55 T2; ;%5 % 9 % 9 # % 9 & % 9 G 3 3 3 3 T T T T The I ,ternary condition. operator is a more efficient form for e*pressing simple if statements. It has the following form: expression1 I expression2 : expression3 It simply states as: if expression1 then expression2 else expression3 Example: Jssign the ma*imum of a and b to -: main,. 0 int a+b+-/ printf,4Fn 5nter a and b 4./ scanf,46d6d7+8a+8b./ - 9 ,a = b. I a : b/ printf,4Ca*imum number: 6d7+ -./ < which is the same as: if ,a = b. - 9 a/ else - 9 b/ The switch case statement: The switch-case statement is used when an e*pressionKs value is to be chec"ed against several values. If a match ta"es place+ the appropriate action is ta"en. (witch case statement is also "nown as multi conditional statement . The general form of switch case statement is: switch ,e*pression. 0 case constant: statement/ brea"/ case constant#: statement/ brea"/ default: statement/ brea"/ < In this construct+ the e*pression whose value is being compared may be any valid e*pression+ including the value of a variable+ an arithmetic e*pression+ a logical comparison rarely+ a bit wise e*pression+ or the return value from a function call+ but not a floating-point e*pression. The e*pressionKs value is chec"ed against each of the specified cases and when a match occurs+ the statements following that case are e*ecuted. 2hen a brea" statement is encountered+ control proceeds to the end of the switch - case statement. The brea" statements inside the switch statement are optional. If the brea" statement is omitted+ e*ecution will continue on into the ne*t case statements even though a match has already ta"en place until either a brea" or the end of the switch is reached. The "eyword case may only be constants+ they cannot be e*pressions. They may be integers or characters+ but not floating point numbers or character string. Case constants may not be repeated within a switch statement. The last case is a special "eyword default. The default statement is e*ecuted if no matches are found. The default is optional and if it is not present+ no action ta"es place if all matches fail. Three important things to "now about switch statement: . The switch differs from the if in that switch can only test for e)uality whereas if can evaluate any type of relational or logical e*pression. #. %o two case constants in the same switch can have identical values. But+ a switch statement enclosed by an outer switch may have case constants and either same. &. If character constants are used in the switch statement+ they are automatically converted to integers. Flowchart Segment - &ase Selection: In the e*ample below+ five possible paths might be followed depending on the value stored in the character storage location L. 5ach path is selected based on the individual value,s. that might be stored in L. Example ":
main,. 0 char gender/ printf ,45nter Mender code:,CA3.4./ scanf ,46c7+ 8gender./ switch ,gender. 0 case NCK : printf ,4 Cale4./ brea"/ case N3K : prrintf ,43emale7./ brea"/ default : printf ,42rong code4./ < < 2e can also have null statements by Oust including a 4+7 or let the switch statement fall through by omitting any statements ,see example below.. Example ,: switch ,letter. 0 case PJQ: case P5Q: case PIQ : case P;Q: (tatement Brea" (tatement Brea" (tatement Brea" switch ,e*pression. Default (tatement %e*t (tatement R R R % % % % case match I case # match I case n match I case P!Q: number1of1vowelsSS/ brea"/ case N Q: number1of1spacesSS/ brea"/ default: number1of1constantsSS/ brea"/ < In the above e*ample if the value of letter is PJQ+ P5Q+ PIQ+ P;Q or P!Q then number1of1vowels is incremented. If the value of letter is P Q then number1of1spaces is incremented. If none of these is true then the default condition is e*ecuted+ that is number1of1constants is incremented. 'ooping and Iteration:
'ooping is a powerful programming techni)ue through which a group of statements is e*ecuted repeatedly+ until certain specified condition is satisfied. 'ooping is also called a repetition or iterative control mechanism. C provides three types of loop control structures. They are: for statement while statement do-while statement The for statement: The for loop statement is useful to repeat a statementAs a "nown number of times. The general synta* is as follows: for ,initiali-ation/ condition/ operation. statement/ The initiali-ation is generally an assignment statement that is used to set the loop control variable. The condition is an e*pression,relationalAlogicalAarithmeticAbitwise T.. that determines when the loop e*ist. The .peration defines how the loop control variable changes each time the loop is repeated. 2e must separate these three maOor sections by semicolon. The for loop continues to e*ecute as long as the condition is true. ;nce the condition becomes false+ program e*ecution resumes on the statement following the for. The control flow of the for statement is as follows: Example ": AA printing all odd and even numbers between to U int */ main ,. 0 for ,*9/ * >9U / *SS. 0 if, * 6 # 99 : . printf, 4 6d is 5V5% Fn7+*./ else printf,4 6d is ;DD Fn7+*./ < < .utput to the screen: is ;DD # is 5V5% & is ;DD G is 5V5% U is 5V5% Example ,: AA sum the s)uares of all the numbers between to U main,. 0 int *+ sum 9 :/ for ,* 9 / * >9 U/ * SS. 0 sum 9 sum S * B */ < printf ,4Fn (um of s)uares of all the numbers between to U 9 6d 7+ sum./ < .utput to the screen: (um of s)uares of all the numbers between to U 9 UU Flowchart Segment - for Statement: The comma ) ' * operator is used to e*tend the fle*ibility of the for loop. It allows the general form to be modified as follows: for ,initiali-ation1+ initiali-ation1#/ condition/ operation1+ operation1#. statement/ Jll the following are legal for statements in C. The practical application of such statements is not important here+ we are Oust trying to illustrate peculiar features that may be useful: . for ,*9:/ ,,*=&. 88 ,*>W../ *SS. #. for ,*9:+y9G/ ,,*=&. 88 ,y>W../ *SS+ yS9#. &. for ,*9:+ y9G+ -9G:::/ -/ -A9:. The second e*ample shows that multiple e*pressions can be separated by a + ,comma.. Example: main,. 0 int O / double degC+ deg3/ clrscr ,./ printf ,4Fn Table of Celsius and 3ahrenheit degrees FnFn7./ printf ,4Celsius Degree Ft 3ahrenheit Degree Fn4. degC 9 -#:.:/ for ,O 9 / O >9 X/ OSS. 0 degC 9 degC S #:.:/ deg3 9 ,degC B W.:AU.:. S &#.:/ printf ,4Fn 6Y.#lfFtF 6Y.#lf 4+ degC+ deg3./ < < Initiali-ation Condition Increment (tatement in The loop %e*t (tatement ;utput: Table of Celsius and 3ahrenheit degrees Celsius Degree 3ahrenheit Degree :.:: #:.:: G:.:: X:.:: Z:.:: ::.:: &#.:: XZ.:: :G.:: G:.:: YX.:: ##.:: %ested for loop: %ested loops consist of one loop placed inside another loop. Jn e*ample of a nested for loop is:
for ,initiali-ation/ condition/ operation. 0 for ,initiali-ation/ condition/ operation. 0 statement/ < statement/ < In this e*ample+ the inner loop runs through its full range of iterations for each single iteration of the outer loop. Example:[rogram to show table of first four powers of numbers to W. @include >stdio.h = void main,. 0 int i+ O+ "+ temp/ printf,DIFtI\#FtI\&FtI\G FnD./ printf,D--------------------------------FnD./ for , i 9 / i > :/ i SS. AB ;uter loop BA 0 for ,O 9 / O > U/ O SS. AB st level of nesting BA 0 temp 9 / for," 9 :/ " > O/ " SS. temp 9 temp B I/ printf ,D6dFtD+ temp./ < printf ,DFnD./ < < ;utput to the screen: I I \ # I \ & I \ G -----------------------------------
# G Z X & W #Y Z G X XG #UX U #U #U X#U X &X #X #WX Y GW &G& #G: Z XG U# G:WX W Z Y#W XUX