Sei sulla pagina 1di 48

CHAPTER 1

INTRODUCTION
INTRODUCTION TO
COMPUTER PROGRAMMING
(CSC425)
2
CONTENTS
CHAPTER 1 : Introduction To Computer Programs
A Brief History of the C++ Language
Generation of Programming Language
What is a computer
Hardware and Software of a computer
What is a computer program
Translation using Compilers or Interpreters
Preparation for Programming
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
A BRIEF HISTORY OF THE C++ LANGUAGE
The C++ Programming Language is basically an
extension of the C Programming Language.
C is a programming language which born at AT &
Ts Bell Laboratories of USA in 1972. It was written
by Dennis Ritchie.
This language was created for a specific purpose:
to design the UNIX operating system (which is used on
many computers).
From the beginning, C was intended to be useful--to
allow busy programmers to get things done.
C++ is the new improved version of C for the latest
programming needs. It contains everything C does,
plus new additions for OOP concept.

3
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
4
3GL
4GL
5GL
Machine Language
Assembly Language
High-Level Language
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Generation of Programming Language
5
Machine Language (1940s 1950s)
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Generation of Programming Language
Lowest level of language
Represent data and program instructions as 1s and 0s
The only language that computer directly understand.
(Do not require translator)
Not convenient to read and use.
First generation language
Machine - dependent






6

Assembly Language (1950s 1960s)
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Generation of Programming Language
Second generation language
Developed to replace 1s and 0s use in machine language.
Use mnemonic codes : abbreviations that easy to remember
A for Add
C for Compare
MP for Multiply

Requires a translator to translate the assembly program into machine
language ( assembler).
Difficult to learn
Machine-dependent







The difference between Machine Language and
Assembly Language




7
Memory Address Machine-Language
Instructions
Assembly-Language
Instructions
00000000 00000000 CLA
00000001 00010101 ADD A
00000010 00010110 ADD B
00000011 00110101 STA A
A Machine-language Program Fragment
and Its Assembly-Language Equivalent
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Generation of Programming Language
8
High Level Language
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Made easy for programmer to develop and maintain program
Machine- independent (can run on may different types of
computers)
Have 3 categories : third, fourth and fifth generation
Written in series of English-like words
Must be translated to machine code first. (Use translator)




Generation of Programming Language
High-level languages
Language Application Area Origin Name
FORTRAN Scientific Programming Formula Translation
COBOL Business data Processing Common Business-Oriented
Language
Lisp Artificial Intelligent List processing
C System Programming Predecessor Language was named B
Prolog Artificial Intelligent Logic Programming
C++ Support objects and object
oriented programming
Incremental modification of C (++ is
the C incremental operator)
Java Supports Web Programming Originally name Oak
9 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

C
S
C
4
2
5

-

I
n
t
r
o
d
u
c
t
i
o
n

T
o

C
o
m
p
u
t
e
r

P
r
o
g
r
a
m
m
i
n
g

10

S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Generation of Programming Language
3rd Generation Language (1960 1970)
Instruction is written as a series of English-like words.
Often are called procedural languages
Translation is performed using compiler or translator
Example of 3GL programming language:
FORTRAN, COBOL and BASIC





High Level Language
11
Example coding in FORTRAN
F
O
R
T
R
A
N
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
FORTRAN
Stands for FORmula TRANslator
Develop in the late 1950s by team of IBM
Designed for scientific applications
Used most often by scientist, engineers and mathematicians.
Generation of Programming Language
High Level Language
12
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
COBOL
Common Business Oriented Language
was developed in the early 1960s
more widely used used for business applications
easy to read, write and maintain


Generation of Programming Language
BASIC
Was develop in the mid - 1960s by John Kemeny and Thomas
Kurtz
Easy to learn and used
Widely used for education and business application.
High Level Language
13

S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
4th Generation Language (1970 1980s)
Also uses English-like statements
Non- procedural languages
only to specify what is to be accomplished without
explaining how
Coding programs requires less time and effort
Example of programming language
PROLOG, FOCUS, LISP and FORTH
Generation of Programming Language
High Level Language
14
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
5th Generation Language (1990s)
Also called natural language.
A type of query language that allows the user to enter requests
that resemble human speech.
Translate human instructions into code the computer
understands.
Sometimes referred to as knowledge-based language.

Generation of Programming Language
High Level Language
15
Example of Natural language

Welcome to Inquiry

