SKAA
SEAA3413
2413
Computer Programming
Introduction to Computer & Programming
Computer & How it Works
• An electronic device capable of performing computations and logical
decisions at speed millions/billions of times faster than human beings
can.
• Computer process data under the control sets of instructions called
computer programs.
• The process of writing the program is called programming and the
people who write programs are called computer programmers.
• The computer only does what we instruct it to do.
• Instructions and data are fed into the computer memory (input).
• Data is then processed by the computer (processing)
• The end result is obtained (output).
• The output is useful to the user.
input process output
Computer Components & Basic Operations
• Computer consists of two parts:-
Hardware
device/physical components in the computer system.
eg. CPU, memory, keyboard, monitor.
Software
program that direct the hardware to perform.
• Basic operations of computing
Input
Processing
Output
Storage
Software
Two types of Software:
1. Application Software
Software that has been developed to solve a particular
problem, to perform useful work on a specific task, or to
provide entertainment
Application software can be either;
a) Custom software is software designed and developed for a
particular purpose (already developed by someone, hire
someone to develop or develop yourself)
b) Package software is the kind of off-the-shelf program
developed for sale to the general public (eg. word
processing programs, spreadsheet programs and graphic
programs)
Software (cont’d)
2. System Software
Software that controls the allocation and usage of
hardware resources and enables the applications software
to run
System software consists of several programs, the most important
of which is the operating system
• The purpose of the operating system is to allow applications
to operate by standardising access to shared resources such
as disks and memory
• Examples of operating system are MS-DOS, Windows 95/98,
Windows NT, Windows XP, Windows Vista, Windows 8, Unix,
Linux and MacOS
Type of Computers
Computers are often classified into 5 sizes:
• Microcontrollers
also known as embedded computers, are the tiny, specialised
microprocessors installed in “smart” appliances and automobiles
• Microcomputers
small computers that can fit on or beside a desk or are portable
Personal computers
Workstation
• Minicomputers
• Mainframes
• Supercomputers
When To Use Computer to Solve Problem?
Not all problems are solved by computer
Computer solution is applied to problems when;
• It can save cost and time compared to manual system
• It is repetitive in nature
• Large amount of data to process and store
Program and Programming
A program is a sequence of instructions that can be
understood by computer to solve a particular problem
Programming refers to the process or steps of constructing
the instructions mentioned above.
• Programming requires a systematic planning to ensure time
efficiency and help to design error-free programs
• A systematic process of developing a program is called a
program development cycle (PDC)
Program Development Cycle
User request Analyse Problem
a change
Assemble Final
Document
Design Logic and
Test and Debug Test Plan
Code the Program
Program Development Cycle (cont’d)
1. Analyse: Define the problem
Make sure you understand the requirements, can identify the
input, processing and output.
2. Design: Plan the solution
Find the logical sequence (algorithm). Every detail should appear
in the algorithm. Use flowcharts and/or pseudocode. Include test
plan with input and anticipated results.
3. Code: Translate the algorithm into programming language
The programmer uses the algorithm devised in step 2 along with
the knowledge of computer language to write and enter program
into the computer.
Program Development Cycle (cont’d)
5. Test and debug: Locate and remove errors
An error in a program is called a bug. Testing is the process of
finding error, and debugging is the process of correcting the errors.
Errors are due to typing mistakes, flaws in algorithm, incorrect
usages of computer language rules.
After all syntax errors have been corrected, use the test plan
created during design stage to check whether the program works.
If the results do not match, the program needs further debugging
until everything is OK.
6. Complete the documentation: Organise all the material that
describes the program
Documentation is intended to allow another person, at a later date,
to understand the program. For commercial programs,
documentation includes an instruction manual and on-line help.
Programming Logic Design
• Before writing a program, it is essential to understand the problem and
carefully plan approach to solve the problem.
• Computing problems can be solved by executing a series of actions in a
specific order; it is called an algorithm.
• Explain it in simple steps before writing a program to accomplish the
task.
Eg. “Calculate bending moment at a point” algorithm.
What will happen
1. Get the loadings and distances.
if the order is
2. Calculate reactions.
changed?
3. Determine BM at the specified point.
4. Display the value.
Eg. “Calculating the bending moment at a point of a beam” algorithm.
1. Get the beam configuration and the load system .
2. Calculate the support reactions .
3. Calculate the BM at the specified point .
Programming Logic Design (cont’d)
• Indicating appropriate sequence is crucial in computer programs.
The task of ordering a program statements correctly is called
program control. (We will learn Control Structures later)
• Three programming design tools are used for developing and
representing algorithms:
1. Flowcharts
User Analyse Problem
2. Pseudocode request
a change
Assemble Final
Document
Design Logic and
Test and Debug Test Plan
Code the Program
Programming Logic Design Tools
1. Flowcharts
Consist of special geometric symbols connected by arrows.
Within each symbol is a phrase representing the activity at
that step. The shape of the symbol indicates the type of
operation.
Advantage
It provides a pictorial representation of the task, which makes
the logic easier to follow. We can clearly see every step and
how each is connected to the next.
Disadvantage
When the program is very large, it may continue for many
pages, making difficult to follow and modify.
Programming Logic Design Tools (cont’d)
Flowchart Symbols
Terminal: Decision:
represent start used for logic/ comparison
or end of a task operations. One entry &
Processing: two exit paths (Yes/No).
for arithmetic & data-
manipulation operations.
Instructions are listed inside Connector:
the symbol. joins different flowlines
Input/Output: Offpage connector: Used
for input/output operations; to indicate that the
ex. Reading & Printing. flowchart continues to
Data to be read/printed the next page
are described inside.
Flowlines: connect
symbols & indicate
Document: the flow of logic
Print report
Predefined Process: Annotation:
represent a group used to provide additional
of statements that perform information about another
one processing task flowchart symbols
Programming Logic Design Tools (cont’d)
Flowchart example – Passing a traffic light
Start
No
red? condition
No Yes
yellow?
Stop
Continue because Yes
it must be green Prepare
to stop
A
Programming Logic Design Tools (cont’d)
Flowchart example – Calculate average mark
Start
N=0 A
Initialisation
Sum=0
Ave =Sum/N
Input x Read mark
Display average
N=N+1, no
x=0 Sentinel value
Sum=Sum + x
End
yes
Modify the flow chart to ignore negative marks
Programming Logic Design Tools (cont’d)
Pseudo Code
• Plain language is used to outline the process. It is sort of the
abbreviated version of the actual program.
• It allows the programmer to focus on the steps required to solve the
problem rather than on how to use the computer language.
• Pseudo code consists of only executable statements
• It is compact compared to flowchart and preferred by many
programmers.
Benefit
• It enables the programmer to concentrate on the algorithm without
worrying about all the syntactic details of a particular programming
language
Pseudo code examples
If the light is red, stop your car. Get the marks of every subject
If it is yellow, prepare to stop. Sum up the marks
Otherwise continue driving Determine the no. of subjects
Divide total mark by no. of subjects
Control Structures
• Statements in a program are normally executed in the
order in which they are written.
• But programmer can also specify that the next statement
to be executed other than the next one in sequence. This
is called transfer of control.
• Programs could be written in terms of three control
structures;
1. Sequential
2. Selection (conditional)
3. Repetition (iterative)
Control Structures (cont’d)
1. Sequential structure
Statements are executed one after the other in the order
in which they are written.
Eg: Calculating Supports reaction of a SS beam carrying a point load.
Start
Input L, P, a
Input L, P, a
Rb= Pa/L Calc. Ra & Rb
Ra=P-Rb Display result
Display Ra, Rb
End
Control Structures (cont’d)
2. Selection structure
One group of instructions is executed when if the specified
condition is fulfilled.
Eg. 1: Mathematics competency test.
No
Mark <50?
Yes
Extra Class
Sit for Qualifying test
If mark < 50 then
attend Extra class before Maths 1 course Maths 1
else take Maths 1 straight away
Flowchart shows clearly how
control structure operates
Eg. 2: Bending moment at a specified point, x.
P
0< x ≤ L
a
Start
L
Read L,a,P,x
Calculate
RA & R B
Read input L, a, P, x
Yes No
Calculate RA and RB Mx=RAx x < a? Mx=RAx – P(x-a)
If x<a, Mx = RAx
Else Mx=RAx-P(x-a)
Display Mx
End
Control Structures (cont’d)
3. Repetitive structure
Allow instructions to be repeated based on the truth or
falsity of some condition.
Eg: Driving license test sequence at RTD.
Sit the Written test
Written Test If fail the test , repeat it
otherwise proceed with on-the-road test
No
Passed written test?
Yes
On-the-Road test
Ex. Determining the number of reinforcement bars.
Input Ast, D
Input req’d area and bar size
Abar=3.142D2/4 Calc area of one bar
N= 0
Add up the bar until Aact > Ast
Display no. of bars, area.
N=N+1
Aact=Abar*N
No
Aact >Ast
Yes
Display N, Aact
Coding
• After the design stage has been completed and appropriate
interface have been chosen, the programmer then
translate the algorithm into a programming language, i.e.
coding.
• Coding a program means to translate the logic into
statements of a particular language (C++, Pascal, etc.) and
enter them into a computer.
• The programming language statements that are typed into
a computer program is called source code.
Programming Language
Five generations of programming languages
• First Generation: Machine Language
Basic language of the computer, representing data as 1s and 0s
Corresponds to on and off electrical states of computer
Not convenient to read and use
• Second Generation: Assembly Language
Low-level programming language that allows a programmer to
write a program using abbreviations or more easily
remembered words instead of numbers
Requires a language translator to translate into machine
language (Assembler)
Programming Language (cont’d)
• Third Generation: High-Level Languages
Resembles some human language such as English
Allows user to write with a familiar notation rather than numbers or
abbreviations
The translator is, depending on the language, either a compiler or
interpreter
Examples are FORTRAN, BASIC, PASCAL and C
• Fourth Generation: Very-High-Level Languages
Often called as 4GL
Much more user oriented and allow users to develop programs with
fewer commands compared with third generation languages
An example is SQL (Structured Query Language), used to retrieve
data from a database management system
Programming Language (cont’d)
• Fifth Generation: Natural Languages
Use human language to give people a more natural connection
with computers
Natural language are part of the field of study known as artificial
intelligent (AI), i.e. a group of related technologies that attempt
to develop machines to emulate human-like qualities, such as
learning, reasoning, communicating, seeing and hearing.
History