MCS 1
Tariq Ali
What is pseudocode?
 List of steps written in English
 Like the instructions for a recipe
 Must be in the right sequence
   Imagine saying “bake the cake” and then “mix it up”
Sample Pseudocode
 Task: add two numbers
 Pseudocode:
    Start
    Get two numbers
       Get first number
       Get second number
   Add them
   Print the answer
   End
Pseudocode
  English language constructs modeled to look like
   statements available in most programming
   languages


  Steps presented in a structured manner
   (numbered, indented, and so on)


  No fixed syntax for most operations is required
Invitation to Computer
Science, Java Version, Third
Edition                        4
Pseudocode (continued)
  Less ambiguous and more readable than natural
   language

  Emphasis is on process, not notation

  Well-understood forms allow logical reasoning
   about algorithm behavior

  Can be easily translated into a programming
   language
Invitation to Computer Science,
Java Version, Third Edition     5
Sample Pseudocode
 Task: add two numbers
 Pseudocode:
    Start
    Get two numbers
       Get first number
       Get second number
   Add them
   Print the answer
   End
What does a flowchart look like?
  The pseudocode from the previous slide would look
  like this as a flowchart:


           Start
                                 Print answer
        Get 2 numbers
                                  End
          Add them
What are those funny symbols?

 START/END

 INPUT/OUTPUT

 PROCESS

 DECISION
What are those funny symbols?
  START/END
  Used at the beginning
  and end of each
  flowchart.
What are those funny symbols?



 INPUT/OUTPUT
 Shows when
 information/data comes
 into a program or is
 printed out.
What are those funny symbols?
What are those funny symbols?
  PROCESS
  Used to show
  calculations, storing of
  data in variables, and
  other “processes” that
  take place within a
  program.
What are those funny symbols?
What are those funny symbols?
  DECISION
  Used to show that the
  program must decide             Y
  whether something        X>7?
  (usually a comparison
                           N
  between numbers) is
  true or false. YES and
  NO (or T/F) branches
  are usually shown.
Another Sample:
Calculating Age
 Pseudocode:
    Start
    Get year DOB
    Calculate age = (sysdate-DOB)
    Print age
    If age > 50 print OLD
    End
Another Sample:
                                       Start
 Calculating Age
                                      Get yr


                                     Calc age

 Flowchart
    Start                           Print age

    Get year born
    Calculate age           OLD   Y Age>50?
    Print age
                                               N
    If age > 50 print OLD             End
    End
Self-Check
 Look at the flowchart section below. If the variable X is
  5, what will print (K or 1st)?



  Print “K”      N                 Y    Print “1st”
                         X > 5?
Self-Check
 Look at the flowchart section below. If the variable X is
  5, what will print (K or 1st)?



   Print “K”     N                   Y    Print “1st”
                         X > 5?



K will be printed. The answer to the question “Is X greater than 5
is NO, since X is equal to (not greater than) 5.
Self-Check
  Choose the correct
  flowchart symbol for each
  of these statements.
  AGE>65?
  Calc. Tax
  START
  Print NAME
Self-Check
  Choose the correct
  flowchart symbol for each
  of these statements.
  AGE>65?
  Calc. Tax
  START
  Print NAME
Challenge
 Try to write pseudocode and create a flowchart for a
  program that calculates the average of three grades
  and prints the average.
 The word GOOD should be printed only if the average
  is more than 80.
Challenge
 Possible pseudocode
    Start
    Get three grades
    Average them (add all of them / number of grads taken)
    Print Average
    Average>80?
       If Yes, print GOOD
   End
START

Challenge                                Get 3 grades
  Possible flowchart
    Start                               Calc avg
    Get three grades
    Average them
                                         Print avg
    Print Average
    Average>80?
                                     Y
        If Yes, print GOOD   GOOD       Avg>80?
    End
                                              N

                                           END
Challenge
 Try to write pseudocode and create a flowchart for a
  program that calculates the average of three grades
  and prints the average.
 The word GOOD should be printed only if the average
  is more than 80.
Challenge
 Possible pseudocode
    Start
    Get three grades
    Average them
    Print Average
    Average>80?
       If Yes, print GOOD
   End
START

Challenge                                Get 3 grades
  Possible flowchart
    Start                               Calc avg
    Get three grades
    Average them
                                         Print avg
    Print Average
    Average>80?
                                     Y
        If Yes, print GOOD   GOOD       Avg>80?
    End
                                              N

                                           END
 Algorithm for Computing Average Miles per
  Gallon
 Write a program to do the task: Print a list of the numbers
  from 4 to 9, next to each number, print the square of the
  number.
