21
Algorithm Development Unit 2

Unit 2_Algorithm Dev Elopement

Embed Size (px)

Citation preview

Page 1: Unit 2_Algorithm Dev Elopement

Algorithm Development

Unit 2

Page 2: Unit 2_Algorithm Dev Elopement

Definition of an Algorithm• An algorithm is a sequence of precise instructions for solving a problem in a

finite amount of time.

Properties of an Algorithm:

• It must be precise and unambiguous executable steps

• It must give the correct solution in all cases

• It must eventually end.

Development of an Algorithm:

• During development of an algorithm, the language gradually progresses from English towards a programming language notation.

• An intermediate notation called pseudocode is commonly used to express algorithms.

Representation (Tools) of an Algorithm:

• A single algorithm can be represented in many ways: Formulas: F = (9/5)C + 32 Words: Multiply the Celsius by 9/5 and add 32. Flow Charts. Pseudo-code.

2Algorithm Development

Page 3: Unit 2_Algorithm Dev Elopement

Expressing Algorithms

• English description

• Pseudo-code

• High-level programming language

More

preciseMore easily expressed

3Algorithm Development

Page 4: Unit 2_Algorithm Dev Elopement

Pseudocode

• Pseudocode is like a programming language but its rules are less stringent.

• Written as a combination of English and programming constructs Based on selection (if, switch) and iteration (while, repeat) constructs in

high-level programming languages

• Design using these high level primitives Independent of actual programming language

• Pseudocode Example to find the sum of 2 numbers: Sequential Algorithm

Start

Declare num1, num2, Sum

Read num1, num2

Sum = num1 + num2

Write Sum

Stop.

Algorithm Development 4

Page 5: Unit 2_Algorithm Dev Elopement

How to write a Pseudocode • An algorithm can be written in pseudocode using six (6) basic computer

operations:

• A computer can receive information Typical pseudocode instructions to receive information are:

Read name

Get name

Read number1, number2

• A computer can output (print) information. Typical pseudocode instructions are:

Print name

Write "The average is ", avg

• A computer can perform arithmetic operation. Typical pseudo-code instructions:

Add number to total, or

Total = Total + Number

Avg = sum/total

5Algorithm Development

Page 6: Unit 2_Algorithm Dev Elopement

How to write a Pseudocode (Cont’d)• A computer can assign a value to a piece of data.

e.g. to assign/give data an initial value:

Initialize total to zero

Set count to 0

To assign a computed value:

Total = Price + Tax

• A computer can compare two (2) pieces of information and select one of two actions. Typical pseudo-code e.g.

IF number < 0 then

add 1 to neg number

ELSE

add one to positive number

end-if

6Algorithm Development

Page 7: Unit 2_Algorithm Dev Elopement

How to write a Pseudocode (Cont’d)• A computer can repeat a group of actions.

Typical pseudo-code e.g.

REPEAT until total = 50

read number

write number

add 1 to total

end-repeat

OR

WHILE total < = 50 do

read number

write number

end-while

7Algorithm Development

Page 8: Unit 2_Algorithm Dev Elopement

How to write a Pseudocode (Cont’d)Let’s review the plan and write out algorithm for the average problem in the specified

format:

Algorithm Average • This algorithm reads a list of numbers and computes their average.

Let: SUM be the total of the numbers read

COUNTER be the number of items in the list

AVG be the average of all the numbers

Set SUM to 0,

Set COUNTER to 0. (i.e. initialize variables)

While there is data do:

Read number

COUNTER = COUNTER + 1

(i.e. add 1 to COUNTER, storing result in COUNTER)

SUM = SUM + number

(i.e. add number to SUM, storing result in SUM)

end-while

if COUNTER = 0 then

AVG = 0

else

AVG = SUM/ COUNTER

end-if

Stop. 8Algorithm Development

Page 9: Unit 2_Algorithm Dev Elopement

Flow ChartsFlow chart

• Possibly the simplest and easiest method to understand the steps in an algorithm, is by using the flowchart method.

