Programming language review
Programmers write instructions in various programming languages
Some directly understandable by computers and
Others requiring intermediate translation steps
Three types of programming languages
1. Machine languages
computer can directly understand only its own machine
language
also called machine code
defined by its hardware architecture
generally consist of numbers 1s and 0s
are cumbersome for humans
Example:
1110110100
1
1111100000
Programming language review cont…
Three types of programming languages
1. Machine languages cont…
2
Programming language review cont…
2. Assembly languages
• English-like abbreviations representing elementary
computer operations
• translated via assemblers: program translate symbols to
machine code
• Example:
LOAD BASEPAY // copy data to BASEPAY
ADD OVERPAY // add value to OVERPAY
STORE GROSSPAY
3
Programming language review cont…
3. High-level languages
To speed up the programming process further, high-level
languages were developed
single statements could be written to accomplish
substantial tasks
Codes similar to everyday English
Use mathematical notations
grossPay = basePay + overTimePay
m= x +y
translated via compilers:
• program translate high-level code to machine code
4
Programming language review cont…
3. High-level languages continued
• Relatively slow: due to compilation time .
• Interpreter programs
– were developed to execute high-level language
programs directly (without the need for compilation),
– more slowly than compiled programs
– Scripting languages such as the popular web languages
JavaScript and PHP are processed by interpreters
5
Programming language review cont…
3. High-level languages continued
Examples :
» Ada
• In 1970s
• Used in U.S Defense
• Named after Ada Lovelace
» BASIC
• Beginners All-purpose Symbolic Instruction Code
• is a general-purpose language
• early 1960s
» FORTRAN
• FORmula TRANslator
• The first high-level programming language
• In 1950s
• for performing complex mathematical calculations. 6
Programming language review cont…
3. High-level languages continued
Examples :
» COBOL
• Common Business-Oriented Language
• in the 1950s,
• designed for business applications
» Pascal
• in 1970,
• designed for teaching programming.
• named in honor of the mathematician, physicist, and
philosopher Blaise Pascal.
7
Programming language review cont…
C#
3. High-level languages continued
Examples : • Pronounced “c sharp.”
C and C++ • was created by Microsoft
• pronounced “c plus plus” • around 2000
• for developing applications
• are powerful,
based on the Microsoft .NET
• general-purpose platform.
languages Java
• developed at Bell • was created by Sun
Laboratories. Microsystems
• The C language was • in the early 1990s
created in 1972 • For single computer and
• the C++ language was web based applications
created in 1983. 8
Problem solving
Steps in program development
1. State the problem clearly- a problem cannot be solved correctly unless it
is being understood.
2. Plan and Write the Logical Order of Instructions - the computer follows
the direction exactly at the given sequence.
3. Code the Program - write the programming statements in the desired
language.
4. Enter the program into the computer - key in or type the statement into
the computer.
5. Run and Debug the program - check if you have the desired output; if not,
trace the possible error.
9
Problem solving cont…
State problem clearly
Stating steps to come to a particular solution
of a given problem.
Problem solving techniques
Analyze the problem
Outline the problem requirements
Design steps (algorithm) to solve the problem
10
Problem solving cont…
Analyze the problem
Thoroughly understand the problem
Understand problem requirements
Does program require user interaction?// input from user
Does program manipulate data? // any data process
What is the output? // result to user
If the problem is complex, divide it into sub
problems
Analyze each sub problem as above
11
Problem solving cont…
Working backwards
Reverse-engineer
Once you know it can be done, it is much easier to do
Look for a related problem that has been solved before
Stepwise Refinement
Break the problem into several sub-problems
Solve each sub-problem separately
Produces a modular structure
K.I.S.S. = Keep It Simple Stupid!
12
Problem solving cont…
Example #1. Calculate area of a rectangle
Understand problem requirements
Is the problem statement complete and unambiguous?
No
Rectangle information.
Does program require user interaction? Yes
Input the length and width
Does program manipulate data? yes
Product of length and width
What is the output? Area
Is there sub-problem? No
13
Problem solving cont…
Exercise
Make problem analysis for the following problems
#1 calculate sum of two integers number
#2 Find perimeter of a rectangle
# 3 calculate average of student marks
#4 to calculate student grade based on marks
– ‘A’ mark >= 85%
– ‘B 85<mark<=75
– ‘C’ 75<mark<=65
– ‘F’ mark<65
14
Algorithm and pseudo code
• Algorithm
– A list of instructions, procedures or formulas to solve a
given problem
– The word derives from the name of the mathematician,
Mohammed ibn-Musa alKhwarizmi (El-Harezmî),
(780 – 850).
– A program is one type of algorithm
• All programs are algorithms
• Not all algorithms are programs!
– Directions to somebody’s house is an algorithm
– The steps to compute the cosine of αo is an algorithm
15
Algorithm and pseudo code cont…
• Pseudo code
– is a computer programming language that resembles
plain English
– cannot be compiled or executed, but explains a
resolution to a problem.
Example :
#1. Write pseudo code and an algorithm for a
program to solve area of a rectangle; where the
user has to provide the length and width of the
rectangle.
16
Algorithm cont…
• Pseudo code
– Input two dimensions, length and width
– Calculate the area from product of length and
width
– Display the area, output
• Algorithm
Step 1: Input L,W
Step 2: A L*W
Step 3: Print A
17
Algorithm and pseudo code cont…
Exercise
#1. Write an algorithm for a program to compare three integer
numbers;X1, X2, X3 and display the largest.
o Solution
o Pseudo code
• Input three numbers, x1,x2,x3
• Compare the largest of the number
• Display the largest number
• Algorithm
• Input x1,x2,x3
• If x1>x2
• If x1> x3
• Display x1
• Else display x3
• Else
• if x2 > x3
• Display x2
• Else display x3 18
Algorithm and pseudo code cont…
• Algorithms generally share a set of properties:
– Input: what the algorithm takes in as input
– Output: what the algorithm produces as output
– Definiteness: the steps are defined precisely
– Correctness: should produce the correct output
– Finiteness: the steps required should be finite
– Effectiveness: each step must be able to be
performed in a finite amount of time
– Generality: the algorithm should be applicable to all
problems of a similar form
19
Flowchart
• Graphical representation of solution
• Standard symbols used so that all computer
programmer can understand it
• shows logic of an algorithm
• emphasizes individual steps and their
interconnections
• e.g. control flow from one action to the next
20
Flow chart cont…
Symbols and meaning
21
Flow chart cont…
Flowchart guide
1. The flowchart should flow from top to bottom
2. If the chart becomes complex, utilize connecting
blocks
3. Avoid intersecting flow lines
4. Use meaningful description in the symbol
22
Flowchart cont…
• Example #3: Draw flowchart for program to
solve area of a rectangle
• Solution:
Show beginning of the
Start code
Get input information,
L
length of the rectangle
Get input information,
W width of the rectangle
Make the calculation ,
A= L*W Area is equal to product of
length and width
Indicate end of the
End Program program.
23