i unit _ cds material

Upload: ananth-nath

Post on 03-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 i Unit _ Cds Material

    1/23

    UNIT I

    1. a) What is an algorithm? Write the variouscriteria used for judging an algorithm.

    1. Step by step Procedure to solve a Particular problemis 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 aset o) values as i"puts a"d produces a si"le ormultiple 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"alsteps.

    . 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"dspace 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) (eyoperatio"s i" sorti" a"d searc!i" alorit!ms-

    . 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"putb. Output

  • 8/12/2019 i Unit _ Cds Material

    2/23

    c. 6i"ite"essd. 7e&"ite"esse. 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

  • 8/12/2019 i Unit _ Cds Material

    3/23

    1 b)What is a &o'chart? !"#lain the di(erent sm*olsused in a+,-WCA/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'.

  • 8/12/2019 i Unit _ Cds Material

    4/23

    !"am#le

  • 8/12/2019 i Unit _ Cds Material

    5/23

    0. a. What is a aria*le2 o' it can *e declared2initiali3ed '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 somedata temporarily.2. T!e value o) a variable ca" be altered; modi&ed laterat a"y time i" t!e proram%. 7eclaratio" o) a variable5

    Sy"ta35

    I" t!e above declaratio" a-b a"d c are i"teervariables '!ic! ca" store some i"teer data9oat 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

  • 8/12/2019 i Unit _ Cds Material

    6/23

    i"t a-b-c> ;; a- b- c are declareda@1>b@2>c@%> ;; 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-a-b-cC>

    ; proram to s'ap 2 variables usi" t!ird variable ;i"clude ;; 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" EdEdK"F-a-bC>

    etc!BC>

    ; proram to s'ap 2 variables 'it! out usi" t!irdvariable ;

  • 8/12/2019 i Unit _ Cds Material

    7/23

    i"clude ;; readi" a -b values at

    ru" timea @ ab>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>

    2 $C. i 43plai"Constants and Different data types.ii In How many ways constants can be defined

  • 8/12/2019 i Unit _ Cds Material

    8/23

    b.

    I" t!ree 'ays co"sta"ts ca" be de&"ed i" a $

    proramiC usi" de&"e

    e35#

    de&"e PI %.1*de&"e SIN4 2de&"e A4R DQ4L$OM4F

    Q!e"ever 'e use t!e above co"sta"ts t!eoccurre"ce o) eac! PI 'ill be replaced 'it! its value.

    iiC usi" (ey'ord co"stco"st i"t si?e@1>co"st 9oat pi@%.1*%2>

    iiiC usi" (ey'ord e"um

  • 8/12/2019 i Unit _ Cds Material

    9/23

    e"um dayJmo"-tue-'ed-t!u-)ri-sat-su">e"um oolea" Jtrue-)alse>

    4. 5istinguish *et'een the follo'ing6i) Actual and formal arguments

    Actual argumentt!ese are t!e arume"ts- '!ic! arespeci&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 actualarume"ts

    +ormal argumentst!ese are t!e arume"ts used i" t!eu"ctio" declaratio".43 5# co"sider i"t cd Bi"t m- i"t "C

    J;; code )or &"di" t!e cd )or m-"

    Here m a"d " are called t!e )ormal arume"ts

    6ii) 7lo*al and local varia*les

    7lo*al varia*let!e variables- '!ic! are declaredoutside all t!e )u"ctio"s Bi"cludi" t!e mai"C is- called aslobal variable. T!ey are available to all t!e )u"ctio"s.

    435 # i"t a>9oat b>void mai"BCJ

    i"t c>c!ar d>

  • 8/12/2019 i Unit _ Cds Material

    10/23

    I" t!e above e3ample a-b are lobal variables-

    c-d are local variables.

    ,ocal varia*let!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*lesAutomatic varia*let!ey are created '!e" t!e )u"ctio"bloc( is e"tered a"d removed '!e" t!e )u"ctio" istermi"ated. y de)ault- all t!e variables are automaticvariables.

    435 mai"BCJi"t a>static i"t b>

    I" t!e above e3ample i"t a ca" also be co"sidered as autoi"t a '!ic! mea"s all local variables are automatic byde)ault as t!ey are created a"d deleted automatically.

    T!e variable b is a static variable '!ose value 'ill bepersiste"t i" bet'ee" t!e )u"ctio" calls

    8tatic varia*lest!ey are variables- '!ic! are declaredlocal to a )u"ctio". Ho'ever t!ey are available eve"outside t!e )u"ctio" i" '!ic! t!ey are declared.

    *. rie9y 43plai" $o"trol Structures available i" $la"uae

    This deals with the various methods that C can control the flowof logic in aprogram. Control statements can be classified as un-conditional andconditional branch statements and loop or iterative statements. The Branchtype includes:

    . !n-conditional:

    goto

    brea"

  • 8/12/2019 i Unit _ Cds Material

    11/23

    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 morealternatives. This re)uires a deviation from the basic se)uential order ofstatement e*ecution. (uch programs must contain two or more statementsthat mightbee*ecuted+ but have some way to select only one of the listedoptions each time the program is run. This is "nown as conditionale*ecution.

    if statement:

    (tatement or set of statements can be conditionally e*ecuted using ifstatement. Here+ logical condition is tested which+ may either true or false.If the logical test is true ,non ero value the statement that immediatelyfollows if is e*ecuted. If the logical condition is false the control transfers tothe ne*t e*ecutable statement.The general synta* of simple if statement is:

    if ,condition statement_to_execute_if_condition_is_true/

    or

    if ,condition0

    statement /statement #/

    1 1 1 1/}

    Flowchart Segment:

  • 8/12/2019 i Unit _ Cds Material

    12/23

    if $ else statement:

    The if statement is used to e*ecute only one action. If there are twostatements to be e*ecuted alternatively+ then if-else statement is used. Theif-else statement is a two way branching. The general synta* of simple if -elsestatement is:

    if ,condition

    statement_to_execute_if_condition_is_true/else

    statement_to_execute_if_condition_is_false/

    2here+statementmay be a single statement+ a bloc"+ or nothing+ and theelse statement is optional. The conditional statement produces a scalarresult+ i.e.+ an integer+ character or floating point type.

    It is important to remember that an if statement in C can e*ecute only onestatement on each branch ,T or 3. If we desire that multiple statements bee*ecuted on a branch+ we must blockthem inside of a {and }pair to ma"ethem a single compound statement. Thus+ the C code for the flowchart

    segment above would be:

    3lowchart (egment:

    (tatement if

    ,Condition

    T

    3

  • 8/12/2019 i Unit _ Cds Material

    13/23

    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/ / i SS A ;uter loop A0

    for ,O 9 / O > U/ O SS A st level of nesting A0

    temp 9 /for," 9 / " > O/ " SS

    temp 9 temp I/printf ,6dFt+ temp/