Tutorial #6 PseudoCode (reprise)

Slides:



Advertisements
Similar presentations
Algorithms CSC1310 Fall What Is Programming? Programming Programming means writing down a series of instructions that tell a computer what to do.
Advertisements

Introduction to Computing Science and Programming I
Understanding the Three Basic Structures
Unit 7.6 Lesson 2 Goals Identify and use flowchart symbols. Plan a sequence of events and incorporate them into a flowchart. Create a simple flowchart.
Prime Numbers and Prime Factorization
Prime Numbers and Prime Factorization
Prime Numbers and Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several.
place a teabag in a mug; boil some water; pour over teabag; wait until strong enough; remove the teabag; add some milk and stir. what is this?
1 Pseudocode For Program Design. 22 Rules for Pseudocode Write only one statement per line Capitalise initial keyword Indent to show hierarchy and structures.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Flow Charting, Structured English and PseudoCode
Tutorial #7 Flowcharts (reprise) Program Design. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect,
Flow Charting Damian Gordon. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect, and a developer.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Programming Fundamentals (750113) Ch1. Problem Solving
Multiplying and Dividing Decimals
Flow Charts. Thinking Creatively Flow Charts START END Is A==6? No A = 1 Yes Print A A = A + 1.
CS001 Introduction to Programming Day 5 Sujana Jyothi
Algorithms In general algorithms is a name given to a defined set of steps used to complete a task. For example to make a cup of tea you would fill the.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Chapter 2 - Algorithms and Design
Selection: IF Statement Damian Gordon. adsdfsdsdsfsdfs dsdlkmfsdfmsdl kfsdmkfsldfmsk dddfsdsdfsd.
Prime Numbers and Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
A PowerPoint about Algorithm’s. What is an algorithm?  a process or set of rules to be followed in calculations or other problem-solving operations,
Iteration: WHILE Loop Damian Gordon. WHILE Loop Consider the problem of searching for an entry in a phone book with only SELECTION:
End Show Writing a computer program involves performing the following tasks. 1. Understanding the problem 2. Developing an Algorithm for the problem 3.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Top-Down Design Damian Gordon. Top-Down Design Top-Down Design (also known as stepwise design) is breaking down a problem into steps. In Top-down Design.
A PowerPoint about Algorithm’s. What is an algorithm? A step by step process of instruction.
Basic Control Structures
 There are times when you will want blocks to repeat. Instead of duplicating blocks and ending up with a long script that might be confusing, there.
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
Prime Numbers and Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several.
Sequence Damian Gordon. Pseudocode When we write programs, we assume that the computer executes the program starting at the beginning and working its.
Prime Numbers & Prime Factorization. Factors Factors are the numbers you multiply together to get a product. For example, the product 24 has several factors.
COMPSCI 102 Discrete Mathematics for Computer Science.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Algorithms and Pseudocode
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Sequences, Modules and Variables David Millard
Comp1004: Programming in Java II Computational Thinking.
Introduction to Python Damian Gordon e:
HOW TO MAKE A GOOD CUP OF TEA SKW 3061 ENGLISH WORKPLACE MISS GURMINDERJEET KAUR MOHAMAD SHAZRYL B. MOHD SHUKOR B01SPS13F
Control Structures: Conditionals, If/Else and Loops David Millard
Prime Numbers and Prime Factorization
Prime Numbers and Prime Factorization
Introduction to programming
Prime Numbers and Prime Factorization
PROGRAM CONTROL STRUCTURE
Introduction To Flowcharting
Prime Numbers and Prime Factorization
Lecture 07 More Repetition Richard Gesick.
Algorithms Today we will look at: what the word algorithm means
Algorithms Y10 Introduction.
Programming Fundamentals (750113) Ch1. Problem Solving
Java Programming Loops
Understanding the Three Basic Structures
Algorithm Discovery and Design
Python: Algorithms Damian Gordon.
Prime Numbers and Prime Factorization
Algorithm and Ambiguity
Prime Numbers and Prime Factorization
Flowcharts and Pseudo Code
Structured Programming
Prime Numbers and Prime Factorization
Prime Numbers and Prime Factorization
PseudoCode Damian Gordon.
Presentation transcript:

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. Let’s say we want to write a program to calculate interest, a good name for the program would be CalculateInterest.

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.

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.

Pseudocode So we start the program as: PROGRAM CalculateInterest:

Pseudocode So we start the program as: PROGRAM CalculateInterest: And in general it’s: PROGRAM <ProgramName>:

Pseudocode Our program will finish with the following: END.

Pseudocode Our program will finish with the following: END. And in general it’s the same:

Pseudocode So the general structure of all programs is: PROGRAM <ProgramName>: <Do stuff> END.

SEQUENCE

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.

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.

Pseudocode In Pseudo code it looks like this: Statement1; Statement2;

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;

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.

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.

SELECTION

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

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.

Pseudocode So, we could state this as: IF (sugar is required) THEN add sugar; ELSE don’t add sugar; ENDIF;

Pseudocode Or, in general: IF (<CONDITION>) THEN <Statements>; ELSE <Statements>; ENDIF;

Pseudocode Or to check which number is biggest: IF (A > B) THEN Print A + “is bigger”; ELSE Print B + “is bigger”; ENDIF;

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.

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.

ITERATION

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

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.

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.

Pseudocode So, we could state this as: WHILE (Kettle is not full) DO keep filling kettle; ENDWHILE;

Pseudocode Or, in general: WHILE (<CONDITION>) DO <Statements>; ENDWHILE;

Pseudocode Or to print out the numbers 1 to 5: A = 1; WHILE(A > 5) DO Print A; A = A + 1; ENDWHILE;

Pseudocode What is the benefit of using a loop?

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

Pseudocode Consider the problem of searching for an entry in a phone book with only condition: Get first entry If this is the required entry Then write down phone number Else get next entry If this is the correct entry then write done entry else get next entry if this is the correct entry …………….

Pseudocode This could take forever to specify.

Pseudocode This could take forever to specify. There must be a better way to do it.

Pseudocode We may rewrite this as follows: Get first entry; Call this entry N; WHILE N is NOT the required entry DO Get next entry; ENDWHILE;

Pseudocode We may rewrite this as follows: This is why we love loops! Get first entry; Call this entry N; WHILE N is NOT the required entry DO Get next entry; ENDWHILE; This is why we love loops!

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.

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.

EXAMPLES

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

Pseudocode PROGRAM PrintNumber: Read A; Print A; END.

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

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

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

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

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.

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

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.

Pseudocode PROGRAM 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; END.

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

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

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

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

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

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?

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.

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.

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.

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

Pseudocode So remember, So, in general, 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.

Pseudocode PROGRAM 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”; END.