Top Banner
PseudoCode Damian Gordon
64

Introduction to Pseudocode

Oct 21, 2014

Download

Education

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Pseudocode

PseudoCode

Damian Gordon

Page 2: Introduction to Pseudocode

Pseudocode

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

Page 3: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

• So we start the program as:

PROGRAM CalculateInterest:

Page 7: Introduction to Pseudocode

Pseudocode

• So we start the program as:

PROGRAM CalculateInterest:

• And in general it’s:

PROGRAM <ProgramName>:

Page 8: Introduction to Pseudocode

Pseudocode

• Our program will finish with the following:

END.

Page 9: Introduction to Pseudocode

Pseudocode

• Our program will finish with the following:

END.

• And in general it’s the same:

END.

Page 10: Introduction to Pseudocode

Pseudocode

• So the general structure of all programs is:

PROGRAM <ProgramName>:<Do stuff>END.

Page 11: Introduction to Pseudocode

SEQUENCE

Page 12: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

• In Pseudo code it looks like this:

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

Page 15: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

SELECTION

Page 19: Introduction to Pseudocode

Pseudocode

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

Page 20: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

• So, we could state this as:

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

Page 22: Introduction to Pseudocode

Pseudocode

• Or, in general:

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

Page 23: Introduction to Pseudocode

Pseudocode

• Or to check which number is biggest:

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

Page 24: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

ITERATION

Page 27: Introduction to Pseudocode

Pseudocode

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

Page 28: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

• So, we could state this as:

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

Page 31: Introduction to Pseudocode

Pseudocode

• Or, in general:

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

Page 32: Introduction to Pseudocode

Pseudocode

• Or to print out the numbers 1 to 5:

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

Page 33: Introduction to Pseudocode

Pseudocode

• What is the benefit of using a loop?

Page 34: Introduction to Pseudocode

Pseudocode

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

Page 35: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

• This could take forever to specify.

Page 37: Introduction to Pseudocode

Pseudocode

• This could take forever to specify.

• There must be a better way to do it.

Page 38: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

EXAMPLES

Page 43: Introduction to Pseudocode

Pseudocode

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

Page 44: Introduction to Pseudocode

Pseudocode

PROGRAM PrintNumber: Read A; Print A;END.

Page 45: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

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

Page 47: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

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

Page 51: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

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

Page 54: Introduction to Pseudocode

Pseudocode

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

Page 55: Introduction to Pseudocode

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: Introduction to Pseudocode

Pseudocode

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

Page 57: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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: Introduction to Pseudocode

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.