Lecture 6 Pseudocode
Lecture 6 Pseudocode
Lecture 6 Pseudocode
CSM 184
ALGORITHMS
Computer programming can be divided into two phases:
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.
A finite set of an instruction that specifies a sequence of operation to be carried out in order
to solve a specific problem.
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.
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.
•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 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
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
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:
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
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:
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
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.
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.