• This algorithm is composed of block symbols to represent each step in the

• solution process as well as the directed paths of each step.

• The most common block symbols are:

9Algorithm Development

Symbol Representation Symbol Representation

Start/Stop Decision

Process Connector

Input/Input Flow Direction

Preparation Predefined Process

Page 10: Unit 2_Algorithm Dev Elopement

Pseudocode Flow Chart

Start

Declare num1, num2, Sum

Read num1, num2

Sum = num1 + num2

Write Sum

Stop.

Sum=num1 + num2

Compare and Contrast a Pseudocode and Flowchart

Algorithm Development 10

Stop

Print Sum

Read num1, num2

Start

Page 11: Unit 2_Algorithm Dev Elopement

Compare and Contrast a Pseudocode and Flowchart (Cont’d)

Algorithm Development 11

Page 12: Unit 2_Algorithm Dev Elopement

Compare and Contrast a Pseudocode and Flowchart (Cont’d)

Algorithm Development 12

Page 13: Unit 2_Algorithm Dev Elopement

Compare and Contrast a Pseudocode and Flowchart (Cont’d)

Algorithm Development 13

Page 14: Unit 2_Algorithm Dev Elopement

Compare and Contrast a Pseudocode and Flowchart (Cont’d)

Algorithm Development 14

Page 15: Unit 2_Algorithm Dev Elopement

A Very Simple Example

PROBLEM

You are required to develop a complete system which will enable the sum of three values to be calculated.

What do we have to do first?

Page 16: Unit 2_Algorithm Dev Elopement

Calculating Electricity Bills

The unit for electricity usage is kWh. For domestic usage, the tariff used is tariff A. The monthly rate for tariff A is 21.8 cents/unit for the first 200 unit, 25.8 cents/unit for the next 800 units and 27.8 cents/unit for each additional units. Given the amount of electricity units (in kWh) used by a customer, compute and print the amount of money needs to be paid by the customer to TNB.

Page 17: Unit 2_Algorithm Dev Elopement

Calculating Electricity BillsBegin

Print “Enter the usage (unit): ”Read usageif (usage > 200)

payment = 200 x 21.8usage = usage – 200if (usage > 800)

payment = payment + (800 x 25.8)usage = usage - 800payment = payment + (usage x 27.8)

elsepayment = payment + (usage x 25.8)

end_ifelse

payment = usage x 21.8end_if Print “Total payment is “, payment

End

Page 18: Unit 2_Algorithm Dev Elopement

Calculating PCC Tuition Fee

This program will calculate the total tuition fee for a PCC student. The program ask for total number of subject registered. Then it will ask for the number of credit hours for each subject. The program will print out the total tuition fee in RM. Assume that tuition fee is RM300 per credit hour

Page 19: Unit 2_Algorithm Dev Elopement

Calculating PCC Tuition Fee

Print “Enter total number of subject: “

Read totalSubjectcounter = 0totalFee = 0while (counter < totalSubject)

Print “Enter the number of credit of the subject: “Read credittotalFee = totalFee + (credit x 300)counter = counter + 1

end_whilePrint “The total fee is “, totalfee

Page 20: Unit 2_Algorithm Dev Elopement

Exercise

1. Write pseudocode of a program that calculate the total sum of series starting from 1 to a number specified by user. Example, if user input 4, the result is 1+2+3+4=10.

2. Write pseudocode of a program that will print the net income of a salesman in ABC Bhd given the total sales of a month. The basic salary is JA500,000. The commission is 5% of the first JA50,000.00 sales, 10% for next JA200,000.00 sales and 20% for the remaining. A sum of 11% of the gross income will be deducted for JA taxes and other contribution.

Page 21: Unit 2_Algorithm Dev Elopement

BeginPrint “Enter last number in the series: “Read lastNumbercounter = 1sum = 0while (counter<=lastNumber) do

sum = sum + countercounter = counter + 1

end_whilePrint “The sum of the series is “, sum

End

Answer