What would you like to know?
Do we have hammer #37645?
One moment please..
Yes, we have 14 hammer #37645.
Do we have 1 box nails #3601?
One moment, please.
No nails #3601 in stock.
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
16
Language Translator
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Assembler
Used in assembly language for translate the language to
machine language
Program need to translate because computer only
understand machine language.
Three kind of translator :
1. Assembler
2. Interpreter
3. Compiler
17
Source
program
Data
INTERPRETER RESULTS
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Interpreter
A set of program that executes the instructions as they were
translated. Interpreter executes a program from its high-level form.
Translates one program code statement at a time.
Immediately displays feedback when it finds error.
Use for Basic
Language Translator
18
Source
program
Data
COMPILER
RESULTS
OBJECT
PROGRAM

Program
Listing
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER
PROGRAMMING
Compiler
A program that translates a high-level language program into
machine language as a complete unit
Translating the source code from its original language into
machine code.
Converts the entire source program into machine language at one
time
Use for C, C++ and Cobol
Language Translator
19
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Programming Approach
2 types Structured Object -oriented
1. Structured Programming

A methodology in which all program logic is
constructed from a combination of three control
structures : sequence, selection and repetition (loop)
Program logic is description of the step-by step
procedures for a module.



20
data - called attributes or variables
procedure - the instructions that tell the data what to do
- called operations or method.
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Programming Approach

2. Object-oriented programming

Packaged the data and procedure into a single unit called an
object which can be reused and modified.


Object have classes and subclasses.
Each class contains the characteristics that are unique to
objects of that class.







PREPARATION FOR PROGRAMMING
Reason for studying concepts of programming
languages :
1. Increased capacity to express ideas and solve
problems.
Studying programming languages may increase the
capacity and ability of students to express their ideas in
a formal, computational form.
2. Increased ability to automate process
Programs are built mainly so that simple, or even
complicated processes to be executed automatically.
21
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Computer programming
Craft of developing a computer program.
Require knowledge, skill and creativity.
Both skill and knowledge in problem solving and
programming language.
22 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Computer program
Hanly [2001], List of instructions that direct the computer to
transform information from one form to another.
Information refers to the contains of specific memory location.
It is written using programming language.
Turn data (input) into useful information (output).
Program uses variables to store data

23 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Program
Input
Output
Storage
Example
Source file
A file containing a program written in a high-level
language; the input for the compiler
Syntax
Grammar rules of programming language
Object file
File of machine-language instructions that is the
output of a compiler
Algorithm
A precise step-by-step action to perform overall task
of the program.
24
Basic Concepts
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Basic Concepts
Compilation Process
25
Compiler
Source File Object File
(Input) (Output)
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

C
S
C
4
2
5

-

I
n
t
r
o
d
u
c
t
i
o
n

T
o

C
o
m
p
u
t
e
r

P
r
o
g
r
a
m
m
i
n
g

26
Problem Analysis
Programs Modelling
Coding Programs
Testing and Debugging
Maintenance

D
O
C
U
M
E
N
T
A
T
I
O
N

PROGRAM DEVELOPMENT LIFE CYCLE
PROGRAM DEVELOPMENT LIFE CYCLE
Computer programming is a process to develop a
computer program.
5 steps:
1. Problem analysis,
2. Program design,
3. Coding,
4. Testing and debugging, and
5. Documentation and Maintenance
27 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

A process of identifying the output, processes and input of a
program.
How to identify output?
Nouns and adjectives
Keywords print, display, produce
How to identify input?
Nouns and adjectives
Keywords accept, enter, receive, read
Define the storage (variable) and data type to hold data
Outline the process
Arithmetic or logic operation
Input-Process-Output (IPO) chart as tool

28
Problem Analysis
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

29

Input Process Output
Defining the problem
Identify :
1. Input ( given data)
2. Output ( the result)
3. Process
relation between input and output
using formula

Problem Analysis
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

30
Example 1

Write a program that can input 3 integer number from
user. Find the average for the number. Display all the
numbers and the average

Problem analysis
Input : 3 numbers
Process : 1. Total up 3 numbers
2. Divide the number by 3
Output : 3 numbers and the average



Problem Analysis
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Example 2
Evaluate the following problem statement and identify the
input and output.

Nations Air force has asked you to write a program to label
supersonic aircraft as military or civilian. Your program is to
be given the planes observed speed in km/h and its estimated
length in meters. For planes traveling in excess of 1100km/h,
you will label those longer than 52 meters civilian and
shorter aircraft as military. For planes traveling at slower
speeds, you will issue an aircraft type unknown message.

31
Problem Analysis
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Input-Process-Output (IPO) chart
INPUT PROCESSING OUTPUT
speed
length
Validate the speed and length


