Chapter 1. Introduction to Computers and Programming

advertisement
Chapter 1. Introduction to
Computers and Programming
1
Starting Out with C++, 3rd Edition
1.1 Why Program?
• Computers can do many different jobs
because they are programmable.
2
Starting Out with C++, 3rd Edition
1.2 Computer Systems:
Hardware and Software
• All computer systems consist of similar
hardware devices and software components.
This section provides an overview of
standard computer hardware and software
organization.
3
Starting Out with C++, 3rd Edition
Hardware
1.
2.
3.
4.
5.
The CPU
Main Memory
Secondary Storage
Input Devices
Output Devices
4
Starting Out with C++, 3rd Edition
Figure 1.1
Input
Device
Central
Processing
Unit
Output
Device
Main
Memory
Output
Device
5
Starting Out with C++, 3rd Edition
Figure 1.2
Instruction
(Input)
Arithmetic
and Logic
Unit
Result
(Output)
Control Unit
6
Starting Out with C++, 3rd Edition
Software
– Operating Systems
• Single tasking
• Multi-tasking
– Application Software
7
Starting Out with C++, 3rd Edition
1.3 Programs and
Programming Languages
• What is a program?
– A set of instructions a computer follows in
order to perform a task. A programming
language is a special language used to write
computer programs.
8
Starting Out with C++, 3rd Edition
Program 1-1
// This program calculates the user’s pay.
#include <iostream.h>
void main(void)
{
float hours, rate, pay;
cout << “How many hours did you work? ”;
cin >> hours;
cout << “How much do you get paid per
hour? ”;
cin >> rate;
pay = hours * rate;
cout << “You have earned $” << pay <<
endl;
}
9
Starting Out with C++, 3rd Edition
Program Output
How many hours did you work? 10
How much do you get paid per hour? 15
You have earned $150
10
Starting Out with C++, 3rd Edition
Programming Languages
• Figure 1-4
High level
(Close to Human
Language)
Low level
(Machine Language)
11
Starting Out with C++, 3rd Edition
Table 1-1
Language
B A S IC
FORTRAN
COBOL
P ascal
C
C++
Jav a
D escrip tio n
B eg in n ers A ll-p u rp ose S y m b o lic In stru ctio n C o d e.
A g en eral p ro g ram m in g lan g u ag e o rig in ally
d esig n ed to b e sim p le en o u g h fo r b eg inn ers to learn .
F o rm u la T ran slato r. A lan g u ag e d esig n ed fo r
p ro gram m in g co m p lex m ath em atical alg o rith m s.
C o m m o n B u sin ess-O rien ted L an g u ag e. A lan g u ag e
d esig n ed fo r bu sin ess ap p licatio n s.
A stru ctu red , g en eral p u rp o se lan g u ag e d esig n ed
p rim arily fo r teach in g p ro g ram m in g .
A stru ctu red , g en eral p u rp o se lan g u ag e d ev elo p ed at
B ell L ab s. C o ffers b o th h ig h -lev el an d lo w -lev el
featu res.
B ased o n th e C lan g u ag e, C + + o ffers o b ject-o rien ted
featu res n o t fo u n d in C . A lso in v en ted at B ell
L ab o rato ries.
A n o b ject-o rien ted lan g u ag e in v en ted at S u n
M icro sy stem s. Jav a m ay b e u sed to d ev elo p
p ro gram s th at ru n o v er th e In tern et, in a w eb
b ro w ser.
12
Starting Out with C++, 3rd Edition
1.4 What is a Program Made of?
• There are certain elements that are common
to all programming languages.
–
–
–
–
Key Words
Programmer-Defined Symbols
Operators
Punctuation
13
Starting Out with C++, 3rd Edition
Language Elements, Table 1-2
Language
E lem en t
K ey W o rd s
P ro gram m erD efin ed
S y m b o ls
O p erato rs
P u n ctu atio n
S y n tax
D escrip tio n
W o rd s th at h av e a sp ecial m ean in g . K ey
w o rd s m ay o n ly b e u sed fo r th eir in ten d ed
p u rpo se.
W o rd s o r n am es d efin ed b y th e p ro g ram m er.
T h ey are sy m b o lic n am es th at refer to
v ariab les o r p ro g ram m in g ro u tin es.
O p erato rs p erfo rm o p eratio n s o n o n e o r m o re
o p eran d s. A n o p eran d is u su ally a p iece o f
d ata, lik e a n u m b er.
P u n ctu atio n ch aracters th at m ark th e
b eg in n ing o r en d ing o f a statem en t, o r
sep arate item s in a list.
R u les th at m u st b e fo llo w ed w h en
co n stru ction a p ro g ram . S y n tax d ictates h o w
k ey w o rd s an d o p erato rs m ay b e u sed , an d
w h ere p u n ctu atio n sy m b o ls m u st ap p ear.
14
Starting Out with C++, 3rd Edition
Lines and Statements
cout << “How many hours did you work?”;
15
Starting Out with C++, 3rd Edition
Variables
• A storage location in the computer’s
memory for holding a piece of
information.
• Symbolic names that represent locations
in the computer’s random-access
memory.
16
Starting Out with C++, 3rd Edition
Variable Declarations
• Two types of information: numbers and
characters
• Numbers may be integers or floating-point
numbers
• The statement below creates three variables in
memory named hours, rate, and pay that each
can store a floating point number
float hours, rate, pay;
17
Starting Out with C++, 3rd Edition
1.5 Input, Processing, and Output
• Input:
cin >> hours;
• Processing:
pay = hours * rate;
• Output
cout<<“You have earned $”<<pay;
18
Starting Out with C++, 3rd Edition
1.6 The Programming Process
• The programming process consists of
several steps, which include design,
creation, testing and debugging activities.
19
Starting Out with C++, 3rd Edition
Designing and Creating a
Program
1. Clearly define what the program is to do
2. Visualize the program running on the
computer.
3. Design a flowchart or hierarchy chart
4. Check the flowchart or hierarchy chart for
logical errors.
20
Starting Out with C++, 3rd Edition
5. Write a pseudocode version of the
program.
6. Check the pseudocode for errors.
7. Write the actual program on paper.
8. Desk-check the program for errors.
9. Enter the code and compile it.
10. Correct any errors found during
compilation. Repeat steps 9 and 10 as
many times as necessary.
21
Starting Out with C++, 3rd Edition
11. Run the program with test data for
input.
12. Correct any errors found while
running the program. Repeat steps
9 through 12 as many times as
necessary.
13. Validate the results of the program.
22
Starting Out with C++, 3rd Edition
1.7 Procedural and Object-Oriented
Programming
• Procedural programming and objectoriented programming are two ways of
thinking about software development and
program design.
23
Starting Out with C++, 3rd Edition
Download