Programming Concepts (1)
Programming Concepts (1)
A Computer Programmer is
is a person who translates the task you want a computer to do
into a form that a computer can understand
implementation phase
implement the program in some programming language (Pascal,
Basic, C)
FORTRAN
FORmula TRANslator introduced in 1957
for use by scientists, engineers and mathematicians
well suited for complex numeric calculations
COBOL
COmmon Business Oriented Language
a programming language used for business data processing
designed to handle large data files
PL/I
Programming Language One created in 1960
general purpose language for powerful computations and
sophisticated data structures
today largely used in the oil industry
BASIC
Beginners Allpurpose Symbolic Instruction Code created in
1960
easy to learn interactive language on a time-sharing computer
PASCAL
Named after Blaise Pascal and created in 1960
suited to both scientific and file processing applications
designed to teach structured programming and top-down
design
C
Developed at Bell Labs in 1970s
used advantages of both high-level and low-level languages
C gives programmers extensive control over computer
hardware
incorporates sophisticated control and data structures
C++ is the object oriented version of C
LISP
is a language that processes symbol sequences (lists) rather
than numbers
designed to handle data strings more efficiently than others
Prolog
is a language for symbol processing
Logo
is an interactive education oriented language
designed to teach inexperienced users logic and programming
techniques
includes list processing capabilities
employs a triangular object called turtle
to draw, animate and color images very simply
Object-Oriented Programming Languages (OOPL)
there are two main parts of a program, instructions and data
traditional prog. languages treat instructions and data as
separate entities
an OOPL treats a program as a series of objects and
messages
an object is a combination of data and instructions that work
on data
and is stored together as a reusable unit
messages are instructions sent between objects
to qualify as an OOPL a language must incorporate concepts of
1. encapsulation
2. inheritance
3. polymorphism
encapsulation is
combining data and instructions into a reusable structures
encapsulation generates prefabricated components of a program
inheritance
is the ability of a programming language to define a new object
that has all the attributes of an existing object
programmer inherits the existing object
writes the code that describes how the new object differs from the
existing one
polymorphism is
the ability of an object to respond to a message in many ways
for example, say you have a circle object and a square object
each object has the same characteristics of drawing
when a drawing message is sent to a circle object it draws a circle
when a drawing message is sent to a square object it draws a
square
thus, each object has the same characteristics of drawing
but the characteristics is implemented differently
Advantages of OOPL
code re-use rather than reinvention and adaptable code
these speed development & maintenance of applications and
reduce cost
Smaltalk, Objective-C and C++ are examples of OOPL
ALGORITHMS AND FLOWCHARTS
A typical programming task can be divided into 2 phases:
Problem solving phase
produce an ordered sequence of steps that describe solution of
problem
this sequence of steps is called an algorithm
Examples of Algorithm: a recipe, to assemble a brand new computer
...
what else?
Implementation phase
implement the program in some programming language
Solution:
Pseudocode: Input a set of 4 marks
Calculate their average by summing and dividing by
4
if average is below 50
Print “FAIL”
else
Print “PASS”
A Flowchart
shows logic of an algorithm
emphasises individual steps and their interconnections
e.g. control flow from one action to the next
Flowchart: START
TRACE TABLE:
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2: Write an algorithm and draw a flowchart to convert
the length in feet to centimeter.
Flowchart:
START
Input
Lft
Lcm = Lft x 30
Print
Lcm
STOP
Example 3: Write an algorithm and draw a flowchart that will read
the two sides of a rectangle and calculate its area.
Flowchart:
START
Input
W, L
A=LxW
Print
A
STOP
Example 4: Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation ax2 + bx + c = 0
Hint: d = sqrt (b2 – 4ac), and the roots are: x1 = (–b + d)/2a and
x2 = (–b – d)/2a
Flowchart:
START
Input
a, b, c
d = sqrt(b2 – 4 x a x c)
x1 = (–b + d) / (2 x a)
X2 = (–b – d) / (2 x a)
Print
x1 and x2
STOP
DECISION STRUCTURES
Y N
is
A>B
Figure 2:
Print A Print B
IF–THEN–ELSE STRUCTURE
The structure is as follows
If condition
then true alternative
else false alternative
endif
Here “>” is called the relational operator. Table 1 gives the possible
relational operators:
Relational Operators
Operator Meaning
> Greater than
< Less than
= Equal to
Greater than or
equal to
Less than or equal
to
Not equal to
Input
VALUE1,VALUE2
Y is N
VALUE1>VALUE2
Print
“The largest value is”, MAX
STOP
NESTED IFS
One of the alternatives within an IF–THEN–ELSE statement
may involve further IF–THEN–ELSE statement
Bonus Schedule
OVERTIME – Bonus Paid
(2/3)*ABSENT
>40 hours $50
>30 but 40 hours $40
>20 but 30 hours $30
>10 but 20 hours $20
10 hours $10