Chapter 1-Introduction Programming-CS126-3rd QTR 2012 2013
Chapter 1-Introduction Programming-CS126-3rd QTR 2012 2013
PROGRAMMING 1
INTRODUCTION : Overview
of Computers and Logic
Input Processing Output
2
Understanding Computer
Components and Operations
• Hardware: equipment, or devices,
associated with a computer
• For a computer to be useful, it needs more
than equipment; a computer needs to be
given instructions
• The instructions that tell the computer what
to do are called software, or programs, and
are written by programmers
• Hardware devices that perform input
include keyboards and mice
3
Understanding Computer
Components and Operations
(continued)
• Through input devices,
– data, or facts, enter the computer system
• Processing data items may involve
– organizing them,
– checking them for accuracy, or
– performing mathematical operations on them
4
Understanding Computer
Components and Operations
(continued)
• The hardware that performs these sorts
of tasks is the central processing unit,
or CPU
• After data items have been processed,
the resulting information is sent to a
printer, monitor, or some other output
device so people can view, interpret,
and use the results
5
Understanding Computer
Components and Operations
(continued)
A digital computer is a machine (also called hardware) that is
capable of storing data and executing algorithms. It is made up of
the following physical components
1. Central Processing Unit (CPU)
2. Memory
2.1 Primary Memory
Read Only Memory (ROM)
Random Access Memory (RAM)
2.2 Secondary Memory
Includes magnetic, hard disk, floppy diskette, ZIP disk, CD-ROM
3. Input/Output Devices (I/O)
3.1 Input devices include keyboard, mouse, touchpad etc.
3.2 Output devices include display screen, printer, plotter
6
Understanding Computer
Components and Operations
(continued)
Software
In loose terms, software refer to computer programs
which serve as interface between the user and the
hardware. Software can be categorized into two,
namely:
1. System Software
- including operating system, compilers, linkers etc.
2. Application Software
- including word processor, spreadsheet, calculator etc.
7
Understanding Computer
Components and Operations
(continued)
What is a computer program?
A computer program is very much like an
algorithm. However, it may not be finite,
i.e., a program need not terminate after a
finite number of steps. An example of such
a program is an operating system.
Basically, a computer program is made up of
two components:
1. Data
2. Instructions for manipulating data
8
Understanding Computer
Components and Operations
(continued)
A computer program is written using a
certain computer programming language.
A programming language describes the:
1. vocabulary (set of symbols and words)
2. syntax (rules)
3. semantics (meaning)
that are used to write a valid computer
program.
9
Understanding Computer
Components and Operations
(continued)
• You write computer instructions in a
computer programming language, such as
Visual Basic, Pascal, COBOL, RPG, C#, C++,
Java, or Fortran
• Every language has rules governing its word
usage and punctuation
• Programming rules are called the language’s
syntax
• Each programming language uses a piece of
software to translate the specific
programming language into the computer’s
on-off circuitry, or machine language 10
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming Languages
1. Low-Level Languages
Low-level languages include:
machine language (lowest level, defined in terms of zero's and
one's)
- assembly language (uses mnemonics)
>They are called low-level because they require the
programmers to have a good understanding of the underlying
hardware for which the programs are intended to run
>Programs written using low-level languages are not portable,
i.e. they cannot be executed in two different and incompatible
processors
11
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming
Languages
1. Low-Level Languages
• To calculate wages = rates * hours in
machine language:
100100 010001 //Load
100110 010010 //Multiply
100010 010011 //Store
12
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming Languages
1. Low-Level Languages
• Assembly language instructions are mnemonic
• Assembler: translates a program written in
assembly language into machine language
13
Understanding Computer
Components and Operations
(continued)
• Using assembly language instructions,
wages = rates • hours can be written
as:
LOAD rate
MULT hour
STOR wages
14
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming Languages
2. High-Level Languages
High-level languages include:
- FORTRAN - ALGOL
- COBOL - PL/1
- BASIC - Pascal
- C++ - Java
>They are called high-level because (1) the language is quasi-
English and (2) it does not require the programmer to know
the actual computer hardware
>The programs written in high-level languages are able to exhibit
portability
15
Understanding Computer
Components and Operations
(continued)
• The language translation software, known as a
compiler or interpreter, tells you if you have
used a programming language incorrectly
• For a program to work properly, you must
give the computer exact instructions in a
specific sequence
• By doing this, you are developing the logic of
the computer program
• Once instructions have been inputted to the
computer and translated into machine
language, a program can be run, or executed
16
Understanding Computer
Components and Operations
(continued)
• Besides input, processing, and output,
all computer systems need and have:
– Internal storage, commonly called memory,
main memory, or primary memory. Though
needed to run programs, internal memory is
volatile—that is, its contents are lost every
time the computer loses power
– External storage, or permanent storage
outside the main memory of the machine, is
held on a device such as a floppy disk, hard
disk, or magnetic tape
17
Understanding the Programming
Process
• The programmer’s job can be broken
down into six programming steps:
1. Understand the problem
2. Plan the logic
3. Code the program
4. Translate the program into machine
language
5. Test the program
6. Put the program into production
18
Understand The Problem
• Really understanding the problem may
be one of the most difficult aspects of
programming
– The description of what the user needs may
be vague
– The user may not even really know what he
or she wants
– Users who think they know what they want
frequently change their minds after seeing
sample output
• A good programmer is often part
counselor, part detective
19
Plan the Logic
• Programmer plans the steps to the
program, deciding what steps to include
and how to order them
• The two most common tools are
flowcharts and pseudocode
• Both tools involve writing the steps of
the program in English
20
Code the Problem
• Some very experienced programmers can
successfully combine the logic planning
and the actual instruction writing, or
coding of the program, in one step
21
Translate the Program into
Machine Language
• Languages such as Java or Visual Basic translate
the programmer’s English-like high-level
programming language into the low-level
machine language that the computer understands
22
Translate the Program into
Machine Language (continued)
• All syntax errors are caught by the
compiler or interpreter
• When writing a program, a programmer
might need to recompile the code several
times
• An executable program is created only
when the code is free of syntax errors
• When you run an executable program, it
might also typically require input data
23
Creating an Executable Program
24
Test the Program
• A program that is free of syntax errors
is not necessarily free of logical errors
26
Understanding the Data Hierarchy
• When data is stored for use on computer
systems, it is often stored in a data hierarchy,
where the smallest usable unit of data is the
character
29
The Language of a Computer
(cont’d.)
30
The Language of a Computer
(cont'd.)
• ASCII (American Standard Code for
Information Interchange)
– 128 characters
– A is encoded as 1000001 (66th character)
– 3 is encoded as 0110011
31
The Language of a Computer
(cont'd.)
• EBCDIC
– Extended Binary Coded Decimal Interchange
Code
– Used by IBM (mainframe, midrange comp)
– 256 characters
• Unicode
– 65536 characters
– Two bytes are needed to store a character
32
Programming with the Problem
Analysis–Coding–Execution Cycle
• Programming is a process of problem
solving
• One problem-solving technique:
– Analyze the problem
– Outline the problem requirements
– Design steps (algorithm) to solve the problem
• Algorithm:
– Step-by-step problem-solving process
– Solution achieved in finite amount of time
33
Using Flowchart Symbols and
Pseudocode Statements
• Flowcharts (pictorial representations) and
pseudocode (English-like representations)
are used by programmers to plan the
logical steps for solving a programming
problem
• Some professional programmers prefer
writing pseudocode to drawing flowcharts,
because using pseudocode is more similar
to writing final statements in programming
language
End
Selection/Conditional Structure
#1] Create a flowchart and a pseudocode
that will prompt the user to input two
number and output the highest number
entered.
Flowchart
Start
Input N1, N2
If Y
N1 > N2
Output N1 A
Output N2
A
End
Pseudocode
Variables Used:
N1, N2 are numeric
Begin
Display ” Input two numbers: ”
Accept N1, N2
if (N1 > N2) then
Display N1
else
Display N2
End
Iterative Program Structure
#1] Create a flowchart/pseudo code
that will generate the following number
series:
X = 10
Y=5
Output X, Y
X=X–1
Y=Y+5
Y
If
X >= 6
N
End
Pseudo code
Variables Used:
X, Y are numeric
Begin
X = 10
Y=5
while (X >= 6) do
begin
Display X, Y
X=X–1
Y=Y+5
end
End
Iterative Program Structure
#5] Create a flowchart/pseudo code
that will input five numbers and output
how many of the numbers entered
are positive or negative.
Flowchart
A
Start
ctrn = ctrn + 1
A
Pseudo code
Variables Used:
num, ctr, ctrp, ctrn are numeric
Begin
ctr = 1
ctrp = 0
ctrn = 0
while (ctr <= 5) do
begin
Accept num
if (num > 0) then
ctrp = ctrp + 1
else
ctrn = ctrn + 1
ctr = ctr + 1
end
Display ctrp, ctrn
End
QUESTIONS???