You are on page 1of 37

1

Computer Programming
Lecture #1
Introduction

 2003 Prentice Hall, Inc. All rights reserved.


2

Programming

• The process of writing computer programs.

• How can we write a program?


– What we need?

LANGUAGE

 2003 Prentice Hall, Inc. All rights reserved.


3

Language

• What is a language?

• What is a programming language?

• Basic types of computer languages:


– Machine Language
– Assembly Language
– High-Level Language

 2003 Prentice Hall, Inc. All rights reserved.


4
Machine Languages, Assembly Languages, and
High-level Languages
• Three types of computer languages
1. Machine language
• Only language computer directly understands
• “Natural language” of computer
• Defined by hardware design
– Machine-dependent
• Generally consist of strings of numbers
– Ultimately 0s and 1s
• Instruct computers to perform elementary operations
– One at a time
• Cumbersome for humans
• Example:
+1300042774
+1400593419
+1200274027

 2003 Prentice Hall, Inc. All rights reserved.


5
Machine Languages, Assembly Languages, and
High-level Languages
• Three types of computer languages
2. Assembly language
• English-like abbreviations representing elementary computer
operations
• Clearer to humans
• Incomprehensible to computers
– Translator programs (assemblers)
• Convert to machine language
• Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY

 2003 Prentice Hall, Inc. All rights reserved.


6
Machine Languages, Assembly Languages, and
High-level Languages
• Three types of computer languages
3. High-level languages
• Similar to everyday English, use common mathematical
notations
• Single statements accomplish large tasks
– Assembly language requires many instructions to
accomplish simple tasks
• Translator programs (compilers)
– Convert to machine language
• Interpreter programs
– Directly execute high-level language programs
• Example:
grossPay = basePay + overTimePay

 2003 Prentice Hall, Inc. All rights reserved.


8

History of C and C++

• History of C++
– Extension of C
– Early 1980s: Bjarne Stroustrup (Bell Laboratories)
– “Spruces up” C
– Provides capabilities for object-oriented programming
• Objects: reusable software components
– Model items in real world
• Object-oriented programs
– Easy to understand, correct and modify
– Hybrid language
• C-like style
• Object-oriented style
• Both

 2003 Prentice Hall, Inc. All rights reserved.


9

C++ Standard Library

• C++ programs
– Built from pieces called classes and functions
• C++ standard library
– Rich collections of existing classes and functions
• “Building block approach” to creating programs
– “Software reuse”

 2003 Prentice Hall, Inc. All rights reserved.


10

Structured Programming

• Structured programming (1960s)


– Disciplined approach to writing programs
– Clear, easy to test and debug, and easy to modify
• Pascal
– 1971: Niklaus Wirth
• Ada
– 1970s - early 1980s: US Department of Defense (DoD)
– Multitasking
• Programmer can specify many activities to run in parallel

 2003 Prentice Hall, Inc. All rights reserved.


11

The Key Software Trend: Object Technology

• Objects
– Reusable software components that model real world items
– Meaningful software units
• Date objects, time objects, paycheck objects, invoice objects,
audio objects, video objects, file objects, record objects, etc.
• Any noun can be represented as an object
– More understandable, better organized and easier to maintain
than procedural programming
– Favor modularity
• Software reuse
– Libraries
• MFC (Microsoft Foundation Classes)
• Rogue Wave

 2003 Prentice Hall, Inc. All rights reserved.


ALGORITHMS AND
FLOWCHARTS
A typical programming task can be divided
into two phases:

 Problem solving phase


 produce an ordered sequence of steps that
describe solution of problem
 this sequence of steps is called an algorithm

 Implementation phase
 implement the program in some programming
language

 2003 Prentice Hall, Inc. All rights reserved.


STEPS IN PROBLEM
SOLVING
 First produce a general algorithm (one
can use pseudo code)

 Refine the algorithm successively to get


step by step detailed algorithm that is
very close to a computer language.

 Pseudo code is an artificial and informal


language that helps programmers
develop algorithms. Pseudo code is
very similar to everyday English.

 2003 Prentice Hall, Inc. All rights reserved.


PSEUDOCODE &
ALGORITHM
 Example 1: Write an algorithm to determine a
student’s final grade and indicate whether it
is passing or failing. The final grade is
calculated as the average of four marks.

 2003 Prentice Hall, Inc. All rights reserved.


PSEUDOCODE &
ALGORITHM
Pseudo code:

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”

 2003 Prentice Hall, Inc. All rights reserved.


PSEUDOCODE &
ALGORITHM
 Detailed
Algorithm
Step Input M1,M2,M3,M4
1: GRADE (M1+M2+M3+M4)/4
Step if (GRADE < 50) then Print
2: “FAIL” else
Step Print “PASS”
3: endif

 2003 Prentice Hall, Inc. All rights reserved.


THE
FLOWCHART
 (Dictionary) A schematic representation of a
sequence of operations, as in a manufacturing
process or computer program.

 (Technical) A graphical representation of the


sequence of operations in an information
system or program. Information system
flowcharts show how data flows from source
documents through the computer to final
distribution to users. Program flowcharts show
the sequence of instructions in a single
program or subroutine. Different symbols are
used to draw each type of flowchart.

 2003 Prentice Hall, Inc. All rights reserved.


THE
FLOWCHART
A Flowchart

shows logic of an algorithm


emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next

 2003 Prentice Hall, Inc. All rights reserved.


FLOWCHART
SYMBOLS
Basi
Name Symbol
cUse in Flowchart

Oval Denotes the beginning or end of the


program

Parallelogram Denotes an input


operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division
etc.

Diamond Denotes a decision (or branch) to be


made. The program should continue
along one of two routes. (e.g.
IF/THEN/ELSE)

Hybri Denotes an output


d operation

Flow Denotes the direction of logic flow in the


line program

 2003 Prentice Hall, Inc. All rights reserved.


EXAMPL
E1
STAR Step 1: Input M1,M2,M3,M4
T
Step 2: GRADE (M1+M2+M3+M4)/4
Input Step 3: if (GRADE <50) then Print
M1,M2,M3,M4
“FAIL”
else
GRADE (M1+M2+M3+M4)/4
Print “PASS” endif

N IS Y
GRADE<5
0

PRINT PRINT
“PASS
” “FAIL”

STOP

 2003 Prentice Hall, Inc. All rights reserved.


EXAMPL
E2
 Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.

Pseudo code:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by multiplying
LFT with 30
 Print length in cm (LCM)

 2003 Prentice Hall, Inc. All rights reserved.


Algorit
Flowchart
hm
 Step 1: Input
STAR
 Lft
Step 2: Lcm Lft x T

30 Input
Lft
 Step 3: Print Lcm
Lcm Lft x 30

Print

Lcm

STOP

 2003 Prentice Hall, Inc. All rights reserved.


EXAMPL
E3
Write an algorithm and draw a flowchart
that will read the two sides of a rectangle
and calculate its area.

Pseudocode
Input the width (W) and Length (L) of a
rectangle
Calculate the area (A) by multiplying L
with W
Print A

 2003 Prentice Hall, Inc. All rights reserved.


Algorithm

STAR
T
 Step Input W,L
1: A L Input
W,
 Step x L

2: W
A LxW
 Step Print A
3:
Print
A

STOP

 2003 Prentice Hall, Inc. All rights reserved.


DECISION STRUCTURES
 The expression A>B is a logical
expression
 it describes a condition we want to test

 if A>B is true (if A is greater than B) we


take the action on left
 print the value of A

 if A>B is false (if A is not greater than B)


we take the action on right
 print the value of B
 2003 Prentice Hall, Inc. All rights reserved.
DECISION STRUCTURES

Y N
is
A>B

Print Print
A B

 2003 Prentice Hall, Inc. All rights reserved.


IF–THEN–ELSE
STRUCTURE
 The structure is as
follows:

If condition then
true alternative
else
false alternative
endif

 2003 Prentice Hall, Inc. All rights reserved.


IF–THEN–ELSE
STRUCTURE
 The algorithm for the flowchart is as
follows:
If A>B
then
print A
Y N
is
else A>B
print B
endif
Print Print
A B

 2003 Prentice Hall, Inc. All rights reserved.


RELATIONAL
OPERATORS
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
Greater than or equal to
Less than or equal to
Not equal to

 2003 Prentice Hall, Inc. All rights reserved.


EXAMPL
E5
 Write an algorithm that reads two
values, determines the largest value and prints
the largest value with an identifying message.

ALGORITHM

Step Input VALUE1, VALUE2


1: if (VALUE1 > VALUE2)
Step then
2: MAX VALUE1
els
e MAX VALUE2
endif
Step Print “The largest value
3: is”, MAX
 2003 Prentice Hall, Inc. All rights reserved.
START

Input
VALUE1,VAL
UE2

Y is
N
VALUE1>VALUE2

MAX MAX
VALUE1 VALUE2

Print
“The largest value is”,
MAX

STOP
 2003 Prentice Hall, Inc. All rights reserved.
NESTED
IFS
 One of the alternatives within an IF–THEN–
ELSE statement
 may involve further IF–THEN–ELSE statement

 2003 Prentice Hall, Inc. All rights reserved.


EXAMPL
E6
 Write an algorithm that reads three numbers
and prints the value of the largest number.

 2003 Prentice Hall, Inc. All rights reserved.


Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2, N1>N3]
else
MAX N3 [N3>N1>N2]
endif else
if (N2>N3) then

MAX N2 [N2>N1, N2>N3]


else
MAX N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest
number is”, MAX

 2003 Prentice Hall, Inc. All rights reserved.


35

Basics of a Typical C++ Environment

• C++ systems
– Program-development environment
– Language
– C++ Standard Library

 2003 Prentice Hall, Inc. All rights reserved.


36

Basics of a Typical C++ Environment


Program is created in
Editor
Phases of C++ Programs: Disk the editor and stored
on disk.

Preprocessor Preprocessor program

1. Edit
Disk
processes the code.

Compiler creates
Compiler Disk object code and stores

2. Preprocess it on disk.

Linker links the object


Linker Disk code with the libraries,

3. Compile Primary
creates a.out and
stores it on disk

Memory
Loader
4. Link Loader puts program
in memory.
Disk
5. Load ..
..
..

Primary

6. Execute CPU
 
Memory

CPU takes each


instruction and
executes it, possibly
storing new data
..
values as the program
.. executes.
..

 2003 Prentice Hall, Inc. All rights reserved.


37

Basics of a Typical C++ Environment

• Input/output
– cin
• Standard input stream
• Normally keyboard
– cout
• Standard output stream
• Normally computer screen
– cerr
• Standard error stream
• Display error messages

 2003 Prentice Hall, Inc. All rights reserved.


38

Introduction to C++ Programming

• C++ language
– Facilitates structured and disciplined approach to computer
program design
• Following several examples
– Illustrate many important features of C++
– Each analyzed one statement at a time
• Structured programming
• Object-oriented programming

 2003 Prentice Hall, Inc. All rights reserved.

You might also like