0% found this document useful (0 votes)
4 views

Introduction To C++ Computer Programming

The document provides an overview of computer programming, including definitions of key terms such as programs, software, and programming languages. It categorizes programming languages into low-level and high-level languages, detailing their advantages and disadvantages, and discusses various programming paradigms like procedural, non-procedural, and object-oriented programming. Additionally, it outlines the steps in program development, design tools, and error types encountered during testing and debugging.

Uploaded by

Peterson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Introduction To C++ Computer Programming

The document provides an overview of computer programming, including definitions of key terms such as programs, software, and programming languages. It categorizes programming languages into low-level and high-level languages, detailing their advantages and disadvantages, and discusses various programming paradigms like procedural, non-procedural, and object-oriented programming. Additionally, it outlines the steps in program development, design tools, and error types encountered during testing and debugging.

Uploaded by

Peterson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SPH 409: COMPUTER PROGRAMMING

Lecture 1: Introduction to 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

Low Level Languages

Machine Language (First Generation Language)


Digital computers represent and process data and instructions as binary numbers. This
representation of instructions and data is called machine language. Program instructions were
written as a series of binary digits (0’s and 1’s). When the program was entered into the
computer execution was direct since machine language needs no translation. The binary
combination allowed the program to have full access to and control the computers internal
circuitry and memory addresses.

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.

Assembly Language (Second Generation)

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.

High Level Languages

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

High level languages can further be classified into :


• Procedural languages (Third Generation)
• Non-Procedural Languages (Fourth Generation Languages or 4GLs)

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.

4GL consists of:


▪ Report Generators: also called report writers. This is a program for end users that is used
to produce reports.
▪ Query Language: This is an easy to use language for retrieving data from a database
management system.
▪ Application Generators: This is a program’s tool that allows a person to give a detailed
explanation of what data to be processed. The software then generates codes needed to
create a program to perform the tasks.

Object Oriented Programming Languages

OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented


Programming (OOP) uses a different set of programming languages than old procedural
programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable
"objects". Hence, you gain re-usability by means of four main object-oriented programming
concepts.

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.

Compilation can be divided into three stages including

Lexical Analysis which involves

• Checking for valid words like data names or operator symbols


• Checking for reserved words (keywords). These are words that have a special meaning
for the compiler. These are then replaced with non-alphanumeric character codes known
as tokens.

Syntax analysis which involves

• Checking the program statements for correct grammatical form.


• Breaking down the complex statements in the program into simpler equivalents and more
manageable forms.

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

Code Generation involves


• Translation of each statement into its equivalent machine code. This is done using tables.
• Setting up linkages that allow the object program to communicate or work with various
operating systems and hardware.
• Fetching of subroutines from the system library, to optimize the code and make it execute
faster and use less storage space.

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

Steps in Program Development

1. Design program objectives


It involves forming a clear idea in terms of the information you want to include in the program,
computations needed and the output. At this stage, the programmer should think in general
terms, not in terms of some specific computer language.

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.

3. Write the Code


It is the design Implementation by writing the (C) code i.e. translating your program design into
C language instructions. You will use C’s text editor or any other editor such as notepad. Using
another editor such as notepad requires you to save your program with the extension .c.

4. Compile the code


A compiler converts the source code into object code. Object code are instructions in machine
language. Computers have different machine languages. C compilers also incorporate code for C
libraries into the final program. The libraries contain standard routines. The end result is an
executable file that the computer can understand.
SPH 409: COMPUTER PROGRAMMING

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.

5. Run the program


This is the actual execution of the final code, usually preceded by linking #. Once the executable
code is complete and working, it can be invoked in future by typing its name in a ‘run’
command.

6. Test the program


This involves checking whether the system does what it is supposed to do. Programs may have
bugs (errors). Debugging involves the finding and fixing of program mistakes.

7. Maintain and modify the program


Occasionally, changes become necessary to make to a given program. You may think of a better
way to do something in a program, a clever feature or you may want to adapt the program to run
in a different machine. These tasks are simplified greatly if you document the program clearly
and follow good program design practices.

Program Design Tools

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

This is a symbolic representation of an algorithm sequence. A flow chart uses predefined


symbols to represent the various actions in an algorithm. The arrangement of the symbols
indicates the flow of logic in the algorithm.

Flow chart symbols

Start or end – They are used in a flow chart to mark the beginning and the end.

Process – used to represent operations on data e.g. computations. Details are


written in a rectangular box.

Input/Output – Input/output operations are represented in a parallelogram.

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

Structured Program Design

Structuring a program breaks it down into understandable chunks. Structured programming is an


approach to writing programs that are easier to read, test, debug and modify. The approach
assists in the development of large programs through stepwise refinement and modularity.
Programs designed this way can be developed faster. When modules are used to develop large
programs, several programmers can work on different modules, thereby reducing program
development time.

In short, structured programming serves to increase programmer productivity, program reliability


(readability and execution time), program testing, program debugging and serviceability.

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.

Advantages of modular programming

• Modules can be reused thus saving development time.


• Testing of individual modules in isolation makes tracing mistakes easier.
• An amendment to a single module does not affect the rest of the program.
• Ability to create libraries of often used routines which are reliable and can go into other
programs.

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.

Program Coding – A readable program

• Data names – Meaningful names make a program code easier to read.


• Comments – Make the program more understandable.
• Indentation – Code should be laid out neatly, with proper use of indentation to reflect the
logic structure of the code.
• Modularization- makes the program easy to understand.

Testing and Debugging

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

Run Time Error


This is an error that occurs during the execution of a program. In contrast, compile-time errors
occur while a program is being compiled. Runtime errors indicate bugs in the program or
problems that the designers had anticipated but could do nothing about. For example, running out
of memory will often cause a runtime error.

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.

You might also like