Chapter 1.
Introduction to
Computers and Programming
For Educational Purpose Only Not be circulated without this banner
Starting Out with C++, 3rd Edition
1.1 Why Program?
Computers can do many different jobs
because they are programmable.
For Educational Purpose Only Not be circulated without this banner
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
overviewwithout
of this banner
For Educational
Purpose
Only Not an
be circulated
standard computer hardware and software
organization.
Starting Out with C++, 3rd Edition
Hardware
1. The CPU
2. Main Memory
3. Secondary
For Educational
PurposeStorage
Only Not be circulated without this banner
4. Input Devices
5. Output Devices
Starting Out with C++, 3rd Edition
Figure 1.1
Input
Device
Central
Processing
Unit
Output
Device
Main
Memory
For Educational Purpose Only Not be circulated without this banner
Output
Device
Starting Out with C++, 3rd Edition
Figure 1.2
Instruction
(Input)
Arithmetic
and Logic
Unit
Result
(Output)
For Educational Purpose Only Not be circulated without this banner
Control Unit
Starting Out with C++, 3rd Edition
Software
Operating Systems
Single tasking
Multi-tasking
For Educational Purpose Only Not be circulated without this banner
Application Software
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
For Educational Purpose Only Not be circulated without this banner
language is a special language used to write
computer programs.
Starting Out with C++, 3rd Edition
Program 1-1
For
// This program calculates the users pay.
#include <iostream.h>
void main(void)
{
float hours, rate, pay;
Educational Purpose
Onlymany
Not be
circulated
this
cout << How
hours
did you without
work? ;
cin >> hours;
cout << How much do you get paid per
hour? ;
cin >> rate;
pay = hours * rate;
cout << You have earned $ << pay <<
endl;
}
banner
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
For Educational Purpose Only Not be circulated without this banner
10
Starting Out with C++, 3rd Edition
Programming Languages
Figure 1-4
High level
(Close to Human
Language)
For Educational Purpose Only Not be circulated without this banner
Low level
(Machine Language)
11
Starting Out with C++, 3rd Edition
Table 1-1
Language
BASIC
FORTRAN
COBOL
Description
Beginners All-purpose Symbolic Instruction Code.
A general programming language originally
designed to be simple enough for beginners to learn.
Formula Translator. A language designed for
programming complex mathematical algorithms.
Common Business-Oriented Language. A language
designed for business applications.
A structured, general purpose language designed
primarily for teaching programming.
A structured, general purpose language developed at
Bell Labs. C offers both high-level and low-level
features.
Based on the C language, C++ offers object-oriented
features not found in C. Also invented at Bell
Laboratories.
An object-oriented language invented at Sun
Microsystems. Java may be used to develop
programs that run over the Internet, in a web
browser.
For Educational Purpose
Only Not be circulated without this banner
Pascal
C
C++
Java
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
For Educational
Purpose Only NotSymbols
be circulated without this banner
Operators
Punctuation
13
Starting Out with C++, 3rd Edition
Language Elements, Table 1-2
Language
Element
Key Words
Description
Words that have a special meaning. Key
words may only be used for their intended
purpose.
Programmer- Words or names defined by the programmer.
Defined
They are symbolic names that refer to
Symbols
variables or programming routines.
Operators
Operators perform operations on one or more
operands. An operand is usually a piece of
data, like a number.
Punctuation Punctuation characters that mark the
beginning or ending of a statement, or
separate items in a list.
Syntax
Rules that must be followed when
construction a program. Syntax dictates how
key words and operators may be used, and
where punctuation symbols must appear.
For Educational Purpose Only Not be circulated without this banner
14
Starting Out with C++, 3rd Edition
Lines and Statements
cout << How many hours did you work?;
For Educational Purpose Only Not be circulated without this banner
15
Starting Out with C++, 3rd Edition
Variables
A storage location in the computers
memory for holding a piece of
information.
For Educational
Purpose Only Not be circulated without this banner
Symbolic names that represent locations
in the computers 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
For Educational Purpose Only Not be circulated without this banner
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:
For Educational
Purpose Only Not be circulated without this banner
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
debugging
activities.
For Educational
Purpose
Onlyand
Not be
circulated without
this banner
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.
For Educational
Purpose Only Not be circulated without this banner
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.
For Educational
Purpose
Onlyand
Not compile
be circulated
9. Enter
the code
it. without this banner
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
For Educational9Purpose
Only
circulated
through
12Not
as be
many
timeswithout
as this banner
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
aboutOnly
software
and
For Educational
Purpose
Not bedevelopment
circulated without
this banner
program design.
23
Starting Out with C++, 3rd Edition