chapter 1
DESCRIPTION
computerTRANSCRIPT
2/17/2011
1
1.1 Introduction concepts
1.0 Introduction To Programming
1.1.1 Term
programme
programmerProgramming
language
Program: A set of step-by-step instructions that directs a computer to
perform a specific task and to produce the required results.
Programming:
Programming is a process of designing/ creating a program.
Programmer:
Programmer is a person who writes the program.
c) A programming language
is set of instructions that can be used to
construct a program. A notation for
writing programs, which are specifications
of a computation or algorithm.
is a language designed to describe a set of
consecutive actions to be executed by a
computer. A programming language is
therefore a practical way for us (humans)
to give instructions to a computer.
2/17/2011
2
1.1.2 Various types of Programming
Languages There are three basic types of
programming languages.
1.1.3 (i) Machine language
The lowest-level programming language
Machine languages are the only languages understood by computers.
While easily understood by computers, machine languages are almost impossible for humans to use because they consist entirely of numbers.
Expressed in binary form, an instruction might be 0111010110110011. Very difficult to use. Lowest level of computer languages.
For example, a program written in Apple PC cannot be run in IBM PC
ii) Assembly language
Second level of language.An assembler
translates assembly language.
It was developed to replace "0" and "1" used in Machine Language.
Machine languages consist entirely of numbers and are almost impossible for humans to read and write.Takes a long time to write programs
English-like abbreviations representing
elementary computer
operations (translated via assemblers)
Example: C++, C , FORTH
2/17/2011
3
iii) High-level language
Instruction is written as a series of English-like words.
Translator (Compiler/Interpreter) is needed to translate high level language to machine language.
It is Machine-Independent. The program can be written and executed on any computer.
E.g. Fortran, COBOL, SQL, PROLOG, C
1.1.4 Structured Programming
Languages
Definition of Structured Programming
Structured programming can be defined as a Software application programming technique that follows a top down design approach with block oriented structures.
It support loop such as while, do-while and for.
Structured programming
The most popular structured programming languages include C,C++, Ada, and Pascal.
Codes similar to everyday English
Use mathematical notations
Example: if (x < 10) {
printf(“too low, ry again\n”);
Scanf(“%d”,&x);
}
2/17/2011
4
b) Modular programming
Programming paradigm based on data types.
An object stores a data type value; variable name refers to object.
A programming style that brakes down program functions into modules, each of which accomplishes one function and contains all the source code and variables needed to accomplish that function.
Modular programming is a solution to the problem of very large programs that are difficult to debug and maintain. By segmenting the program into modules that perform clearly defined functions, you can determine the source of program erros more easly.
c. Object-Oriented Programming
Languages
is about relation between the data and the code operating on the data (or data operating on the code).
is a programming paradigm that uses "objects" –data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance. Many modern programming languages now support OOP
is one the newest and most powerful paradigms. In object- oriented programs, the designer specifies both the data structures and the types of operations that can be applied to those data structures.
This pairing of a piece of data with the operations that can be performed on it is known as an object. A program thus becomes a collection of cooperating objects, rather than a list of instructions.
Objects can store state information and interact with other objects, but generally each object has a distinct, limited role.
2/17/2011
5
An object stores a data type value;
variable name refers to object.
Object-oriented programming (OOP) is a
programming paradigm based on data
types.
The most popular object-oriented
programming languages include Java, Visual
Basic, C#, C++, and Python.
Object-orientated programming
languages, such as SmallTalk and
HyperTalk, incorporate modular
programming principles
1.1.5 Comparison between the
following types of programming:
a) Structured programing
The main advantage for structured
programing is that it make input easy, and
also it is better to use because it does not
merges the data we stored in strings and
variables, sometime it happens that we get
merged data if we don't use structured data.
And it also make easy to manage variables.
Structured programming is as
follows:
--Program startvarvarvar
function { ... }function { ... }function { ... }
main { ... }--- Program End
You have units of code, which operate on variables, and are called in reference to those variables, to follow a structure acting on those variables.
2/17/2011
6
b) Modular programming
-A detailed process of problem solving
-Break a large problem into several
smaller
Sub-sections are solved separately by
observing that all these small modules to
solve the whole problem of the origin
Modular Programming - Need
and Approach
Typical industry programming projects
consist of thousands of lines of code or
more
Analogous approaches: manufacturing a
spacecraft, or a car Manufacture individual
components, test separately
Assemble into larger components,
integrate and test
Modular Programming -
Process
Advantages of Modular
Programming
Manageable: Reduce problem to smaller, simpler, humanly comprehensible problems
Divisible: Modules can be assigned to different teams/programmers
Enables parallel work, reducing program development time
Facilitates programming, debugging, testing, maintenance
Portable: Individual modules can be modified to run on other platforms
• Re-usable: Modules can be re-used within a program and acrossprograms
2/17/2011
7
An Example of Code that Doesn't Use
Modular Programming
A simple example of repeated code can be seen in:
PI = 3.14159265358979
R = 1
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
R = 2
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
R = 3
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
R = 4
C = 2 * PI * R
A = PI * R * R
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
Here the same functionality is repeated but does works (as can be seen in f igure 1). However, the programmer can work more ef f iciently by using modular programming.
A Simple Example of Modular
ProgrammingThe aim of the programmer is now to ensure that code is reused as much as possible. They do this by identifying the code that can be place
separate functions and subroutines:
PI = 3.14159265358979
function circumference (R)
circumference = 2 * PI * R
end Function
function area (R)
area = PI * R * R
end function
sub print_details (R)
C = circumference (R)
A = area (R)
wscript.echo "Radius " & R & " Circumference " & C & " Area " & A
end sub
print_details 1
print_details 2
print_details 3
print_details 4
The output is the same as before (as can be seen in figure 2), but this time the chance of the operation of the script being altered due to a typing error is greatly reduced, and if there is an error then the task of correcting it is made much simpler. The programmer can also use the functionality throughout their application without worrying about having to rewrite the code, thereby improving the e ff iciency of both the code and their time.
c)Object oriented
- fourth-generation language is object
oriented.
- Programmers will more tend to think of
the objects involved
the problem and the relationships
between objects.
-Examples of programming languages are
C + + Java
Why Object Oriented Language?
Reusability-Cepatkan project
- Improve the quality of
- Easily adjusted
- Flexible
- Easy maintenance
- Reduce the risk for complex projects
2/17/2011
8
c) Object oriented
is as follows:
--- Program Startobject {varvarfunction { ... }function { ... }function { ... }}
varvar
function { ... }main { ... }--- Program end
Variables can be objects, which have their
own data and functions. Think like C and
structures, except structures can have
functions "in them" which operate
specificly on their own data.
Example 1: Object-oriented
Problem Solving Approach
Step 1: Problem Identification - Compute
the sum of two numbers and print out
the result.
Step 2: Object Identification
- Identify objects needed to solve the
problem.
Num1 - first number (object).
Num2 - second number (object).
Sum - result of the addition of the two
numbers (object).
2/17/2011
9
Step 3: Message Identification
- Messages needed to send to objects.
+ aNum - This is the message sent to the receiver object with an parameter aNum. The result of this message is the value (a numeric object) of the total sum of the receiver object and aNum. print - a message that displays the value of the receiver object.
Step 4: Object-message sequences - Following is the object-message sequence to solve the problem.
(Num1 + Num2)print The message + with a parameter Num2 (an object), is sent to the object Num1.
The result of this is an object (Num1 + Num2), which is sent the message print. The parentheses are included to avoid any ambiguity as to which message should be activated first.
Note: The sequence of Smalltalk evaluation
is from left to right unless is specified by
parentheses ( ). For example:
2 + 3 * 4 is 20
while
2 +(3 * 4) is 14
1.2.1 Stages of Problem Solving
2/17/2011
10
The steps in programming:
1-Definitions and identify problems- Identify and understand the problems to
be solvedi. Read the questions and identify and
understand the requirements of the questionii. For the analysis to determine the
problem with three main areas:* Input required* OUTPUT issued* PROCESS determine the formula
to get the output from a given input
1.2.2 Elements of problem analysis
PROCESS
2 - Planning variables
variable is a reference to a memory
location
Variables with a specific type of data to
determine the memory size provided
All variables must be used in the program
prior diishtiharkan
3. Design a flow chart (design
software) Better known as the process of
designing algorithms
algorithm - list of measures to solve
problems
Created before the actual program coding
to make sure the solution method
correct the problem that is use
2/17/2011
11
There are two ways to write the
algorithm:
i. Pseudo code
-Steps problem solving
-Written to spoken language daily
Pseudo code
Example: Changing a bulb is burned
Start
Removing the bulb is burned
Replace with new bulb
End
Is it sufficient for the
implementation of the computer?
Example: Changing a bulb is burned (details)
StartPlace the ladder in the position of the bulb burnsChoose a suitable lampTake the stairs up to the light bulb that burnsTurn the bulb counterclockwise and take out the bulbFit the new bulb in place properTurn the bulb clockwiseDown the stairsPlace the back stairsEnd
Contoh:Mengira harga
bayarbagi buku
2/17/2011
12
4-Writing program
- Represents the implementation phase in which the code was written and typed into the computer-The program should follow the syntax correctly written on the appropriate programming language- Some programming languages: BASIC, COBOL, FORTRAN, C + +, Visual BASIC and other.
Problems ---- Pseudo Code /flow chart -----program
5-Testing & debugging program
-The program that has been built to test / run (run) to ensureoutput produced is correct and meet the needs of users- Menyahsilap (debugging) --- the program is tested with real data-There is a possibility of erroneous output (error) could result from a
program written
The types of programming errors
i) Syntax Error / encoding
when programmers fail to follow the rules governing how the instruction should be written.
Example: wrong spelling of the WRITE command. The computer will print a diagnostic message.
After all syntax errors corrected or removed compiler can understand and translate the program.
ii) Logic errors
error occurred from instruction sequences that are not correct.
It might be the error in logic programming.
Instruction means may be in conflict.
Example: the programmer should write direction READ but it write WRITE
iii. Time Error
Due to data entry is not in accordance
with the instructions written in
programming
2/17/2011
13
6-Documentation Program
If the output properly certified and meet
consumer needs
- at this stage is to prepare a report for
reference and for the updates in the
future
- Facilitate the programmer to understand
the design and programmable logic
The documentation includes:
Type of problem or the specific requirements
of the appropriate
Description of inputs, outputs, constraints and
formulas for problem
A tool used logic---- flow chart or pseudo
code
Example output of the program was
implemented (executed) using the test data
The steps or guidelines for using the program
1.2.4 Determine input, process and
output for a given problem
Understand, identify and define problems.
Determine the input requirements,
process and output (IPO)
Provide IPO
Problem analysisProblem 1
Create a program that will accept three numbers as input from the user. Find the average number and the third show -the three numbers together with the average value.
Analysis of problemsInput - 3 numbersProcess - 1. Add 3 numbers
2. 3 Divide the total number to 3Output - 3 and the average number
2/17/2011
14
answers Problem 2
Given the value of x = 10 and the value of
a = 12, find the equation of state revenue
under
y = 2x + a -6
Problem analysis
1) Input:
the value of x = 10
and the value of a = 12
2) the formula / process:
y = 2x + a - 6
3) output:
the value of y
1.2.5 DESIGN TOOL
It is a framework or flow that shows the
steps in problem solving.
Methods to design a program:
Flowchart
Pseudocode
IPO Chart(input-process-output
chart)
Structure Chart
2/17/2011
15
a) Flowchart
A graphical representation of data,
information and workflow using certain
symbols that are connected to flow lines
to describe the instructions done in
problem solving.
It shows the flow of the process from
the start to the end of the problem
solving
Explanation of basic
flowchart symbol:
2/17/2011
16
Cont…Explanation of basic
flowchart symbol:
There are few additional symbols of flowchart
used in a complicated program: GOSUB and
RETURN. It’s usually used in a big program.
Example Flowchart:
Flowchart to calculate the total of fine for late
returning of library books.
2/17/2011
17
b)Pseudocode
Steps in problem solving that is written half in programming code and half in human language.
For example, some part uses C language code and some part uses Malay or English language.
Advantages:i. Easily understood.
ii. Easily maintained.
iii. The codes are changeable.
Disadvantages:
Cannot be executed in the computer.
Example:
START
Total=0, Average=0
Input a, b, c
Total = a + b + c
Average = Total / 3
Output a, b, c
Output Average
END
c)IPO Chart (Input Process
Output)Chart IPO (input output processing)
Compiling information available in the Detailed get in shape with a display input,
processing and output
Problem 1
Uncle Ahmad wants to buy 5 cans of paint
to paint his house. The price for a can of
paint is RM 25.50. Calculate the price to
be paid for 5 cans of paint to buy.
2/17/2011
18
analysis of the problem:
1) input:
Number of cans of paint purchased, the
price of a can of paint
2) The formula / process:
5 cans of paint price = price x number of
cans
3) output:
prices for purchased 5 cans of paint
Problem 2
A lecturer to determine the grade a
student based on the marks obtained by
students in the special examination. If you
score between 85 and 100, grade A if the
student is not the student will receive a
grade of B. What grades will be earned by
the student if the score is 89?
1) input:
marks obtained
2) The formula / process:
If you score 85-100, is a grade A
If the contrary is a B grade
3) Output:
grade obtained
Problem:
A photostat shops charge 5 cents
for each page copied. Design
solutions to calculate the fee should
imposed if a number of pages that have
been
diphotostat.
2/17/2011
19
definition and identify problems
plan using the variable
Writing pseudo code
Design a flow chart (design software)
Example 1:
Problem: To calculate the amount of water bill
Example 2:
Problem: To calculate area of a circleExample : Calculate the Salary of
Employee
2/17/2011
20
d)Structure Charts Structure chart is an additional method in
preparing programs that has many sub modules.
It consists rectangular boxes, which represents all the sub modules in a program and is connected by arrows.
It illustrates the top-down design of a program and is also known as hierarchical chart because its components are in hierarchical form.
The advantage is that it is easy to be drawn and to be changed.