0% found this document useful (0 votes)
12 views37 pages

CSC101 Slides - Week5-1 3

Uploaded by

atiwurchaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views37 pages

CSC101 Slides - Week5-1 3

Uploaded by

atiwurchaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

CSC 101 – INTRODUCTION TO

COMPUTER SCIENCE
FACULTY OF COMPUTING
DEPARTMENT OF COMPUTER
SCIENCE
WEEK 5 - ALGORITHM AND FLOW CHART
ALGORITHM AND FLOW CHART

In this week, we will discuss the following:


1. Introduction to algorithm
2. Syntaxes of algorithm
3. Problem Solving skills using algorithm
4. Type of Control Structures
INTRODUCTION
Intelligence is one of the key characteristics which differentiate a
human being from other living creatures on the earth.
Basic intelligence covers day to day problem solving and making
strategies to handle different situations which keep arising in day to
day life.
For example, if you have only N500 for your lunch and transportation
back home. The decision you make when trying to decide what to
buy for lunch in order to have enough money to transport yourself
back home comes with intelligence.
PROBLEM SOLVING
Can you think of a day in your life which goes without problem solving?
Answer to this question is of course, NO.
In our lives we are bound to solve problems and we solve problems in
almost all given tasks or decisions we make in the daily running of our lives.
Problem solving can be as trivial as:
◦ Changing your TV channel from one program to another that you were
waiting to watch.
◦ Replying someone who asked you for the time.
◦ Microwaving your meal to heat it up before eating.
PROBLEM SOLVING
We can define
Problem as a kind of barrier from achieving
something, and
Problem Solving as a process to get that barrier
removed by performing some sequence of activities.
ALGORITHM
Algorithm
◦ is a sequence of activities that must be processed in order to get
a desired output/result from a given input or for a given task.
◦ is formula or set of steps for solving a particular problem.
To be an algorithm, a set of rules must be unambiguous and have
a clear stopping point and ending point.
There may be more than one way to solve a problem, therefore,
there may be more than one algorithm for a given problem.
Properties of ALGORITHM
Finiteness: An algorithm must always terminate after a finite
number of steps.
Definiteness: Each step of an algorithm must be precisely
defined. It is done by well thought actions to be performed at
each step of the algorithm.
Input: Any operation you perform need some beginning
value/quantities associated with different activities in the
operation. So the value/quantities are given to the algorithm
before it begins.
Properties of ALGORITHM
Output: One always expects output/result (expected
value/quantities) in terms of output from an algorithm. The
result may be obtained at different stages of the algorithm.
Effectiveness: Algorithms to be developed/written using
basic operations.
ALGORITHM
In writing algorithms we will use following symbols for
different operations:
◦ ‘+’ for Addition
◦ ‘-’ for Subtraction
◦ ‘*’ for Multiplication
◦ ‘/’ for Division and
◦ ‘  ’ for assignment.
◦ ‘%’ for Modulus
ALGORITHM
In algorithm as in programming, you should be mindful that
there is a difference between an expression and a statement.
An EXPRESSION is a combination of values, variables, functions
and mathematical operations
A STATEMENT is a combination of one or more expressions that
executes instructions to complete a task.
Additionally, in algorithm as in programming, we deal with
variables and not values.
ALGORITHM
Variables can be some of the following with the given
properties:
Names: the name you use for your variable can be any
name. even a person’s name, such as, Stanley.
◦ It can also be of any length, lowercase and uppercase letter.
◦ Note that lower and upper case are considered to be different. That
is stanley, Stanley and STANLEY are NOT the same variable names.
There are reserved names called KEYWORDS/IDENTIFIERS that
cannot be used as variables: class, public, if, while, for etc.
ALGORITHM
Ensure your variable names give proper information about
what they should contain. For example, to accept age, it is
advisable to use the variable names associated with age
such as age, myAge etc.
Variables
◦ Can contain a number. Ex: name1, name2, age1 etc.
◦ can start with an underscore “_” Ex: _myName, _age.
◦ cannot start with a digit. Ex: 1name, 2age cannot be used.
Exercises
For example,
“2*3” is an expression while “2*3 = 5” is a statement.
“2 > 3” is an expression while “2 > 3” is False is a statement.

The statement “A  X*3” means “A will have a value of X*3” and it is


