0% found this document useful (0 votes)
7 views35 pages

CH 02

Chapter 2 of 'Starting Out with Programming Logic and Design' focuses on the fundamentals of program design, including the use of flowcharts and pseudocode. It covers key concepts such as input, processing, output, variable declarations, data types, and named constants, as well as debugging techniques and documentation. The chapter also provides examples and exercises to help readers design their first programs effectively.

Uploaded by

hameedshaibu99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

CH 02

Chapter 2 of 'Starting Out with Programming Logic and Design' focuses on the fundamentals of program design, including the use of flowcharts and pseudocode. It covers key concepts such as input, processing, output, variable declarations, data types, and named constants, as well as debugging techniques and documentation. The chapter also provides examples and exercises to help readers design their first programs effectively.

Uploaded by

hameedshaibu99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Starting out with Programming Logic and

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.

Figure 2-1 The program development cycle

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.

2. Determine the steps that must be taken to perform the


task.
– Create an algorithm, or step-by-step directions to
solve the problem.
– Use flowcharts and/or pseudocode to solve.

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

Input Processing Output


Number of Multiply the number of hours Gross pay
hours worked by the hourly pay rate.
worked The result is the gross pay.

Hourly pay rate

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

Asterisk Multiplication Multiplies one number by another


/ Division Divides one number by another and gives the quotient
MOD Modulus Divides one number by another and gives the remainder
Caret Exponent Raises a number to a power

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

Batting Average Hits Times at Bat

Determine what is required for each phase of the program:


1. What must be read as input?
2. What will be done with the input?
3. What will be the output?

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

2. Some process is performed on the input.


– Calculate the batting average
– Divide the number of hits by the number of times at
bat
3. Output is produced.
– The player’s batting average

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

• The following pseudocode algorithm has


an error. The program is supposed to ask
• the user for the length and width of a
rectangular room, and then display the
room’s area. The program must multiply
the width by the length in order to
determine the area. Find the error.

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Exampl 1 …… continued

• area width length.


• Display “What is the room’s width?”.
• Input width.
• Display “What is the room’s length?”.
• Input length.
• Display area.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 2
• Design a hierarchy chart or flowchart for a program that
calculates the total of a retail sale. The program should
ask the user for:
• – The retail price of the item being purchased
• – The sales tax rate
• Once these items have been entered, the program
should calculate and display:
• – The sales tax for the purchase
• – The total of the sale

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 3

• The variable j starts with the value 10.


• The variable k starts with the value 2.
• The variable l starts with the value 4.
• Store the value of j times k in j .
• Store the value of k times l in l .
• Add j and l , and store the result in k .
• Display the value in k on the screen.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 4
• A soft drink company recently surveyed 16,500 of its
customers and found that approximately 15 percent of
those surveyed purchase one or more energy drinks per
week. Of those customers who purchase energy drinks,
approximately 58 percent of them prefer citrus-flavored
energy drinks. Write a program that displays the
following:
• • The approximate number of customers in the survey
who purchase one or more energy drinks per week • The
approximate number of customers in the survey who
prefer citrus-flavored energy drinks

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 5

• One acre of land is equivalent


to 43,560 square feet. Write a
program that calculates
• the number of acres in a tract
of land with 391,876 square
feet.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Example 6
• A car with a 20-gallon gas tank averages 23.5 miles per
gallon when driven in town
• and 28.9 miles per gallon when driven on the highway.
Write a program that calculates
• and displays the distance the car can travel on one tank
of gas when driven in town
• and when driven on the highway.
• Hint: The following formula can be used to calculate the
distance:
• Distance Number of Gallons Average Miles per Gallon

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

You might also like