Programming fundamentals lecture 3
Programming fundamentals lecture 3

Programming fundamentals lecture 3

  • 1.
  • 2.
    What is pseudocode? List of steps written in English  Like the instructions for a recipe  Must be in the right sequence  Imagine saying “bake the cake” and then “mix it up”
  • 3.
    Sample Pseudocode  Task:add two numbers  Pseudocode:  Start  Get two numbers  Get first number  Get second number  Add them  Print the answer  End
  • 4.
    Pseudocode  Englishlanguage constructs modeled to look like statements available in most programming languages  Steps presented in a structured manner (numbered, indented, and so on)  No fixed syntax for most operations is required Invitation to Computer Science, Java Version, Third Edition 4
  • 5.
    Pseudocode (continued) Less ambiguous and more readable than natural language  Emphasis is on process, not notation  Well-understood forms allow logical reasoning about algorithm behavior  Can be easily translated into a programming language Invitation to Computer Science, Java Version, Third Edition 5
  • 6.
    Sample Pseudocode  Task:add two numbers  Pseudocode:  Start  Get two numbers  Get first number  Get second number  Add them  Print the answer  End
  • 7.
    What does aflowchart look like?  The pseudocode from the previous slide would look like this as a flowchart: Start Print answer Get 2 numbers End Add them
  • 8.
    What are thosefunny symbols?  START/END  INPUT/OUTPUT  PROCESS  DECISION
  • 9.
    What are thosefunny symbols?  START/END  Used at the beginning and end of each flowchart.
  • 10.
    What are thosefunny symbols?  INPUT/OUTPUT  Shows when information/data comes into a program or is printed out.
  • 11.
    What are thosefunny symbols? What are those funny symbols?  PROCESS  Used to show calculations, storing of data in variables, and other “processes” that take place within a program.
  • 12.
    What are thosefunny symbols? What are those funny symbols?  DECISION  Used to show that the program must decide Y whether something X>7? (usually a comparison N between numbers) is true or false. YES and NO (or T/F) branches are usually shown.
  • 13.
    Another Sample: Calculating Age Pseudocode:  Start  Get year DOB  Calculate age = (sysdate-DOB)  Print age  If age > 50 print OLD  End
  • 14.
    Another Sample: Start Calculating Age Get yr Calc age  Flowchart  Start Print age  Get year born  Calculate age OLD Y Age>50?  Print age N  If age > 50 print OLD End  End
  • 15.
    Self-Check  Look atthe flowchart section below. If the variable X is 5, what will print (K or 1st)? Print “K” N Y Print “1st” X > 5?
  • 16.
    Self-Check  Look atthe flowchart section below. If the variable X is 5, what will print (K or 1st)? Print “K” N Y Print “1st” X > 5? K will be printed. The answer to the question “Is X greater than 5 is NO, since X is equal to (not greater than) 5.
  • 17.
    Self-Check  Choosethe correct flowchart symbol for each of these statements.  AGE>65?  Calc. Tax  START  Print NAME
  • 18.
    Self-Check  Choosethe correct flowchart symbol for each of these statements.  AGE>65?  Calc. Tax  START  Print NAME
  • 19.
    Challenge  Try towrite pseudocode and create a flowchart for a program that calculates the average of three grades and prints the average.  The word GOOD should be printed only if the average is more than 80.
  • 20.
    Challenge  Possible pseudocode  Start  Get three grades  Average them (add all of them / number of grads taken)  Print Average  Average>80?  If Yes, print GOOD  End
  • 21.
    START Challenge Get 3 grades  Possible flowchart  Start Calc avg  Get three grades  Average them Print avg  Print Average  Average>80? Y  If Yes, print GOOD GOOD Avg>80?  End N END
  • 22.
    Challenge  Try towrite pseudocode and create a flowchart for a program that calculates the average of three grades and prints the average.  The word GOOD should be printed only if the average is more than 80.
  • 23.
    Challenge  Possible pseudocode  Start  Get three grades  Average them  Print Average  Average>80?  If Yes, print GOOD  End
  • 24.
    START Challenge Get 3 grades  Possible flowchart  Start Calc avg  Get three grades  Average them Print avg  Print Average  Average>80? Y  If Yes, print GOOD GOOD Avg>80?  End N END
  • 26.
     Algorithm forComputing Average Miles per Gallon
  • 28.
     Write aprogram to do the task: Print a list of the numbers from 4 to 9, next to each number, print the square of the number.