Introduction To C++ Computer Programming
Introduction To C++ Computer Programming
Definitions
Program
This is a complete set of step-by-step instructions that control and direct the computer hardware in
carrying out a given task. Tasks may vary from very simple e.g. computing surface area to
complex ones like statistical analysis.
Programs are usually written to solve user problems on a computer.
Software
This term refers to all programs together with their associated
Programming Language
This is a set of symbols and the rules that govern their rules that are employed in the construction
of a computer program.
Programmer
This is a person who is trained and/or specializes in the technique of creating, maintaining and
modifying computer programs.
Programming Languages
Programming languages provide the basic building block for all software. They are the means
by which people can tell the computer how to carry out a task.
A program can be written in a variety of programming languages. The languages can broadly be
classified into two categories:
• Low-level language – which refers to the machine language and assembly language.
• High-Level languages: - which refers to languages such as COBOL, FORTRAN, BASIC
Advantages
• Program translation was fast because no translation was required.
• The program could directly address and control the internal circuitry meaning that these
programs were more effective in hardware usage and control.
SPH 409: COMPUTER PROGRAMMING
Disadvantages
• Writing programs was time consuming
• Tracing errors in a program was extremely difficult.
• Difficult to learn and use.
• Program modification was cumbersome.
• They were machine dependent i.e. a program created for one type of machine would not
work on a different type of machine.
• To write an effective program the programmer had to have expert knowledge on the
computer’s internal workings.
This language was more user oriented than machine language. Instructions are represented using
mnemonic code and symbolic addresses. Words like add, sum etc could be used in programs.
An assembler translated these codes into machine language.
Advantages
• Much easier to learn compared to machine language.
• Coding took less time than coding using machine language.
• Error correction was less cumbersome.
Disadvantages
• Were also machine specific
• Execution took longer than machine language programs due to the translation process.
These languages are user friendly and problem oriented compared to the low level languages.
Programs written in high level languages are shorter than the machine language programs.
They have an extensive vocabulary of words and symbols therefore program instructions are
written using familiar English-like statements and mathematical statements.
A single high-level language program is translated into multiple machine code instructions.
Advantages
• They are portable i.e. they can be used on more than one type of machine.
• Creating program and error correction takes less time.
• Are relatively easy to learn and use.
• The programmer does not have to be an expert on the internal workings of the machine.
Disadvantages
• Program execution takes more time due to the translation process.
• They do not address the internal circuitry of computers as effectively as the low level
languages.
• A translated program will occupy more space.
SPH 409: COMPUTER PROGRAMMING
Procedural Languages
They require the programmer to specify step-by-step how the computer will accomplish a
specific task. Program execution follows the exact sequence laid down by the programmer
during coding. Examples include FORTRAN, PASCAL, BASIC,
Non-Procedural Languages
They allow the programmer to specify the desired result without having to specify the detailed
procedure needed to achieve the result.
They are more user oriented and allow programmers to develop programs with fewer commands
compared with 3rd generation languages. They are called non procedural because programmers
can write programs that need only tell the computer what they want done, not all the procedures
of doing it.
An object can be considered a "thing" that can perform a set of related activities. The set of
activities that the object performs defines the object's behavior. For example, the hand can grip
something or a Student (object) can give the name or address.
Definitions:
Syntax
These are the rules of a language that govern the ways in which words, symbols, expressions and
statements may be formed and combined in that language.
SPH 409: COMPUTER PROGRAMMING
Semantics
These are the rules of language that govern meaning of statements in any language.
Language Translators
These are programs that translate programs written in a language other than machine language
into machine language programs for execution. Programs written in assembly or high level
languages are called source programs or source code before they undergo translation. After the
translation the machine language version of the same program is called object program or object
code. Language translators can be classified into
• Assemblers
• Compilers
• Interpreters
Assembler
This is a program that translates a source program written in assembly language into its
equivalent machine code (object code).
Compiler
During compilation both the high level source program and the compiler are loaded into the
RAM. The compiler reads through the source program statement by statement, converting each
correct statement into multiple machine code instructions. The process continues until the
compiler has read through the entire source program or it encounters an error. An erroneous
statement is not translated but is placed on the source program error listing, which will be
displayed to the programmer at the end of the translation process. Where there are no errors the
compiler will generate a machine code object program which is stored for subsequent execution.
The first two stages of compilation are carried out by a part of the compiler known as the parser
and can be referred to as parsing.
SPH 409: COMPUTER PROGRAMMING
The optimized code is then used to generate the object program which is stored on media such as
disk to await subsequent execution.
Interpreter
It is similar to the compiler in that it translates high level language programs into machine code
for execution but no object code is generated. An interpreter translates the program one
statement at a time and if it is error free it executes the statement. This continues till the last
statement in the program has translated and executed. Thus an interpreter translates and executes
a statement before it goes to the next statement. When it encounters an error it will immediately
stop the execution of the program.
Program Development
2. Design program
The programmer decides how the program will go about its implementation, what should the
user interface be like, how should the program be organized, how to represent the data and what
methods to use during processing. At this point, you should also be thinking generally although
some of your decisions may be based on some general characteristics of the C language.
The compiler also checks errors in your program and reports the error to you for correction. The
object code can never be produced if the source code contains syntax errors.
Algorithms
An algorithm is a sequence of steps which results to a plan or strategy on how to go about
solving a problem. There are different ways of presenting an algorithm. Some common ways
include:-
• Pseudocode
• Flow charts
Pseudocode
This is a case where an algorithm is expressed in English like statements (descriptions). For
example the following pseudocode calculates the pay amount for five employees.
Start
Initialize counter to 1
Enter employee details
Computer pay amount
Print the pay amount
Increment counter by one
Check the value of the counter
If counter < 6
Loop to step 3
End
#
Combining all object code segments from different modules and libraries functions.
SPH 409: COMPUTER PROGRAMMING
Flow Chart
Start or end – They are used in a flow chart to mark the beginning and the end.
Decision – This symbol is used to indicate decision making and branching. The
criterion is shown inside the symbol and the lines (paths) out show the results.
Connectors – A small circle containing a number or letter that is used to split a large flow
chart into smaller parts.
Example
SPH 409: COMPUTER PROGRAMMING
Program Design
Modular Programming
This is a technique that involves breaking down the entire problem into smaller, more
manageable units.
Features
• Each module within the application carries out a singular task.
• Each module runs independently of the other modules.
• Since each module is independent, a breakdown in any module does not greatly affect the
running of the application.
• Debugging is easier since errors can be traces to individual modules.
Top-Down Approach
In this approach an outline program is designed first, showing the main tasks and components of
the program, and the order in which they are to be executed. Each main component is then
reduced to a number of smaller, simple and more manageable components and this process
continues at each level until there is sufficient detail to allow the coding stage to proceed.
The process of reducing components into sequences of smaller components is often referred to a
stepwise refinement and forms the basis of structured programming.
SPH 409: COMPUTER PROGRAMMING
Bottom-up Approach
In this design approach, the application is developed starting at the bottom of the hierarchy i.e.
the single task modules. As each category of programs is completed on the hierarchy, the
controlling program for that category is created.
Testing is part of the procedures that ensure that a program corresponds with original
specification and that it works in its intended environment. It is the process of running software
to find errors (or bugs), though it is possible that some errors will get through the testing process
without being found.
Types of Errors
There are three types of errors: Syntax, Semantic and Logic errors.
Syntax errors
They result from the incorrect use of the rules of programming. The compiler detects such errors
as soon as you start compiling. A program that has syntax errors can produce no results. You
should look for the error in the line suggested by the compiler. Syntax errors include;
Missing semi colon at the end of a statement e.g. Area = Base * Length
Use of an undeclared variable in an expression
Illegal declaration e.g. int x, int y, int z;
Use of a keyword in uppercase e.g. FLOAT, WHILE
Misspelling keywords e.g. init instead of int
Note:
The compiler may suggest that other program line statements have errors when they may not.
This will be the case when a syntax error in one line affects directly the execution of other
statements, for example multiple use of an undeclared variable. Declaring the variable will
remove all the errors in the other statements.
Logic Errors
These occur from the incorrect use of control structures, incorrect calculation, or omission of a
procedure. Examples include: An indefinite loop in a program, generation of negative values
instead of positive values. The compiler will not detect such errors since it has no way of
knowing your intentions. The programmer must try to run the program so that he/she can
compare the program’s results with already known results.
SPH 409: COMPUTER PROGRAMMING
Semantic errors
They are caused by illegal expressions that the computer cannot make meaning of. Usually no
results will come out of them and the programmer will find it difficult to debug such errors.
Examples include a data overflow caused by an attempt to assign a value to a field or memory
space smaller than the value requires division by zero, e.t.c.