Problem solving techniques Algorithm flowchart 952021 Mr Mohamed

Problem solving techniques Algorithm & flowchart 9/5/2021 Mr. Mohamed El. said 1

Problem solving �Problem solving means: process of transforming the problem description into a solution using problem solving techniques. �To solve a problem , five steps are required: 1. Define the problem 2. Plan the solution 3. Implement the solution. 4. Test and debug the program. 5. Document the program. 9/5/2021 Mr. Mohamed El. said 2

Cont. � Define the problem: Specify the problem requirements i. e. � Input(s) and their form, processing and output(s) and their form required. � Special constraints or conditions (if any) � Any formulas or equations to be used � Plan the solution: Develop a list of steps, arranged in a specific logical order which, when executed, produces the solution for a problem. � Implement the solution: Use any high level programming language to write a computer program for that algorithm. 9/5/2021 Mr. Mohamed El. said 3

Cont. �Test & debug the program: Once the program is written, it may contain some programming errors. 4 phases are followed for testing: i. Translating/compilation. ii. Desk-checking error detection iii. Debugging error correction iv. Execution: to demonstrate program correctness �Documenting the program: �Write a description that explains what the program does. Write comments for line codes or create separate text file. 9/5/2021 Mr. Mohamed El. said 4

Divide-and-Conquer �It is the most-well known algorithm design strategy , it uses top-down design: Original problem Sub-problem 1 Sub-problem 2 a solution to Sub-problem 1 a solution to Sub-problem 2 a solution to the original problem �Divide problem into several smaller sub problems �Conquer: solve the several smaller sub problems �Combine the solutions of smaller sub-problem to get a solution to the ordinal problem. 9/5/2021 Mr. Mohamed El. said 5

Program Design techniques: �Algorithm: step by step procedure to solve a particular problem. �Characteristics: � An algorithm must have input(s). � An algorithm must have output(s) � An algorithm must have finite time for execution and termination. �An algorithm must have no ambiguities. Each instruction in an algorithm should be defined clearly, basic and easy to perform. 9/5/2021 Mr. Mohamed El. said 6

Algorithm Representation �An algorithm can be represented using Pseudocode or Flowchart. �Pseudocode is an English-like language with limited vocabulary that can be used to design and describe algorithms. �Flowchart: �Graphical representation of the algorithm. �Flowcharts is a tool for showing steps of solution using symbols �The symbols used are geometrical shapes that are connected by flow lines. �It is an alternative to pseudocode; whereas a pseudocode description is verbal, a flowchart is graphical in nature. 9/5/2021 Mr. Mohamed El. said 7

Flowchart Symbols Terminal symbol - indicates the beginning and end points of an algorithm. Process symbol - shows an instruction other than input, output or selection. Input-output symbol - shows an input or an output operation. Selection symbol - shows a selection process for two-way selection. On-page connector - provides continuation of logical path at another point in the same page. Flow lines - indicate the logical sequence of execution steps in the algorithm. 9/5/2021 Mr. Mohamed El. said 8

Example: -1 Yummy Cupcake �Problem: You are required to calculate the amount to be paid by a customer buying cupcakes. �Solution (Algorithm) 1. Read the quantity of cupcake purchased 2. Read the price per cupcake 3. Calculate total amount to pay: Total amount to pay = quantity of cupcake x price per cupcake 4. Display the total amount to pay 9/5/2021 Mr. Mohamed El. said 9

Control Structures Any algorithm can be described using only 3 control structures: sequence, selection and repetition. �Sequence: A series of statements that are executed in the order they are written in an algorithm. �Selection: Defines two courses of action depending on the outcome of a condition. A condition is an expression that is, when computed, evaluated to either true or false. �Repetition: Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. 9/5/2021 Mr. Mohamed El. said 10

The Sequence control structure �Format: begin statement 1. statement 2. … … statement n. end begin statement 1 statement 2 … statement n end 9/5/2021 Mr. Mohamed El. said 11

Example: -2 �Problem: calculate a person’s age Begin read birth year age = current year – birth year display age End begin read birth year age = current year – birth year Display age end 9/5/2021 Mr. Mohamed El. said 12

The Selection control structure � Single selection: EX 1: if (number is odd number) print “This is an odd number” end_if No � Double selection: � The keyword used are if and else. � Format: if (condition) then-part else-part end_if 9/5/2021 elsestatement(s) Mr. Mohamed El. said Condition? Yes thenstatement(s) 13

Example: -3 Begin read age if (age > 55) print “Old” else print “Young” end_if End Begin read age YES age > 55? NO print “Young” print “Old” End 9/5/2021 Mr. Mohamed El. said 14

Nested selection: Example: -4 if (number is equal to 1) print “One” else if (number is equal to 2) print “Two” else if (number is equal to 3) print “Three” else print “Other” end_if 9/5/2021 Mr. Mohamed El. said 15

The Repetition control structure �The keyword used is while. �Format: while (condition) loop-body end_while Condition? True Loop Statement(s) False 9/5/2021 Mr. Mohamed El. said 16

Example: -4 Problem: Write n ALGORITHM that reads and displays the age of 10 people (one after another). For this problem, we need a way to count how many people whose age have been processed (read and displayed). Therefore, we introduce a concept of counter, a variable used to count the number of people whose age have been processed by the program. 9/5/2021 17 Mr. Mohamed El. said

Solution as Algorithm: Begin Counter initialization number of users giving his age = 1 while (number of users giving his age <= 10) read the age from the user. print the user age. Loop condition number of user giving his age + 1 end_while Updating counter End Begin users = 1 while (users <= 10) read age print age. users = users + 1 end_while End 9/5/2021 18 Mr. Mohamed El. said

Solution as flowchart: Begin users = 1 NO users <= 10? End YES read age print age users =users + 1 9/5/2021 19 Mr. Mohamed El. said

Example: -5 �Write an ALGORITHM that will calculate and print the age of 10 persons, given their birth year. If the age of the person is above 55, then the ALGORITHM will print “Old”, otherwise, the program will print “Young”. 9/5/2021 20 Mr. Mohamed El. said

Note that in this example, we are using all the three control structures: sequence, selection and repetition Solution: Begin End users = 1 while (users <= 10) Read birth year age = current year – birth year print age if age > 55 print “old” else print “young” end_if users = users + 1 end_while Exercise: Draw the flowchart diagram for last problem. 9/5/2021 21 Mr. Mohamed El. said

Volume calculation Write an algorithm that read the value of height, width and length of a box from the user and print its volume. Also draw a flowchart for the problem. 9/5/2021 22 Mr. Mohamed El. said

Calculating Electricity Bills The unit for electricity usage is k. Wh. For domestic usage, the monthly rate 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 k. Wh) used by a customer, calculate the amount of money needs to be paid by the customer to TNB. A bill statement needs to be printed out. Write an algorithm and draw a flowchart to solve the above problem. 9/5/2021 23 Mr. Mohamed El. said

Sum of 1 to 10 Write an algorithm and draw a flowchart that computes and prints the sum of all integers between 1 and 10. 9/5/2021 24 Mr. Mohamed El. said
- Slides: 24