CHAPTER 1CHAPTER 1INTRODUCTIONINTRODUCTION
INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER
PROGRAMMINGPROGRAMMING(CSC425)(CSC425)
2
CONTENTS
CHAPTER 1 : Introduction To Computer Programs
• A Brief History of the C++ LanguageGeneration of Programming LanguageWhat is a computerHardware and Software of a computer What is a computer programTranslation using Compilers or Interpreters
• Preparation for Programming
Se
me
ster Ja
n –
Ap
r 20
10
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 & T’s 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
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
4
3GL
4GL
5GL
Machine LanguageMachine Language
Assembly LanguageAssembly Language
High-Level LanguageHigh-Level Language Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Generation of Programming Language
5
Machine Language (1940s – 1950s)Machine Language (1940s – 1950s)
Se
me
ster Ja
n –
Ap
r 20
10
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)Assembly Language (1950s – 1960s)
Se
me
ster Ja
n –
Ap
r 20
10
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 AddA for Add
C for CompareC for Compare
MP for MultiplyMP 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
Se
me
ster Ja
n –
Ap
r 20
10
Generation of Programming Language
8
High Level LanguageHigh Level Language
Se
me
ster Ja
n –
Ap
r 20
10
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”
9CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
CS
C4
25
- Intro
du
ction
To
C
om
pu
ter P
rog
ram
min
g
10
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Generation of Programming Language
3rd Generation Language (1960 – 1970)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 LanguageHigh Level Language
11Example coding in FORTRAN
FFOORRTTRRAANN
Se
me
ster Ja
n –
Ap
r 20
10
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 LanguageHigh Level LanguageHigh Level Language
12
Se
me
ster Ja
n –
Ap
r 20
10
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 LanguageHigh Level Language
13
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
4th Generation Language (1970 – 1980s)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 LanguageHigh Level LanguageHigh Level Language
14
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
5th Generation Language (1990s)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 LanguageHigh Level LanguageHigh 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.
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
16
Language TranslatorS
em
este
r Jan
– A
pr 2
01
0
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
AssemblerAssembler 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. Assembler2. Interpreter 3. Compiler
17
Sourceprogram
Data
INTERPRETER RESULTS
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
InterpreterInterpreter 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
Sourceprogram
Data
COMPILER RESULTSOBJECT
PROGRAM
ProgramListing
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
CompilerCompiler 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
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Programming Approach
2 typesStructured Object -oriented
1.1. Structured ProgrammingStructured 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 variablesprocedure - the instructions that tell the data what to do
- called operations or method.
Se
me
ster Ja
n –
Ap
r 20
10
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Programming Approach
2.2. Object-oriented programmingObject-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
Se
me
ster Ja
n –
Ap
r 20
10
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.
22CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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
23CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
ProgramInput Output
StorageExampleExample
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
Se
me
ster Ja
n –
Ap
r 20
10
Basic Concepts
Compilation Process
25
CompilerSource File Object File
(Input) (Output)
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
CS
C4
25
- Intro
du
ction
To
C
om
pu
ter P
rog
ram
min
g
26
Problem Analysis
Programs Modelling
Coding Programs
Testing and Debugging
Maintenance
DOCUMENTATION
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
27CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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
Se
me
ster Ja
n –
Ap
r 20
10
29
Input Process Output
Defining the problem Identify :
1. Input ( given data)2. Output ( the result)3. Process relation between input and outputusing formula
Problem Analysis
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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
Se
me
ster Ja
n –
Ap
r 20
10
Example 2 Evaluate the following problem statement and
identify the input and output.
Nation’s Air force has asked you to write a program to label supersonic aircraft as military or civilian. Your program is to be given the plane’s 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
Se
me
ster Ja
n –
Ap
r 20
10
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
Se
me
ster Ja
n –
Ap
r 201
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
33CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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
ExampleExampleCSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
35
1. Set sum = 0, avg = 02. Read 3 integer number3. Total up 3 integer number
sum = a+b+c4. Find the average
avg = sum / 35. Display 3 integer number and average
algorithm
Program Design
Example to get average of 3 numbers.
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
36
Flowchart
Pseudocode
START INPUT a,b,c sum = a+b+c avg = sum / 3 PRINT a,b,c PRINT avgEND
Refer to Example Average
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
Program Design
Refer to Example Aircraft
STARTREADSpeed, LengthIF Speed > 1100 FALSE - Classification = “Aircraft Type Unknown”TRUE - Length > 52FALSE - Classification = “Military”TRUE Classification = “Civillian”
END
37CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
Program Design
38CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
Program DesignRefer to Example Aircraft
Desk-Check the algorithm
Speed Length Classification
11701180900800
35566634
????
39
Program Design
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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 it’s own syntax (rules of
language)
40
Source CodeSource Code
Coding
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
41
Refer to Example Average
Coding in C++ Language
Output of the program
Coding
41CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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
42CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
Testing and Debugging Three types of error:
1. Syntax error2. Logic error3. 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“compile-time error”
43CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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.
44CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
Documentation and MaintenanceDocumentation 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
45CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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
Se
me
ster Ja
n –
Ap
r 20
10
47
A good program
must have
AccuracyAccuracy
ReliabilityReliability
EfficiencyEfficiency
MaintainabilityMaintainability
ReadabilityReadability
UsabilityUsability
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10
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. 48CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING
Se
me
ster Ja
n –
Ap
r 20
10