You are on page 1of 4

CSC126 Introduction to Computer Programming

Lab/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 1:

Find the area of a Circle of radius r (A = πr2)

Input: Radius, r
Process: A = πr2
Output: Area of circle, A

Pseudocode:

Start
1. Read the radius, r
2. Calculate area, A by using formula:
A = 3.14 * r * r
3. Display area, A of the circle
End

Flowchart:
Start

Read radius, r

Calculate the area, A by the using formula:


A = 3.14 * r * r

Display area, A

End
Example Problem 2:
Input: A, B
Design an algorithm to determine the greater number
between two numbers Process:
1) If A > B
2) If A < B

Output:
1) Display “A”
2) Display “B”

Pseudocode:

Start
1. Read the number, A and B
2. If A > B, then display “A”
3. Else, display “B”
End

Start
Flowchart:

Read A and B

Yes If No
A > B

Display “A” Display “B”

End
Exercises

Task 1: Write an algorithm to convert Fahrenheit to Celsius


C = (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” and if the
result is negative, display “Negative”.

Task 5: Write an algorithm to subtract 2 numbers. If the result is positive, display “Positive” and if the
result is negative, display “Negative”; if the result is 0, display “Zero”.

Task 6: 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 7: Write an algorithm to find the largest value of any three numbers

You might also like