Speed >1100km/h AND
length > 52m
Speed > 1100km/h AND
length <= 52m
Speed <= 1100km/h


Classification
Can be one if the
following values

Civilian

Military

Aircraft type unknown
32
Problem Analysis
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Program Design
Planning the solution to a problem
Developing an algorithm
Algorithm is a precise step-by-step action to perform
overall task of the program.
Can be represented either using flowchart or pseudo-
code
Tools used for Program Design are:-
Pseudo Code
Flow Chart

33 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Flow chart
A set of symbols and edges used in flow chart:
34

Terminal (Begin and End)
To represent the process
Input/Output Operation
Condition/Evaluation
Direction
Example
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

35
1. Set sum = 0, avg = 0
2. Read 3 integer number
3. Total up 3 integer number
sum = a+b+c
4. Find the average
avg = sum / 3
5. Display 3 integer number and average
algorithm
Program Design
Example to get average of 3 numbers.

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

36
Flowchart
sum =0 , avg = 0
Read a, b, c
sum = a+b+c
avg = sum / 3
Display a,b,c
Display avg
End
start
Pseudocode
START
INPUT a,b,c
sum = a+b+c
avg = sum / 3
PRINT a,b,c
PRINT avg
END

Refer to Example Average

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Program Design
Refer to Example Aircraft

START
READ
Speed, Length
IF
Speed > 1100
FALSE - Classification = Aircraft Type Unknown
TRUE - Length > 52
FALSE - Classification = Military
TRUE Classification = Civillian
END




37 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Program Design
38
Begin
Read Speed
Read Length
End
Speed > 1100
Classification = Military
Classification = Civilian
Classification = Aircraft Type Unknown
Length > 52
Display classification
False
False
True
True
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Program Design
Refer to Example Aircraft

Desk-Check the algorithm
Speed Length Classification
1170
1180
900
800
35
56
66
34
?
?
?
?
39
Program Design
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Refer to Example Aircraft

Implement the flowchart or pseudo code into specific
programming language rules (syntax)
Identify the storage requirement
Compilation
Express solution in a programming language
Translate the logic from the flowchart or pseudocode.
There are many programming languages : BASIC, COBOL,
Pascal, Fortran, C, C++, Java etc
Each language has its own syntax (rules of language)

40
Source Code
Coding
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

41
Refer to Example Average
Coding in C++
Language
Output of the program
Coding
41
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Testing and Debugging
Program must be freed from syntax error
Use a set of test data to validate the output.
Program must produce receive valid input and produce
correct output.
Program must handle invalid input efficiently.
Does the program accept out of range value?
Trace error either syntax or logic error
1. Testing
running the program with a set of data
2. Debugging
Trace and fixed the error

42 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Testing and Debugging
Three types of error:
1. Syntax error
2. Logic error
3. Run-time error

Syntax error
Occurs when not applied the rules of the language.
Can be traced by the compiler during compilation
Also called as compile-time error


43 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Testing and Debugging
Logic error
Error in logic of processing
Cannot be traced by the compiler
Output produced is wrong
Run-time error
errors caused program instructions that require the
computer to do something illegal such as an attempt to
divide a number by 0.
program will stop automatically and display a certain
messages.

44 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Documentation and Maintenance
Documentation
User manual
Written detailed description of the program cycle and specific facts
about the program
Documentation materials include :
1. Description of the program
2. Logic tools : flowcharts, pseudocode
3. Data- record descriptions
4. Program listing
5. Testing results
6. Comments
Program description
capability, limitation, user guide
45
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

46
Documentation and Maintenance
Maintenance
Modification made to the finished program
Software to meet current requirement
Need to refer the previous documentation
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

47
A good
program
must have
Accuracy
Reliability
Efficiency
Maintainability
Readability
Usability
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

SELF EXERCISE
1. Write a flow chart to calculate and display an average of three
numbers using sequential design
2. Rewrite the above design, determine whether the average is
higher than 50 or not. Display the appropriate message for
each case.
3. Rewrite problem 1 using iteration any iteration approach.
4. Write a flow chart to calculate an average score of 20
students.
5. Write a flow chart to calculate an average of a series positive
numbers. The process will stop if number entered has a
negative value.
6. The cost to send a telegram to UK is RM15.50 for the first 15
letters and RM0.50 for the subsequent letters. Draw a
flowchart to calculate the cost of sending the telegram.
48 CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
S
e
m
e
s
t
e
r

J
a
n


A
p
r

2
0
1
0

Potrebbero piacerti anche