Week 2b. algorithm formal representation

24
Nur Afny C. Andryani SSi (Applied Math) MSc (AI-Soft Computing) [email protected] [email protected] www.istb.ac.id Week 3. Algorithm Formal Representation & Control Structures Algorithm & Problem Solving (CS110)

description

 

Transcript of Week 2b. algorithm formal representation

Page 1: Week 2b. algorithm formal representation

Nur Afny C. Andryani SSi (Applied Math) MSc (AI-Soft Computing)

[email protected] [email protected]

www.istb.ac.id

Week 3. Algorithm Formal Representation &

Control Structures

Algorithm & Problem Solving (CS110)

Page 2: Week 2b. algorithm formal representation

Formal Representation

• It is common among programmer, engineer, and computer scientist to represent an

algorithm in formal way. Thus, it will be easily understood.

• Formal representation of algorithm is aimed to make the algorithm can be code by various

language programming easily.

• There are three kind common algorithm representation:

– Flow Chart

– Pseudo Code

In general the three above methods are the way to express the problem solving in formal

way.

Page 3: Week 2b. algorithm formal representation

Flow chart • Diagram that represent the flow of algorithm

• The Diagram consist of some symbols that represent general flow of programming

Symbol Representations

Terminal: Symbol for “Start “and “End” of algorithm

Input and Output operation

Process : indicate process such as mathematical computation, variable assignment

Flow line: connect one symbol to other symbol

Diamond: represent decision structure

Connector: joining two kinds of algorithm (flow)

Page 4: Week 2b. algorithm formal representation

example • You are on of HR staff and need to make procedure

to determine the amount of salary paid for the

employee per month

– The formula: number of working day times the

amount paid per day

– The input: how many the employee’s working

day & how much the amount paid per days

start

Input wd=working days

Input p: amount of paid per day

Calculate salary/month salary = wd x p

Write “salary” Display “salary”

end

Page 5: Week 2b. algorithm formal representation

Pseudo code

• Formal representation of algorithm using structured English

BEGIN

INPUT (READ) the number of working days a month

INPUT (READ) the amount paid per day

Calculate the salary per month

“salary” = number of working days a month x amount paid per day

DISPLAY (“salary”)

END

Instruction Explanation

GET Get the information from key board

READ Information from file

WRITE Send information to a file

DISPLAY (PRINT) Send information to screen

INITIALIZE (SET) Set initial value

Page 6: Week 2b. algorithm formal representation

Rules of Pseudo code

• Write only one statement per line

• Capitalized initial key word : READ, GET, SET, WHILE, END, IF, IF ELSE, ENDIF, ect

• Indent to show hierarchy

• End multiline structures

See how the IF/ELSE/ENDIF is constructed above. The ENDIF (or END

whatever) always is in line with the IF (or whatever starts the structure).

• Avoid using special features of language

Page 7: Week 2b. algorithm formal representation

Basic Structure of computer programming

There are three structures on computer programming:

• Sequence Structure

– Structure programming that perform executing several statements (procedure/ process)

in sequence

• Decision Structure

– Structure programming that perform executing a statement/ procedure/ process when

satisfy a certain condition

• Repetition Structure

– Structure programming that perform executing the same (sequence) procedures in

several time.

Page 8: Week 2b. algorithm formal representation

Sequence Structured BEGIN

READ

Statement 1

Statement 2

…..

Statement (n)

DISPLAY (output)

END

start

input

Display Output

end

Statement (1)

Statement (2)

Statement (n)

Page 9: Week 2b. algorithm formal representation

Example

• One of the jobs that Joe Roberts has been given at work is to order special paper for a report for a board meeting. The paper comes in reams of 500 sheets. He always make five more copies than the number of people that will be there. Joe wants to know how many reams of paper he needs for a meeting. He can order only whole, not partial, reams. Assume the required number of pages will not equal an exact number of reams. Test your solution with the following data

– The report is 140 pages long.

– There will be 25 people at the meeting.

BEGIN CalculateNumOfReams INPUT The report pages number, The number of people attend OUTPUT The number of reams paper ordered READ PagesReport READ NumOfPeople Reams = (PagesReport*(NumOfPeople+5))/500 Integer (Reams) DISPLAY “Reams” END

Page 10: Week 2b. algorithm formal representation

Decision Structured BEGIN

INPUT

IF condition is satisfied

THEN

Statement (1)

ELSE

statement (2)

DISPLAY (output)

ENDIF

END

BEGIN

INPUT

IF condition (1) is satisfied

THEN

Statement (1)

ELSE IF condition (2)

statement (2)

……..

ELSE IF condition (n)

Statement (n)

ELSE

Statement (n+1)

DISPLAY (output)

ENDIF

END

start

input

Display Output

end

Statement

condition

yes

No

Page 11: Week 2b. algorithm formal representation

Example • Calculate the largest number in a set of three numbers

Algorithm FindLargestNum Purpose this algorithm finds the largest number in a set of three numbers INPUT three numbers OUTPUT The largest number BEGIN FindTheLargestOfThree READ num1 READ num2 READ num3 SET LargetsNum to num1 IF LargestNum <num2 Then SET LargestNum to num2 ELSE LargestNum ENDIF IF LargestNum < num3 SET LargestNum to num3 ELSE LargestNum ENDIF DISPLAY LargestNum END FindTheLargestOfThree

