1. Introduction and Program design
1. Introduction and Program design
Computer program – It refers to a series of coded instructions written and fed to the computer to help in solving
a particular problem.
Programming language – this refers to a set of rules, symbols and special words used to construct a computer
program. Programming language rules consist of the following:
Rules of Syntax, which specify how valid instructions, are written in the language.
Rules of Semantics, which determine the meaning of the instructions (what the computer will do).
Generally it is a notation for communicating to a computer what we want it to do.
LEVELS OF LANGUAGES
i). High Level Languages – Contain statements that are written in English words and symbols. Such languages
are not designed specifically for any brand of computer. High-level languages permit faster development of
large programs. The final program as executed by the computer is not as efficient, but the savings in
programmer time
Features
Provides notation to describe problem-solving strategies rather than organize data and instructions at
machine-level.
It requires a compiler or interpreter.
ii). Low Level Languages (Assembly Language) – Contain statements that are written using mnemonics
(codes suggesting their meaning and therefore easier to remember) to present operations and addresses that
relate to their main memory and storage registers of a computer.
Features
Uses Symbolic Names
Low-level programming wastes effort in coding a solution rather than solving a problem.
It is difficult to build and maintain large programs.
It requires assembler.
iii). Machine Languages – Is a machine oriented language in which instructions correspond or resemble a
computers own native machine code consisting of strings of 1’s and 0’s, stored as binary numbers. The
instructions are interpreted and executed directly by the computer. The main problems with using machine
code directly are that it is very easy to make a mistake, and very hard to find it once you realize the mistake
has been made.
PROGRAM PROCESSING
A program written in a high level language (source code) can only be run in its machine code equivalent
format. This is achieved by use of a computer program translator.
ii) Interpreter
This is a program that translates the statements in high-level language as the program is running. This means
they are translated into machine language and then executed as the program is running
Characteristics of Interpreters:
Reads the source code one line at a time performing the specific instructions contained in that line
Slower, but often easier to develop
Allows runtime Flexibility
More appropriate for use on the web
Good error messaging
iii) Compiler
This is a program that translates a high level language into machine oriented language. Once the program has
been compiled, the resulting machine code is saved separately, and can be run on its own at any time. As with
assembly-language programs, updating or correcting a compiled program requires that the original (source)
program be modified appropriately and then recompiled to form a new machine-language (object) program.
Sometimes the object code cannot be directly executed. Various library files must be “linked in” using another
special program called a linker, which produces executable code.
Roles of a Compiler
i). It converts the source code into object code (instructions in machine language).
ii). It checks the source code for syntax errors i.e. object code can never be produced if the source code
contains syntax errors
Characteristics of Compilers:
Reads the entire program and converts it into object code/ binary code/ machine code.
Runs faster
Typically has more functionality and is easier to optimize
Best choice for complex programs that need to be fast
Poor error messaging
Libraries
Libraries (in computer programming terms) contain chunks of precompiled (object) code for various functions
and procedures that come with a programming language that requires compilation, e.g. functions and
procedures to facilitate I/O.
PROGRAM BUGS
It refers to program errors. They are caused by three main types of errors, namely
Syntax errors
Logic errors
Semantic errors
Debugging Tools
To assist in error detection many debugging tools exist. Some of these allow the user to analyze the core dump
that occurs in the event of a fatal error. (A core dump describes the “state” of a program when a fatal error
occurs). Others allow programmers to step through and execute a program line by line to support analysis of its
execution.
STRUCTURED PROGRAMMING
This refers to a collection of programming techniques for implementing program and control abstractions in a
hierarchical manner. It emphasizes in programming using sequences, conditions and repetition (no jumps and
“goto” statements). Structured programming methodology enhances readability and hence maintainability.
ii). Top down Design. The top-down program-design technique assumes a modular program and organizes
these modules into a hierarchical structure. Top-down design is characterized by stepwise refinement and
delayed decision. An overview of a modular, hierarchical program is depicted in the figure below.
At any level in the hierarchy, the program can be regarded as complete and tested for correctness.
C Programming Environment
The C programming typically go through the following four phases:
Phase 1
Involves creation of a C program in text mode (source code)
Phase 2.
Involves translating of the program using a compiler
If the compiler flags an error in the program, then the programmer must return to phase one and amend the
lines of text that have errors.
Phase 3
Involves the linking/loading of the compiled program
Phase 4
Involves program execution
Machine code program
Data running in memory results.
The program might contain run time errors, in which case the operating system will terminate the program
prematurely. To modify the program, it will be necessary to repeat the 4 phases again.
ii) Outline the Solution: -This is where the programmer may decide to break the problem into smaller tasks
or steps. It usually takes the steps of hierarchy or structure chart.
v) Translate the design into a program: -This step involves translating the algorithm into a program using
an appropriate programming language
i). Flowchart
This refers to the graphical representation of an algorithm or a portion of it. It used to convey in diagrammatic
form, the logic, processing operations and flow of control required of a computer program
Flowcharts are used by programmers in two ways:
a) To plan the structure of the program before its written
b) To describe the structure of the program after its been written.
Flowcharts are made up of boxes of standard shapes linked to show the order of processing. Each box contains a
brief note stating what an application is when applicable and further explanation can be given.
The following are the standard set of symbols used to draw flowcharts
i). Start/End
Start
Used to indicate the start or end
End of a process
ii). Process
iii). Input/output
iv). Decision.
A A small circle containing a number or letter is used to split a large flowchart into
small parts.
A
a) Sequence:
Statement(s) -A
B Statement(s) -B
Statement(s) -C.
b) Iteration (Repetition)
True
True
Task(s)
True False
If condition is true
Condition Task statement(s) A
Else
Task statement(s)
Task(s) B
Task(s) A
False
Condition
If condition is true
True Task statements(s)
End if
Task(s)
False
True
1 False
False
True
2 FalseFals
e
Task (A)
True False
Task (B) 3 False
Task (C)
Condition
Case of condition
Case 1:
Task statement(s) A
Case 2:
Task statement(s) B
Case 3:
Task statement(s) C
………
………
End case
The advantage of flowchart is that the patterns of logic are easy to form.
The main disadvantage is that it allows the designer of an algorithm to develop structures that don’t conform to
the 3 control constructs which could lead to inelegant and dangerous implementation in whatever programming
language used i.e. they are incapable of being expressed in structured syntax in any programming language and
would lead to excessive use of goto’s and their associated labels.
Start
Input
Number
True
False
Number < 0
True False
Print
Negative Number > 0
Print Zero
Print Positive
End
Example
Develop a pseudocode algorithm for a program that accepts each of the average marks of 10 students in a class
and computes the sum and the average mark of the class. The program should display the average mark of the
class.
Begin
Set total to zero
Set counter to one
While counter is less than or equal to ten
Input student average mark
Add average mark to total
Add one to the counter
Set the class average to total divided by ten
Display the class average
End
Counter 1 2 3 4 5 6
Total 0 70 125 200 260 300
Average mark 70 55 75 60 40
Class average 60
Exercise
Develop a pseudocode algorithm for a program that accepts each of the average marks of students in a class and
computes the sum and the average mark of the class. The program should display the average mark of the class.
Desk Checking.
It involves tracing through the logic of the algorithm with some test data. It follows the following six simple
steps.
i) Choose s simple input test cases that are valid. Two or three test cases are usually efficient.
ii) Establish what the expected result should be for each the case data.
iii) Make a table of the relevant variable names within the algorithm on the paper.