Christmas Term - JS 2 ICT - Week 6-7 - Computer Programming Language II
Christmas Term - JS 2 ICT - Week 6-7 - Computer Programming Language II
Algorithms
The idea of an algorithm is to describe exactly how to do something so that
any person (or computer!) can do it without
understanding how the person who wrote it was thinking.
They are usually written in any human language and some mathematical
notations.
Example:
An algorithm to find the average of four numbers:
start
Step 1 – enter the first number; N1
Step 2 – enter the second number; N2
Step 3 – enter the third number; N3
Step 4 – enter the fourth number; N4
Step 5 - calculate the total (N1+N2+N3+N4); N5
Step 6 – calculate the average (N5/4); N6
Step 7 – print average (N6)
Step 8 – end
In computing, algorithms are usually represented as a;
program flowchart or
in pseudo-code.
Examples of
flowcharts
Examples of Here is a simple flowchart
flowcharts used in showing a simple
summation process
This flowchart shows doing
a homework by a student
This flowchart shows how
profit and loss can be
calculated
Example 1. Design an algorithm and the corresponding flowchart for
adding the test scores as given below:
a) Algorithm
1. Start
2. Sum = 0
Evaluation …
6. Add to sum
8. Add to sum
16. Stop
Example 2: The problem with this
algorithm is that, some of the steps
appear more than once, i.e. step 5 get
second number, step 7, get third number,
etc. One could shorten the algorithm or
flowchart as follows:
1. Start
Evaluation … 2.
3.
Sum = 0
Get a value
4. sum = sum + value
5. Go to step 3 to get next Value
6. Output the sum
7. Stop
Create a flowchart to add 10 and
20 and print out the sum
STEPS
Evaluation … Initialise sum = 0 (process)
Enter the numbers (I/O)
Add them and store the result in sum
(process)
Print sum (I/O)
Create a flowchart to find
the sum of 5 numbers
STEPS
Initialise sum = 0 and count =
0 (process)
Enter n (I/O)
Evaluation … Find sum + n and assign it to
sum and then increment count
by 1 (process)
Is count < 5 (decision)
If yes go to step 2
Else
Print sum (I/O)
Create a flowchart to log in to
facebook account
STEPS
Enter www.facebook.com in browser
(I/O)
Facebook homepage loads (process)