CSC 121. Problem Solving Lesson 3
CSC 121. Problem Solving Lesson 3
CSC 121. Problem Solving Lesson 3
ng
PROBLEM SOLVING
CSC 121 (3 UNITS)
Lecture 2
• Define an algorithm
• State its features, advantages and disadvantages
• Describe ways to implement algorithms
• Create flowchart to solve problems.
• Create pseudocodes solve problems
Algorithm
A step by step process for solving a problem.
Algorithm calculatesimpleinterest
Step 1:Start.
Step 2: Read the three input quantities P, N, and R.
Step 3: Calculate simple interest as
Simple interest = P* N* R/100
Step 4: Print simple interest.
Step 5: Stop.
Natural Language Algorithms Examples
Example 2: Write an algorithm to find the area of the triangle.
Algorithm findAreaOfTriangle
Step 1:Start.
Step 2: Input the given elements of the triangle namely sides 𝑏,
𝑐, and the angle between the sides 𝐴
Step 3: Area = 1/ 2 ∗ 𝑏 ∗ 𝑐 ∗ 𝑠𝑖𝑛(𝐴)
Step 4: Output the Area
Step 5: Stop.
Examples Algorithms
3. Write an algorithm to find the largest of three numbers 𝑋, 𝑌,
𝑍.
Algorithm FindLargestOfThreeNumbers
Step 1: Start
Step 2: Read 3 numbers and store in A, B, C
Step 3: Compare A and B. lf A > B then go to step 6
Step 4: Compare B and C if C > B then go to step 8
Step 5: print “B is largest” go to step 9
Step 6: Compare A and C if C > A then go to step 8
Step 7: Print ”A is largest” go to step 9
Step 8: Print “C is largest”
Step 9: Stop
Examples Algorithms
4, Write an algorithm to calculate the perimeter and area of
rectangle. Given its length and width
Algorithm calculatePerimeterAndAreaOfRectangle
Step 1:Start.
Step 2: Read length and width of the rectangle
Step 3: Calculate perimeter = 2* (length + width)
Step 4: Calculate area = length *width.
Step 5: Print perimeter
Step 6 Print area
Step 7: Stop
Examples Algorithms
5. Problem: Create an algorithm that multiplies two numbers
and displays the output.
algorithm: Multwonumbers
Step 1 Start
Step 2 Declare three integers x, y & z
Step 3 Enter values of x & y
Step 4 Multiply values of x & y
Step 5 Store result of step 4 to z
Step 6 Print z
Step 7 Stop
Flowchart
A flowchart is a pictorial or visual representation of
an algorithm.