Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

Lecture 07

VIEWS: 1 PAGES: 15

									Programming Fundamentals
        Lecture 7
               Flow Charts
• Flowchart - a graphical way of writing
  algorithms
  – Rectangle is used for calculations
  – Parallelogram is used for input and output
  – Circle is used as connector
  – Diamond is used as decision
  – Symbols are connected by arrows to
    represent the order of the operations
Input/Output



           Processing




                        Decision




                                   Connector
              Calculations
• Calculations are shown in rectangles
• Example
  – Total = Cost + Tax
  – Num = Num + 1         Total = Cost + Tax
                           Total = Cost + Tax




                              Num = Num + 1
                              Num = Num + 1
            Input / Output
• Data input and output are shown in
  parallelograms
  – Read num
  – Display num


      READ Num       DISPLAY Num
                     DISPLAY Num
      READ Num
                Decisions
• Decisions are shown within
  diamonds and specify a
  condition to be tested                           False
                                                    False
                                  Gross > 50000
• A decision is composed of :     Gross > 50000


  – A condition                   True
                                                 Rate = 0.28
                                                  Rate = 0.28
                                   True
  – An operation to be done if
                                  Rate = 0.31
                                   Rate = 0.31
    condition is TRUE
  – Possibly an operation to be
    done if condition is FALSE
              Start / End
• Every flowchart             Start

  must have one
  start symbol and          Input num
  one end symbol
                        Square = num x num


                             Display
                              num


                              Stop
      Example 1 (Sequence)
• Example
  – GET Age1
  – GET Age2
  – CALCULATE Allowance for Child1
    • Allow1 = Age1 x Rate
  – CALCULATE Allowance for Child2
    • Allow2 = Age2 x Rate
  – DISPLAY Allow1
  – DISPLAY Allow2
Start                  Example (Cont…)
           Get Age1



        Get Age2




                   Allow1 = Age1 x Rate



                   Allow2 = Age2 x Rate


                                              Display
                                               Age1

                                          Display
                                           Age2


                                                        Stop
       Example 2 (Selection)
• PROMPT for Age
  GET Age
  IF Age less than 10
   CALCULATE Allow = Age x YoungRate
 ELSE
  CALCULATE Allow = Age x OlderRate
 ENDIF
 DISPLAY Allow
            start
             start


         prompt Age
         prompt Age


          Get Age
          Get Age


 TRUE
 TRUE                      FALSE
                            FALSE
          Age < 10
          Age < 10


Allow = Age x        Allow = Age x
                     Allow = Age x
Allow = Age x
 YoungRate             OlderRate
                        OlderRate
  YoungRate




          Display Allow
           Display Allow


                stop
                 stop
        Example 3 (Iteration)
• Set KidsPaid to 0
• Set Total to 0
• WHILE KidsPaid < 3
   – PROMPT for Age
   – GET Age
   – CALCULATE Allowance = Age x Rate
   – ADD Allowance to Total
   – INCREMENT KidsPaid
• ENDWHILE
• DISPLAY Total
              start
               start

           KidsPaid = 0
            KidsPaid = 0


              Total = 0
               Total = 0




        KidsPaid < 3
         KidsPaid < 3
TRUE
 TRUE                          FALSE
                                FALSE

 Prompt Age
  Prompt Age


  Read Age
   Read Age
                       Display Total
                        Display Total

 Calc Allowance
  Calc Allowance
                            stop
                             stop
 Add Allowance
  Add Allowance
    to Total
     to Total


 Increment KidsPaid
  Increment KidsPaid
               Exercise 1
• Draw a flowchart to find the sum of first 50
  natural numbers
               Exercise 2
• Draw a flowchart to calculate factorial of
  an integer

								
To top