CH 02
CH 02
Design
Fourth Edition
Chapter 2
Input, Processing, and
Output
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Learning Objectives
2.1 Designing a Program
2.2 Output, Input, and Variables
2.3 Variable Assignment and Calculations
2.4 Variable Declarations and Data Types
2.5 Named Constants
2.6 Hand Tracing a Program
2.7 Documenting a Program
2.8 Designing Your First Program
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (1 of 7)
1. The first step in programming is designing – flowcharts
and pseudocode help with this process.
2. Next, the code is written.
3. All code must be cleared of all syntax errors.
4. After the executable is created, it can be checked for
logic errors.
5. If logic errors exist, the program must be debugged.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (2 of 7)
The purpose of Programming Logic and Design is to focus
on Flowcharts and Pseudocode.
The design is the foundation of a good program.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (3 of 7)
Two steps in designing a program
1. Understand the tasks that the program is to perform.
– Learning what the customer wants.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (5 of 7)
Flowcharts Figure 2.2 Flowchart for the pay
calculating program
• A diagram that graphically
depicts the steps that take
place in a program
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (6 of 7)
• Flowchart Connector Symbol
– Use connectors to break
a flowchart into two or
more smaller flowcharts,
and placing them side-
by-side on the page.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (7 of 7)
• Off-Page Connector Symbol
– To connect flowcharts on different pages
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (4 of 7)
Pseudocode
• Fake code used as a model for programs
• No syntax rules
• Well written pseudocode can be easily translated to
actual code
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.2 Output, Input, and Variables (1 of 6)
Output – data that is generated and displayed
Input – data that a program receives
Variables – storage locations in memory for data
Computer programs typically follow 3 steps
1. Input is received
2. Some process is performed on the input
3. Output is produced
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.2 Output, Input, and Variables (2 of 6)
Input, Processing, and Output of a Pay Calculating
program:
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.2 Output, Input, and Variables (3 of 6)
IPO Chart: Describes the input, processing, and output of a
program.
Example:
IPO Chart for the Pay Calculating Program
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.2 Output, Input, and Variables (4 of 6)
Display is the keyword to show output to the screen
Sequence – lines execute in the order they appear
String Literals – a sequence of characters
Figure 2-7 The statements Figure 2-8 Output of
execute in order Program 2-1
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.2 Output, Input, and Variables (5 of 6)
Input is the keyword to take values from the user of the
program
It is usually stored in variables
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.2 Output, Input, and Variables (6 of 6)
Programmers can define variable names following certain
rules
– Must be one word, no spaces
– Generally, punctuation characters are avoided
– Generally, the first character cannot be a number
– Name a variable something that indicates what may
be stored in it
camelCase is popular naming convention
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.3 Variable Assignment & Calculations (1 of 2)
Variable assignment does not always have to come from
user input, it can also be set through an assignment
statement
Set price = 20
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.3 Variable Assignment & Calculations (2 of 2)
Calculations are performed using math operators
The expression is normally stored in variables
Set sale = price – discount
Table 2-1 Common math operators
Symbol Operator Description
+ Addition Adds two numbers
− Subtraction Subtracts one number from another
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.4 Variable Declarations & Data Types (1 of 2)
A variable declaration includes a variable’s name and a
variable’s data type
Data Type – defines the type of data you intend to store in
a variable
– Integer – stores only whole numbers
– Real – stores whole or decimal numbers
– String – any series of characters
• Declare Real grossPay
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.4 Variable Declarations & Data Types (2 of 2)
For safety and to avoid logic errors, variables should be
initialized to 0 or some other value
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.5 Named Constants
A named constant is a name that represents a value that
cannot be changed
– Makes programs more self explanatory
– If a change to the value occurs, it only has to be
modified in one place
Constant Real INTEREST_RATE = 0.069
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.6 Hand Tracing a Program
Hand tracing is a simple debugging process for locating
hard to find errors in a program
Involves creating a chart with a column for each variable,
and a row for each line of code
Figure 2-18 Program with the hand trace chart completed
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.7 Documenting a Program
External documentation describes aspects of the
program for the user, sometimes written by a technical
writer
Internal documentation explains how parts of the
program works for the programmer, also known as
comments
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.8 Designing Your First Program (1 of 5)
Calculate the batting average for any player
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.8 Designing Your First Program (2 of 5)
1. Input is received.
– The number of hits
– The number of times at bat
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.8 Designing Your First Program (3 of 5)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.8 Designing Your First Program (4 of 5)
Figure 2-20 Flowchart for program 2-15
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.8 Designing Your First Program (5 of 5)
• Summary
– Input
▪ Determine data needed for input
▪ Choose variables to store the input
– Process
▪ Determine calculations to be performed
▪ Choose variables to store the calculations
– Output
▪ Determine what output the program will display
▪ Usually the results of the program’s calculations
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Examples 1
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exampl 1 …… continued
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 3
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 5
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 7
• Write a program that displays the following
pieces of information, each on a separate line:
• Your name
• Your address, with city, state, and ZIP code
• Your telephone number
• Your college major
• Use only a single cout statement to display all
of this information.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved