This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

A typical programming task can be divided into two phases: Problem solving phase

produce an ordered sequence of steps that describe solution of problem this sequence of steps is called an algorithm implement the program in some programming language

Implementation phase

Steps in Problem Solving

First produce a general algorithm (one can use pseudocode) pseudocode) Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language. Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English.

Pseudocode & Algorithm

Example 1: Write an algorithm to determine a student s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

Pseudocode & Algorithm Pseudocode: Pseudocode: Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print FAIL else Print PASS .

M3.M4 Step 2: GRADE n (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print FAIL else Print PASS endif .Pseudocode & Algorithm Detailed Algorithm Step 1: Input M1.M2.

.The Flowchart (Dictionary) A schematic representation of a sequence of operations. as in a manufacturing process or computer program.

g. control flow from one action to the next .The Flowchart A Flowchart shows logic of an algorithm emphasizes individual steps and their interconnections e.

Flowchart Symbols Basic .

M2.M4 Step 2: GRADE n (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then Print ³FAIL´ else Print ³PASS´ endif Y N IS GRADE<5 0 PRINT ³PASS´ PRINT ³FAIL´ STOP .M2.M3.M4 GRADEn(M1+M2+M3+M4)/4 Step 1: Input M1.Example START Input M1.M3.

Pseudocode: Pseudocode: Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM) .Example 2 Write an algorithm and draw a flowchart to convert the length in feet to centimeter.

Example 2 Algorithm Step 1: Input Lft Step 2: Lcm n Lft x 30 Step 3: Print Lcm Flowchart START Input Lft Lcm n Lft x 30 Print Lcm STOP .

Example 3 Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode Input the width (W) and Length (L) of a rectangle Calculate the area (A) by multiplying L with W Print A .

L AnLxW Print A STOP .L Step 2: A n L x W Step 3: Print A START Input W.Example 3 Algorithm Step 1: Input W.

and the roots b )/2a are: x1 = ( b + d 4ac and x2 = ( b )/2a d)/2a )/2a .Example 4 Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation ax2 bx c ! 0 Hint: d = sqrt ( 2 ).

c) of the quadratic equation Calculate d Calculate x1 Calculate x2 Print x1 and x2 .Example 4 Pseudocode: Pseudocode: Input the coefficients (a. b.

c d n sqrt ( b vb 4 v a v c x1 n ( b + d) / (2 x a) x2 n ( b d) / (2 x a) Print x1. b. x2 ) START Input a.x2 STOP . b.Example 4 Algorithm: Algorithm: Step 1: Step 2: Step 3: Step 4: Step 5: Input a. c d n sqrt(b x b ± 4 x a x c) x1 n(±b + d) / (2 x a) X2 n (±b ± d) / (2 x a) Print x1 .

DECISION STRUCTURES The expression A>B is a logical expression it describes a condition we want to test if A>B is true (if A is greater than B) we take the action on left print the value of A if A>B is false (if A is not greater than B) we take the action on right print the value of B .

DECISION STRUCTURES Y is A>B N Print A Print B .

IF±THEN±ELSE STRUCTURE IF±THEN± The structure is as follows If condition then true alternative else false alternative endif .

IF±THEN±ELSE STRUCTURE IF±THEN± The algorithm for the flowchart is as follows: If A>B then print A Y N is else A>B print B Print Print endif A B .

Relational Operators Relational Operators Operator > < = u e { Description Greater than Less than Equal to Greater than or equal to Less than or equal to Not equal to .

MAX . determines the largest value and prints the largest value with an identifying message. ALGORITHM Step 1: Input VALUE1. VALUE2 Step 2: if (VALUE1 > VALUE2) then (VALUE1 MAX n VALUE1 else MAX n VALUE2 endif Step 3: Print The largest value is .Example 5 Write an algorithm that reads two values.

VALUE2 Y is VALUE1>VALUE2 N MAX n VALUE1 MAX n VALUE2 Print ³The largest value is´.Example 5 START Input VALUE1. MAX STOP .

NESTED IFS One of the alternatives within an IF THEN ELSE statement may involve further IF THEN ELSE statement .

Example 6 Write an algorithm that reads three numbers and prints the value of the largest number. .

MAX . N2>N3] else MAX n N3 [N3>N2>N1] endif endif Step 3: Print The largest number is . N3 Step 2: if (N1>N2) then (N1>N2) if (N1>N3) then (N1>N3) MAX n N1 [N1>N2.Example 6 Step 1: Input N1. N1>N3] else MAX n N3 [N3>N1>N2] endif else if (N2>N3) then (N2>N3) MAX n N2 [N2>N1. N2.

.Example 6 Flowchart: Draw the flowchart of the above Algorithm.

hours absent (ABSENT) and determine the bonus payment (PAYMENT).Example 7 a) b) Write and algorithm and draw a flowchart to read an employee name (NAME). overtime hours worked (OVERTIME). .

Example 7 Bonus Schedule OVERTIME ± (2/3)*ABSENT >40 hours >30 but e 40 hours >20 but e 30 hours >10 but e 20 hours e 10 hours Bonus Paid $50 $40 $30 $20 $10 .

ABSENT Step 2: if (OVERTIME±(2/3)*ABSENT > 40) then PAYMENT n 50 else if (OVERTIME±(2/3)*ABSENT > 30) then PAYMENT n 40 else if (OVERTIME±(2/3)*ABSENT > 20) then PAYMENT n 30 else if (OVERTIME±(2/3)*ABSENT > 10) then PAYMENT n20 else PAYMENT n 10 endif Step 3: Print ³Bonus for´.OVERTIME. PAYMENT . NAME ³is $´.Step 1: Input NAME.

Example 7 Flowchart: Draw the flowchart of the above algorithm? .

Start Flowchart with loop Is light less than 50? No Yes Turn lamp on .

Start Flowchart with loop Is light less than 50? No Yes Turn lamp on Turn lamp off .

Start Flowchart with loop Is light less than 50? No Yes Turn lamp on Turn lamp off Beep .

Start Flowchart with loop Is light less than 50? No Yes Turn lamp on Turn lamp off Beep .

6 T2a .Scenarios An automatic bell rings in a shop when somebody enters Gates open when somebody approaches them The turnstile at the fun park registers people as they want to enter and displays a green light 7.

Flowchart with loop .

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd