You are on page 1of 35

ALGORITHMS

CSM 184
ALGORITHMS
Computer programming can be divided into two phases:

Problem solving phase


◦ Make an ordered sequence of steps that solves a problem.
◦This sequence of steps is called an algorithm.

Implementation phase
◦ Implement using a programming language.
Algorithm
 It is a list of instructions specifying a precise description of a step by step process that
terminates after a finite number of steps for solving an algorithm problem producing
the correct answer in the end.

 It is a recipe for solving problems.

 A finite set of an instruction that specifies a sequence of operation to be carried out in order
to solve a specific problem.

 An unambiguous procedure specifying a finite number of steps to be taken.


Algorithms
In the problem-solving phase of computer programming, you will be designing
algorithms.

This means that you will have to be conscious of the strategies you use to solve
problems in order to apply them to programming problems.

These algorithms can be designed though the use of flowcharts or pseudocode.


Algorithm:
Every computer solution you write must have the following attributes:
1. Finiteness: It implies that the computer solution should be able to produce the
required results or output after a finite number of steps.

2. Definiteness: It means all statements in a computer solution should be precise


and well defined such that it will not give room for ambiguity or confusion

3. Generality: It implies that a computer solution should be capable of solving


problems of a particular type or class and not just a particular set of input.
Algorithm:
Every computer solution you write must have the following attributes:
4. Effectiveness : By effectiveness, we mean that all operations involved in a
computer solution should be basic and capable of being performed
mechanically in a finite number of steps

5. Have input and output capabilities: It implies that a computer solution should
have a precise initial input data from a specified set that can be processed
internally to generate the expected output.
METHODS OF SPECIFYING
ALGORITHM
Pseudocode - Pseudocode (which means fake code, because its not really
programming code) specifies the steps required to accomplish the task.

 Flowchart - a traditional graphical tool with standardized symbols. Show the


sequence of steps in an algorithm.
•Pseudocode is a type of structured English that is used to specify an algorithm.

•Pseudocode cannot be compiled nor executed, and there are no real formatting or
syntax rules.
Pseudocode
Pseudocode is an artificial and informal language that helps you develop algorithms.

Pseudocode is similar to everyday English; it is convenient and user friendly although it


is not an actual computer programming language.

Pseudocode programs are not executed on computers. Rather, they merely help you
"think out“ a program before attempting to write it in a programming language such as
C++.
Structure of a computer solution
Every computer solution should have the following structure but not necessarily in the order
presented below. This is because, computer solutions solve different problems
Some computer solutions may or may not require all the steps listed below:

1. The input step: This is used to define various inouts that are required by the computer
solution. The input data can be read from a file, entered from a keyboard or generated through
an assignment statement.

2. The assignment step: This step is used to define the various mathematical expressions that
are required to solve a problem at stake
Structure of a computer solution
Some computer solutions may or may not require all the steps listed below:
3. The decision step: This step is used to define the decisions that are to be taken in
obtaining the required output. Each decision involves selecting one of the possible
alternatives at a time

4. The repetitive/iterative/looping step: This is used to define statements that are


required to be executed a number of times

5. The output step: This step defines the various outputs that are expected to be
obtained from a computer solution
Variables
Values that you may want a computer to process are normally kept in names known
as variables.

We shall use one word or at most two words separated by the underscore.

We would avoid the use of special characters, punctuation marks, monetary symbols in a
variable name

For eg. If we want to accept values representing the age, gross pay, tax and net pay, we can
declare variables such as age, grosspay, tax and netpay
How to write computer solutions
We will consider a few problems and see how computer solutions are written.

Since we have not yet learnt any programming language, we will be using
pseudocodes

We shall try and write all our statements as close as possible to mathematical expressions
where necessary

Non-mathematical statements should be as short as possible


How to write computer solutions
As indicated earlier, a computer solution usually consists of input, output, decision making,
repetitive and assignment statements. The following defines how to make use of these

For inputs: We shall try to use the following terminologies when we want to indicate that a
computer user would have to enter a value (values) for processing: READ, INPUT,
ACCEPT, ENTER and GET

For example: INPUT age means that a user should enter a value to be stored in a variable
called age
How to write computer solutions
For output, we shall use any of the following terminologies when we want to
indicate that a computer solution should give results: WRITE, DISPLAY, PUT,
PRINT.

For example, WRITE age means that we want the computer to display to the
screen a value stored in a variable called age
How to write computer solutions
For decision making: Whenever we need to make a decision, we shall use IF statement
with the following structure:

IF condition THEN statement (s) ELSE statement (s)


The statement can be spread over several lines.
Example: If we need to compare the value in variable A and B and write the larger of the
two, then we shall write a statement as shown below:
IF A>B
WRITE A
ELSE
WRITE B
How to write computer solutions

For repetitive/ iterative/looping: We shall use DO, WHILE and


FOR in most cases.

To mark the end of a DO for example, we shall write or use


END OF DO
Rules for writing pseudocode
1.Always capitalize the initial word

2.Have only one statement per line.

3.Indent to show hierarchy, improve readability, and show nested constructs.


Rules for writing pseudocode
4. Always end multiline sections using any of the END keywords (END
OF IF, END OF WHILE, etc.).

5. Keep your statements programming language independent.

6. Keep it simple, concise, and readable.


Example: Computer solution for finding
sum of three numbers
Let us assume we want to write a computer The steps needed to solve the problem are as follows:
solution for finding the sum of three numbers.

1. Accept the three numbers into the three


To solve this problem, we will need three variables x, y and z
variable names to store each of the three 2. Add x, y, z and store the results in sum
numbers.
3. Write out sum

Let us use the names x, y and z to represent the


numbers we want to find their sum.
Adding three numbers: example using
pseudocode
We can write the above statements in a more compact way or a manner close to
programming language as follows

1. INPUT x, y, z
2. sum = x+y+z
3. WRITE sum
4. STOP
Adding four numbers: example using
pseudocode
Similarly, to write a computer solution to find the sum of 4 numbers, we
would need four variables to store the four umbers. In this case, the
computer solution would be as follows:

1. INPUT w, x, y, z
2. sum = w+x+y+z
3. WRITE sum
3. STOP
Exercise 1

Write a computer solution using pseudocode to


read two numbers and multiplies them together
and print out their product.
Exercise 2

Write a computer solution using pseudocode to accept four marks


from a person, and finds the average of the marks. If the average is
less than 40, the computer should output “FAIL” else the computer
should output “PASS”
Exercise 3

Write a computer solution using pseudocode


to accept a temperature reading in Fahrenheit,
convert it to degrees Celsius and display the
results to the computer screen
Exercise 4

Write pseudo code that performs the following: Ask a user to


enter a number. If the number is between 0 and 10, write the
word blue. If the number is between 10 and 20, write the word
red. if the number is between 20 and 30, write the word green.
If it is any other number, write that it is not a correct color
option.
Example: Write a computer solution in
pseudocode to find the average of N numbers
1. The inputs to the program would be:
Write a computer solution in pseudocode to find the
average of N numbers
A. We need to know the number of students (N).
This is required so that when the solution is translated
into a program, it will enable the computer to know
the number of times a user should prompted to enter
To be able to write down the steps needed, we need to a student’s age
identify our inputs, mathematical formulas if any and
expected output.
B. all the ages of the N students involved. These ages
These are as follows:
would be accepted as input one at a time
Example: Write a computer solution in pseudocode to find
the average of N numbers

2. Output : The output will be the computed average based on


all the ages accepted as input

3. Processing /Formula: We need to find the sum of all ages and


then divide by the total number of students.
Analysis of the problem: Write a computer
solution in pseudocode to find the average of N numbers
From the above problem, it is realized that we need the following:

i. An accumulator we will refer to as SUM, to add the ages of the students involved. This “SUM”
variable must first be initialized to zero.
Mathematically, this can be written as SUM = 0;

We have to set SUM to zero since we want to be sure that before we start getting the ages, we
have not put any value in SUM that would affect our final results.
Analysis of the problem: Write a computer
solution in pseudocode to find the average of N numbers
From the above problem, it is realized that we need the following:

ii. A counter ( we will refer to this as counter)


A counter is required to keep track of the number of students whose ages have been accepted
from the user and fed into the computer so as to determine at any point in time whether or not
more data should be accepted. We shall also set counter to zero as COUNTER = 0.

We are setting counter to zero because at this stage, no data has been entered
Analysis of the problem: Write a computer
solution in pseudocode to find the average of N numbers

iii. The next thing we have to do is to ask for a student’s age. We shall
use the word “READ” to get an input from the user

We shall also use a variable age to hold the input value being read

So this step can be written as READ AGE


Analysis of the problem: Write a computer
solution in pseudocode to find the average of N numbers
From the above problem, it is realized that we need the following:

Iv. The AGE should be added to the content of the accumulator, SUM.
After this addition, the new value of SUM should replace whatever value was in
SUM before the addition
We will therefore write this as SUM = SUM + AGE.

Note that this statement SUM = SUM + AGE means add the value of AGE to the
current value of SUM and replace the value in SUM by the results of the addition
Analysis of the problem: Write a computer
solution in pseudocode to find the average of N numbers
From the above problem, it is realized that we need the following:

V. Whenever an age is read and added to the SUM, the value of the counter should be increased
by 1, that is add 1 to the current value of COUNTER and then replace the old value of the
COUNTER by the new value.

In pseudocode, this can be written as COUNTER = COUNTER + 1.

The value of COUNTER will enable us to check at any given time whether all input values have
been entered or not.
Analysis of the problem: Write a computer
solution in pseudocode to find the average of N numbers
From the above problem, it is realized that we need the following:

Vi. The reading of AGEs, adding AGEs to SUM and increasing the value of
COUNTER by one should be repeated until the COUNTER value is equal to
the number of students, N.

When this condition is met, the AVERAGE can then be computed by


dividing the content of the accumulator SUM by the number of students,
N
FINAL PSEUDOCODE SOLUTION: Write a
computer solution in pseudocode to find the average of N
numbers
The entire pseudocode for finding the average of N numbers is shown below
1. SET a counter COUNTER to 0
2. Set an accumulator SUM to 0
3. Read number of students, N
4. While COUNTER <=N
5. READ a student’s age, AGE
6. ADD age to SUM
7. Increase COUNTER by 1
8. END OF WHILE
9. Computer average, AVERAGE = SUM divided by N
10. PRINT AVERAGE
11. STOP

You might also like