CPE 101. Programming The Computer
CPE 101. Programming The Computer
Machine Operating
Language System Store
Program
Problem definition
The first stage requires a good understanding of the
problem. The programmer (i.e. the person writing the program)
needs to thoroughly understand what is required of a problem. A
complete and precise unambiguous statement of the problem to
be solved must be stated. This will entail the detailed specification
which lays down the input processes and output required.
Devising the method of solution
The second stage involved is spelling out the detailed
algorithm. The use of a computer to solve problems (be it scientific
or business data processing problems) requires that a procedure or
an algorithm be developed for the computer to follow in solving
the problem.
Developing the method of solution
There are several methods for representing or developing
solutions to a problem. Examples of such methods are: algorithms,
flowcharts, pseudo code, and decision tables.
Writing the instructions in a programming language
After outlining the method of solving the problem, a proper
understanding of the syntax of the programming language to be
used is necessary in order to write the series of instructions
required to get the problem solved.
Translating the instructions into machine sensible form
After the program is coded, it is converted into machine
sensible form or machine language. There are some specially
written programs that translate users programs (source programs)
into machine language (object code). These are called translators.
Compilers translate instructions that machines can execute at a go,
while interpreters accept a program and execute it line-by-line.
During translation, the translator carries out syntax check on the
source program to detect errors that may arise from wrong use of
the programming language.
Program debugging
A program might not execute successfully the first time due to
the presence of a few errors (bugs). Debugging is the process of
locating and correcting errors. There are three classes of errors.
• Syntax (grammar) errors: Caused by coding mistake (illegal
use of a feature of the programming language).
• Logic (semantic) errors: Caused by faulty logic in the design
of the program. The program will work but not as intended.
• Execution (run-time) errors: The program works as
intended but illegal input or other circumstances at run-
time makes the program stop.
There are two basic levels of debugging. The first level called
desk checking or dry running is performed after the program has
been coded. Its purpose is to locate and remove as many logical
and clerical errors as possible. The program is then read (or
loaded) into the computer and processed by a language translator.
The function of the translator is to convert the program
statements into the binary code of the computer called the object
code. As part of the translation process, the program statements
are examined to verify that they have been coded correctly, if
errors are detected, the language translator generates a series of
diagnostics referred to as an error message list. With this list in the
hand of the programmer, the second level of debugging is reached.
The error message list helps the programmer to find the
cause of errors and make the necessary corrections. At this point,
the program may contain entering errors, as well as clerical errors
or logic errors. The programming language manual will be very
useful at this stage of program development. After corrections
have been made, the program is again read into the computer and
again processed by the language translator. This is repeated over
and over again until the program is error-free.
Program testing
The purpose of testing is to determine whether a program
consistently produces correct or expected results. A program is
normally tested by executing it with a given set of input data
(called test data), for which correct results are known. For
effective testing of a program, the testing procedure is broken into
three segments. The programmer can use any of these three
alternatives to locate the bugs.
a. The program is tested with inputs that one would
normally expect for an execution of the program.
b. Valid but slightly abnormal data is injected (used) to
determine the capabilities of the program to cope with
exceptions. For example, minimum and maximum
values allowable for a sales amount field may be
provided as input to verify that the program processed
them correctly.
c. Invalid data is inserted to test the program's error-
handling routines. If the result of the testing is not
adequate, then minor logic errors still abound in the
program.
Other methods of testing a program for correctness include:
• Manual walk-through: The programmer traces the
processing steps manually to find the errors, pretending to
be the computer, following the execution of each
statement in the program, noting whether or not the
expected results are produced.
• Use of tracing routines: If this is available for the language’s
development interface, this is similar to manual
walkthrough, but is carried out by the computer; hence it
takes less time and is not susceptible to human error.
• Storage dump: This is the printout of the contents of the
computer's storage locations. By examining the contents of
the various locations when the program is halted, the
instruction at which the program is halted can be
determined. This is an important clue to finding the error
that caused the halt.
• Program documentation: A documentation of the program
should be developed at every stage of the programming
cycle. The following are documentations that should be
done for each program.
(a) Problem Definition Step
• A clear statement of the problem
• The objectives of the program (what the program is to
accomplish)
• Source of request for the program.
• Person/official authorizing the request
(b) Planning the Solution Step
• Flowchart, pseudo code or decision tables
• Program narrative
• Descriptive of input, and file formats
(c) Program source coding sheet
(d) User manual to aid persons who are not familiar with the
program to apply it correctly. The manual contains a
description of the program and what it is designed to
achieve.
(e) Operator manual to assist the computer operator to
successfully run the program. This manual contains:
i) Instructions about starting, running and terminating
the program,
ii) Message that may be printed on the console or VDU
(terminal) and their meanings.
iii) Setup and take down instruction for files.
Advantages of Program Documentation
a. It provides all necessary information for anyone who comes
in contact with the program.
b. It helps the supervisor in determining the program's
purpose, how long the program will be useful and future
revisions that may be necessary.
c. It simplifies program maintenance (revision or updating).
d. It provides information as to the use of the program to
those unfamiliar with it.
e. It provides operating instructions to the computer
operator.
5.4.2 Flowchart
A flowchart is a graphical representation of the major steps
in solving a task. It displays in separate boxes the essential steps of
the program and shows by means of arrows the direction of
information flow. The boxes, most often referred to as illustrative
symbols, may represent documents, machines or actions taken
during the process. A flowchart can also be said to be a graphical
representation of an algorithm, that is, it is a visual picture which
gives the steps of an algorithm and also the flow of control
between the various steps.
Symbol Function
Used to show the START or STOP. May
show exit to a closed subordinate.
INPUT U, V, W
*
RESULT U + (V V) / W
PRINT RESULT
2. Suppose you are given N numbers. Prepare the algorithm
that adds up these numbers and find the average. Draw the
flowchart.
Solution in Algorithm
i. Start
ii. Set up a Counter which counts the number of times the
loop is executed. Initialize Counter to 1. Counter← 1
iii. Initialize sum to zero. Sum← 0
iv. Input value and add to sum. Input value; Sum← Sum+value
v. Increment the counter. counter← counter+1
vi. Check how many times you have added up the number, if it
is not up to the required number of times, to step iv.
(if counter <N, then go to step iv)
vii. Compute the average of the numbers. Avg← Sum/N
viii. Print the average.
ix. Stop.
For Flowchart, see Fig. 5.6
5.4.7 Pseudocodes
A pseudocode is a program design aid that serves the
function of a flowchart in expressing the detailed logic of a
program. Sometimes a program flowchart might be inadequate for
expressing the control flow and logic of a program. By using
pseudo codes, program algorithms can be expressed as English-
language statements. These statements can be used both as a
guide when coding the program in a specific language and as
documentation for review by others. Because there is no rigid rule
for constructing pseudo codes, the logic of the program can be
expressed in a manner that does not conform to any particular
programming language. A series of structured words is used to
express the major program functions. These structured words are
the basis for writing programs using a technical term called
structured programming.
Example
Construct a pseudocode for the problem in the example above.
BEGIN
STORE 0 TO SUM
STORE 1 TO COUNT
DO WHILE COUNT not greater than 10
ADD COUNT to SUM
INCREMENT COUNT by 1
ENDWILE
END
START
SUPPLY N
COUNTER 1
SUM 0
INPUT VALUE
IF Yes
COUNTER<20
?
No
AVG SUM/20
PRINT AVG
STOP
START
Read number
of workers N
COUNTER 1
Fig. 5.7 Flowchart for Exercise 3