Tutorial 1: Problem Solving using Algorithm
Objectives:
1. To illustrate algorithm before writing C++ program.
a. Design a simple algorithm using pseudocode
b. Design a simple algorithm using flowchart
Pseudocode
• A sequence of activities to be processed for getting desired output from a given input
• Before writing a pseudocode for a problem, one should find out what is/are the inputs and what
is/are expected output
• It uses short terms or simple English
• While writing pseudocode we will use following symbol for different operations:
“+” for Addition
“-” for Subtraction
“*” for Multiplication
“/” for Division
Flowchart
• A type of diagram that represents a workflow or process
• A flowchart can also be defined as a diagrammatic representation of an algorithm, a step-by-step
approach to solving a task
• Common flowchart symbols:
Example Problem
Problem : Find the area of a Circle of radius r (A = pr2)
Input : Radius r of the Circle
Process : A = 3.14*r*r
Expected Output : Area of circle
Pseudocode:
Start
1. Read the radius, r
2. Calculate area, A using formula:
A=3.14*r*r
3. Display area, A of the circle
End
Flowchart:
Start
Read radius, r
Calculate area, A:
A = 3.14*r*r
Display area, A
A
End
Example: Design an algorithm to determine the greater number between two numbers
Pseudocode:
Start
1. Read the number, A and B
2. If A>B, then display “A”
3. Else, display “B”
End
Flowchart:
Start
Read number, A and B
Yes No
If
A >B
Display “A” Display “B”
End
Exercises
Task 1: Write an algorithm to convert Fahrenheit to Celsius
Celsius = (F-32) x (5/9)
Task 2: Write an algorithm to display the area of a rectangle by accepting length and width from the user.
Task 3: Write an algorithm for a program that can prompts user to enter total number of umbrellas he/she
wants to buy. Price of one umbrella is RM15.00. This program will calculate and displays total price of
these umbrellas to user.
Task 4: Write an algorithm to subtract 2 numbers. If the result is positive, display “Positive”; if the result
is negative, display “Negative”; if the result is 0, display “Zero”.
Task 5: Write an algorithm to ask user to enter weight. Program will only display status based on below
condition:
• weight >100kg, display “Obese”
• weight < 30kg, display “Underweight”
• others, display “Normal”
Task 6: Write an algorithm to find the largest value of any three numbers