read as “Assign X*3 to the Variable A.”
Type of Control Structures
The algorithm and flowchart, classification to the
three types of control structures. They are:
◦ Sequence
◦ Selection (Branching)
◦ Loop (Repetition)
SEQUENCE
The Sequence is exemplified by order of statements placed one after
the other
◦ the one above or before another gets executed first.
◦ In flowcharts, sequence of statements is usually contained in the
rectangular process box.
Exercises
Question 1: Find the area of a Circle of radius r. Use the formula Area =
πr2. (πr2 = Pi *r*r)
Solution:
Step 1: Start
Step 2: Read\input the Radius r of the Circle
Step 3: Area  Pi*r*r // calculation of area // is used for comments
Step4: Print Area
Step 5: End
Exercises
Question 2: Write an algorithm to read two numbers and find their
sum.
Solution:
◦ Step1: Start
◦ Step2: Read\input the first num1.
◦ Step3: Read\input the second num2.
◦ Step4: Sum  num1+num2 // calculation of sum
◦ Step5: Print Sum
◦ Step6: End
BRANCH
The Branch refers to a binary decision based on some condition.
If the condition is true, one of the two branches is explored; if the
condition is false, the other alternative is taken.
This is usually represented by the ‘if-then’ construct in pseudo-codes,
flowcharts and programs.
In flowcharts, this is represented by the diamond-shaped decision
box.
This structure is also known as the selection structure.
Exercises
Problem 1: Write algorithm to find the greater between two
numbers/integers.
Solution A
◦ Step1: Start
◦ Step2: Read/input A and B
◦ Step3: If A greater than B then, C  A // if A > B, then C  A
◦ Step4: if B greater than A then C  B // if B > A, then C  B
◦ Step5: Print C
◦ Step6: End
Exercises
Problem 1: Write algorithm to find the greater between two
numbers/integers.
Solution B
◦ Step1: Start
◦ Step2: Read/input A and B
◦ Step3: if A > B
Print A “is Greater than “B
◦ else
Print B “is Greater than “A
◦ Step4: End
Exercises
Problem 3: A algorithm to find the largest value of any three
numbers.
Solution A
◦ Step 1: Start
◦ Step 2: Read/input A, B and C
◦ Step 3: If (A>=B) and (A>=C) then Max=A
◦ Step 4: If (B>=A) and (B>=C) then Max=B
◦ Step 5: If (C>=A) and (C>=B) then Max=C
◦ Step 6: Print Max
◦ Step 7: End
Exercises
Problem 3: A algorithm to find the largest value of any three numbers/integers.
Solution B
◦ Step 1: Start
◦ Step 2: Read/input A, B and C
◦ Step 3: Max A
◦ Step 4: If B > Max
Max  B
◦ Step 5 If C > Max
Max  C
◦ Step 6: Print Max
◦ Step 7: End
LOOP
The LOOP allows a statement or a sequence of statements to be
repeatedly executed based on some loop condition.
It is represented by the ‘while’, ‘do…while’ and ‘for’ constructs in
most programming languages, for unbounded loops and bounded
loops respectively.
Unbounded loops refer to those whose number of iterations
depends on the eventuality that the termination condition is
satisfied. While the bounded loops refer to those whose number of
iterations is known before-hand.
Properties and Syntaxes of
LOOPS
In a Loop, you must consider and adhere by the following:
◦ S1: The loop starts with the initialization of the control variable
◦ S2: The control variable must pass through the condition of the loop
◦ S3: If it satisfies the condition, you can then enter into the loop and
execute the defined statements.
◦ S4: The control variable is modified and you go back to S2.
◦ The Steps S2-S4 will continue until the condition in S2 is no longer been
satisfied.
Properties and Syntaxes of
LOOPS
The syntax of a Loop generally looks like this:
◦ Variable initialization
◦ Condition
◦ Statements
◦ Iteration (increment or decrement of initialized variable)
◦ Go back to condition
◦ End of Loop
Types of LOOPS
There are so many types of loops. In this course, we
will discuss the:
1. While Loop
2. Do … While Loop
3. For Loop
While Loop
The structure of the While Loop looks like this:
initialization
while (condition)
Statements
iteration
end loop
Exercises
Problem 1: Using the while loop, write an algorithm to display the numbers
between 1 and 20
Start
S1: j ← 1 //initialization
S2: while (j <= 20) // j < 21 //condition
Display j //statement
j←j+1 //increment
go to S2
End loop
Exercises
Problem 2: Using a while loop, write an algorithm to display the even numbers
between 0 and 99
Solution A
Start
S1: j ← 0
S2: while (j <= 99) // OR j < 100
Display j //statement
j←j+2 //increment
go to S2
End
Exercises
Problem 3: Using a while loop, write an algorithm to display the even numbers between 0 and
99
Solution B
Start
S1: j ← 0
S2: while (j <= 99) // OR j < 100
if j % 2 == 0 //using modulus
Display j //statement
j←j+1 //increment
go to S2
End
Do…While Loop
The structure of the Do…While Loop is similar to
reverse process of the While loop.
initialization
Do
Statements
Iteration
while (condition)
end loop
Exercises
Problem 4: using the do…while loop, write an algorithm to display the
numbers between 1 and 20
Start
S1: a ← 1 //initialization
S2: do
Display j //statement
a←a+1 //increment
while (a<= 20 ) //condition
End
For Loop
The structure of the For Loop looks like this:
for (initialization; condition; iteration)
statements
end loop
Exercises
Problem 5: Using a for loop, write an algorithm to display the
numbers between 1 and 20
Start
S1: for b1; b <= 20; b b +1)
Display b //statement
go to S1 and continue from b b +1
End
Exercises
Problem 5: An algorithm to calculate the sum of numbers between 1 and 20.
Start
S1: j ← 1, Sum 0
S2: if j <= 20
Sum  Sum + j
j←j+1
go to S2
End
Exercises
Problem 6: An algorithm to calculate the sum of numbers between 1
and 20 and find their average.
Problem 7: An algorithm to display the numbers from 30 to 1 in that
order.
FLOWCHART
Week 6 will discuss flowchart.

You might also like