Page 12: Week 2b. algorithm formal representation

Exercises

• Write pseudo code to take user name and age as input. And print name and whether the

user is eligible for a job or not with the following criteria.

If user is older than 22 then he is eligible for the job otherwise display not eligible.

Page 13: Week 2b. algorithm formal representation

Repetition structure

• This structure is also well known as problem solving with loops

• There are three types of repetition structures:

– FOR loop

Execute a set of instructions as amount as the given number of repetition

– WHILE/ WHILE-END Loop

Execute a set of instructions while a condition is TRUE and stop repeating when condition

arises that is FALSE

– REPEAT/UNTIL Loop

Execute a set of instructions while condition is FALSE until the condition is TRUE

Page 14: Week 2b. algorithm formal representation

Learn these!! Before we go to the structures

Incrementing : The task of incrementing, or counting, is done by adding a constant to the

value of variable

Ex: COUNTER = COUNTER + 1

Var = Var +C It means that the value of Var is always be incrementing by C

This instruction enable programmer to count the number of items, people, temperatures and so

on which is a part of problem solving

Accumulating : The accumulation structure also quite often perform in programming. This

instruction summing a group of numbers. The process is quite similar with incrementing,

except a variable instead of a constant is added to another variable

Ex: SUM = SUM + VARIABLE

TOTALSALES = TOTALSALES + SALES

PRODUCT=1

PRODUCT = PRODUCT * NUMBER

Page 15: Week 2b. algorithm formal representation

FOR LOOP

• It can be classified as iteration statement

• Execute the Instruction(s) as given number of iteration

FOR <iteration bound>

STATEMENT

ENDFOR

start

i=1

i<=n?

Statement

i=i+1

end

Page 16: Week 2b. algorithm formal representation

While-loop

Execute the statements as long as the condition is TRUE and out of the loop when the

condition is FALSE

WHILE <condition(s)>

INSTRUCTION

INSTRUCTION

...

INSTRUCTION

END-WHILE

start

Sequence of instructions

WHILE <CONDITION(S)>

END

F

T

Page 17: Week 2b. algorithm formal representation

REPEAT UNTIL

Execute the statements as long as the condition is FALSE and out of the loop when

the condition is TRUE

REPEAT

INSTRUCTION

INSTRUCTION

...

INSTRUCTION

UNTIL <condition(s)>

start

Sequence of instructions

IF <CONDITION(S)>

END

F

T

Page 18: Week 2b. algorithm formal representation

Study Case

• Create a program to display input number which are form 1 to 10 using

– For loop

– While loop

– Repeat until

Page 19: Week 2b. algorithm formal representation

Solution

• For Loop

INPUT p is 10 integer

form 1 to 10

BEGIN

FOR p is 1 to 10

DISPLAY (p)

ENDFOR

END

• WHILE Loop

INPUT p is integer number

BEGIN

WHILE (p>=1) && (p<=10)

DISPLAY (p)

ENDWHILE

END

• REPEAT Loop

INPUT p is integer number

BEGIN

DISPLAY (p)

UNTIL (p<1)&&(p > 10)

END

Page 20: Week 2b. algorithm formal representation

Study Case • Create a program to calculate factorial

INPUT p as integer positive OUTPUT p factorial BEGIN INITIALIZE FACT=1 INITIALIZE n=0 n=n+1 FACT=FACT * (p-n) UNTIL < p=n+1> END DISPLAY (FACT) END

INPUT p as integer positive OUTPUT p factorial BEGIN INITIALIZE FACT=1 INITIALIZE n=0 WHILE < p>=n+1 > n=n+1 FACT=FACT * (p-n) END-WHILE DISPLAY (FACT) END

INPUT p as integer positive OUTPUT p factorial BEGIN INITIALIZE FACT=1 FOR i from 1 to p FACT=FACT * i END-FOR DISPLAY (FACT) END

Page 21: Week 2b. algorithm formal representation

Have fun for a while ;)

Page 22: Week 2b. algorithm formal representation

Jom dijawab

Page 23: Week 2b. algorithm formal representation

Exercises • The last Stop Boutique is having a five-day sale. Each day, starting on Monday, the price will drop 10% of the previous day’s

price. For example, if the original price of a product is $20.00, the sale price on Monday would be $18.00. On Tuesday the

sale price would be $ 16.20 and so forth. Design an algorithm to calculate the price of an item on n-day during the day-

sale.

Algorithm CalculatePrice Purpose: Calculate Price for certain item on day-n Input: Original Price of an Item, Number of day Output: Price BEGIN CalculatePrice INPUT n NUMBER OF DAY INPUT OriPrice ORIGINAL PRICE IF n >=1 && n<=5 Price=((1-0.1)^n)) * OriPrice ELSEIF n>5 Price=OriPrice ELSE Price=error END DISPLAY (“Price”) END

Page 24: Week 2b. algorithm formal representation

Exercises

• Develop a solution that will calculate the average temperature, given a set of temperatures.

The number of temperatures may differ from time to time. Test the solution with the

following 5 temperatures: 78, 85, 87, 75, 86