64
Tutorial #6 PseudoCode (reprise) Program Design

Tutorial #6 PseudoCode (reprise)

  • Upload
    maili

  • View
    40

  • Download
    5

Embed Size (px)

DESCRIPTION

Tutorial #6 PseudoCode (reprise). Program Design. Pseudocode. The first thing we do when designing a program is to decide on a name for the program. Pseudocode. The first thing we do when designing a program is to decide on a name for the program. - PowerPoint PPT Presentation

Citation preview

Page 1: Tutorial #6 PseudoCode  (reprise)

Tutorial #6PseudoCode (reprise)

Program Design

Page 2: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• The first thing we do when designing a program is to decide on a name for the program.

Page 3: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• The first thing we do when designing a program is to decide on a name for the program.

• Let’s say we want to write a program to calculate interest, a good name for the program would be CalculateInterest.

Page 4: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• The first thing we do when designing a program is to decide on a name for the program.

• Let’s say we want to write a program to calculate interest, a good name for the program would be CalculateInterest.

• Note the use of CamelCase.

Page 5: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• The first thing we do when designing a program is to decide on a name for the program.

• Let’s say we want to write a program to calculate interest, a good name for the program would be CalculateInterest.

• Note the use of CamelCase.

Page 6: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So we start the program as:

PROGRAM CalculateInterest:

Page 7: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So we start the program as:

PROGRAM CalculateInterest:

• And in general it’s:

PROGRAM <ProgramName>:

Page 8: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Our program will finish with the following:

END.

Page 9: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Our program will finish with the following:

END.

• And in general it’s the same:

END.

Page 10: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So the general structure of all programs is:

PROGRAM <ProgramName>:<Do stuff>END.

Page 11: Tutorial #6 PseudoCode  (reprise)

SEQUENCE

Page 12: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• When we write programs, we assume that the computer executes the program starting at the beginning and working its way to the end.

• This is a basic assumption of all algorithm design.

Page 13: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• When we write programs, we assume that the computer executes the program starting at the beginning and working its way to the end.

• This is a basic assumption of all algorithm design.

• We call this SEQUENCE.

Page 14: Tutorial #6 PseudoCode  (reprise)

Pseudocode• In Pseudo code it looks like this:

Statement1;Statement2;Statement3;Statement4;Statement5;Statement6;Statement7;Statement8;

Page 15: Tutorial #6 PseudoCode  (reprise)

Pseudocode• For example, for making a cup of tea:

Organise everything together;Plug in kettle;Put teabag in cup;Put water into kettle;Wait for kettle to boil;Add water to cup;Remove teabag with spoon/fork;Add milk and/or sugar;Serve;

Page 16: Tutorial #6 PseudoCode  (reprise)

Pseudocode• Or as a program:

PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk and/or sugar; Serve;END.

Page 17: Tutorial #6 PseudoCode  (reprise)

Pseudocode• Or as a program:

PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk and/or sugar; Serve;END.

Page 18: Tutorial #6 PseudoCode  (reprise)

SELECTION

Page 19: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• What if we want to make a choice, for example, do we want to add sugar or not to the tea?

Page 20: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• What if we want to make a choice, for example, do we want to add sugar or not to the tea?

• We call this SELECTION.

Page 21: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So, we could state this as:

IF (sugar is required) THEN add sugar; ELSE don’t add sugar;ENDIF;

Page 22: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Or, in general:

IF (<CONDITION>) THEN <Statements>; ELSE <Statements>;ENDIF;

Page 23: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Or to check which number is biggest:

IF (A > B) THEN Print A + “is bigger”; ELSE Print B + “is bigger”;ENDIF;

Page 24: Tutorial #6 PseudoCode  (reprise)

Pseudocode• Adding a selection statement in the program:

PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END.

Page 25: Tutorial #6 PseudoCode  (reprise)

Pseudocode• Adding a selection statement in the program:

PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; Put water into kettle; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END.

Page 26: Tutorial #6 PseudoCode  (reprise)

ITERATION

Page 27: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• What if we need to tell the computer to keep doing something until some condition occurs?

Page 28: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• What if we need to tell the computer to keep doing something until some condition occurs?

• Let’s say we wish to indicate that the you need to keep filling the kettle with water until it is full.

Page 29: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• What if we need to tell the computer to keep doing something until some condition occurs?

• Let’s say we wish to indicate that the you need to keep filling the kettle with water until it is full.

• We need a loop, or ITERATION.

Page 30: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So, we could state this as:

WHILE (Kettle is not full) DO keep filling kettle;ENDWHILE;

Page 31: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Or, in general:

WHILE (<CONDITION>) DO <Statements>;ENDWHILE;

Page 32: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Or to print out the numbers 1 to 5:

A = 1;WHILE(A > 5) DO Print A; A = A + 1;ENDWHILE;

Page 33: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• What is the benefit of using a loop?

Page 34: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Consider the problem of searching for an entry in a phone book with only condition:

Page 35: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• Consider the problem of searching for an entry in a phone book with only condition:

Get first entryIf this is the required entry

Then write down phone numberElse get next entryIf this is the correct entry

then write done entryelse get next entry

if this is the correct entry…………….

Page 36: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• This could take forever to specify.

Page 37: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• This could take forever to specify.

• There must be a better way to do it.

Page 38: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• We may rewrite this as follows:

Get first entry;Call this entry N;WHILE N is NOT the required entryDO Get next entry;

Call this entry N;ENDWHILE;

Page 39: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• We may rewrite this as follows:

Get first entry;Call this entry N;WHILE N is NOT the required entryDO Get next entry;

Call this entry N;ENDWHILE;

• This is why we love loops!

Page 40: Tutorial #6 PseudoCode  (reprise)

Pseudocode• Or as a program:

PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; WHILE (Kettle is not full) DO keep filling kettle; ENDWHILE; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END.

Page 41: Tutorial #6 PseudoCode  (reprise)

Pseudocode• Or as a program:

PROGRAM MakeACupOfTea: Organise everything together; Plug in kettle; Put teabag in cup; WHILE (Kettle is not full) DO keep filling kettle; ENDWHILE; Wait for kettle to boil; Add water to cup; Remove teabag with spoon/fork; Add milk; IF (sugar is required) THEN add sugar; ELSE do nothing; ENDIF; Serve;END.

Page 42: Tutorial #6 PseudoCode  (reprise)

EXAMPLES

Page 43: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and print it out.

Page 44: Tutorial #6 PseudoCode  (reprise)

Pseudocode

PROGRAM PrintNumber: Read A; Print A;END.

Page 45: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and print it out double the number.

Page 46: Tutorial #6 PseudoCode  (reprise)

Pseudocode

PROGRAM PrintDoubleNumber: Read A; B = A*2; Print B;END.

Page 47: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number, check if it is odd or even.

Page 48: Tutorial #6 PseudoCode  (reprise)

Pseudocode

PROGRAM IsOddOrEven: Read A; IF (A/2 gives a remainder) THEN Print “It’s Odd”; ELSE Print “It’s Even”; ENDIF;END.

Page 49: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm to print out the bigger of two numbers:– Read in two numbers, call them A and B. Is A is bigger

than B, print out A, otherwise print out B.

Page 50: Tutorial #6 PseudoCode  (reprise)

Pseudocode

PROGRAM PrintBiggerOfTwo: Read A; Read B; IF (A>B) THEN Print A; ELSE Print B; ENDIF;END.

Page 51: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm to print out the bigger of three numbers:– Read in three numbers, call them A, B and C.

• If A is bigger than B, then if A is bigger than C, print out A, otherwise print out C.

• If B is bigger than A, then if B is bigger than C, print out B, otherwise print out C.

Page 52: Tutorial #6 PseudoCode  (reprise)

PseudocodePROGRAM BiggerOfThree: Read A; Read B; Read C; IF (A>B) THEN IF (A>C) THEN Print A; ELSE Print C; END IF; ELSE IF (B>C) THEN Print B; ELSE Print C; END IF; END IF;END.

Page 53: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Print out the numbers from 1 to 5

Page 54: Tutorial #6 PseudoCode  (reprise)

Pseudocode

PROGRAM Print1to5: A = 1; WHILE (A != 6) DO Print A; A = A + 1; ENDWHILE;END.

Page 55: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Add up the numbers 1 to 5 and print out the result

Page 56: Tutorial #6 PseudoCode  (reprise)

PseudocodePROGRAM PrintSum1to5: Total = 0; A = 1; WHILE (A != 6) DO Total = Total + A; A = A + 1; ENDWHILE; Print Total;END.

Page 57: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and check if it’s a prime number.

Page 58: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and check if it’s a prime number.– What’s a prime number?

Page 59: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and check if it’s a prime number.– What’s a prime number?– A number that’s only divisible by itself and 1, e.g. 7.

Page 60: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and check if it’s a prime number.– What’s a prime number?– A number that’s only divisible by itself and 1, e.g. 7. – Or to put it another way, every number other than itself and 1

gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder then 7 is prime.

Page 61: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So let’s say we want to express the following algorithm:– Read in a number and check if it’s a prime number.– What’s a prime number?– A number that’s only divisible by itself and 1, e.g. 7. – Or to put it another way, every number other than itself and 1

gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder then 7 is prime.

– So all we need to do is divide 7 by all numbers less than it but greater than one, and if any of them have no remainder, we know it’s not prime.

Page 62: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So, • If the number is 7, as long as 6, 5, 4, 3, and

2 give a remainder, 7 is prime.• If the number is 9, we know that 8, 7, 6, 5,

and 4, all give remainders, but 3 does not give a remainder, it goes evenly into 9 so we can say 9 is not prime

Page 63: Tutorial #6 PseudoCode  (reprise)

Pseudocode

• So remember, – if the number is 7, as long as 6, 5, 4, 3, and 2

give a remainder, 7 is prime.• So, in general, – if the number is A, as long as A-1, A-2, A-3, A-

4, ... 2 give a remainder, A is prime.

Page 64: Tutorial #6 PseudoCode  (reprise)

PseudocodePROGRAM Prime: Read A; B = A - 1; IsPrime=True; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime= False; ENDIF; B = B – 1; ENDWHILE; IF (IsPrime == true) THEN Print “Prime”; ELSE Print “Not Prime”; ENDIF;END.