Input, Output and Algorithms
Input, Output and Algorithms
Design
Fifth 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
2.9 Focus on Languages: Java, Python, and C++
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 (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.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.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
2.9 Focus on Languages: Java
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (1 of 11)
• Setting up a Java Program
Class Name
{
public static void main(String[] args)
{
}
}
main Method
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (2 of 11)
• Displaying Screen Output
– System.out.println()
Displays a line of output, then advances the output cursor to the next line.
Program Output
My major is Computer Science.
I plan to be a software developer.
Programming is fun!
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (3 of 11)
• Displaying Screen Output
– System.out.print()
Displays output, but does not advance the output cursor to the next line.
Program Output
Programmingisfun!
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (4 of 11)
• Variable Declarations (see the rules for variable names on page 76 of your book)
int speed;
double distance;
String name;
Data Type What It Can Hold
byte Integers in the range of –128 to +127
short Integers in the range of –32,768 to +32,767
int Integers in the range of –2,147,483,648 to +2,147,483,647
float Floating-point numbers in the range of ±3.4×10–38 to ±3.4×1038 , with 7 digits of accuracy
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (4 of 11)
• Variable Declarations (see the rules for variable names on page 76 of your book)
int speed;
double distance;
String name;
Data Type What It Can Hold
byte Integers in the range of -128 to +128
short Integers in the range of -32,768 to +32,767
int Integers in the range of -2,147,483,648 to +2,147,483,647
long Integers in the range of –9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807
float Floating-point numbers in the range of ±3.4 * 10-38 to ±3.4 * 1038, with 7 digits of
accuracy
double Floating-point numbers in the range of ±1.7 * 10-308 to ±1.7 * 10308, with 15 digits of
accuracy
String Strings of text
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (5 of 11)
• Reading Keyboard Input (reading an integer)
import java.util.Scanner;
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (6 of 11)
• Reading Keyboard Input (reading a real number)
import java.util.Scanner;
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (7 of 11)
• Reading Keyboard Input (reading a string)
import java.util.Scanner;
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (8 of 11)
• Displaying Multiple Items with the + Operator
Example:
Example:
number = 5;
System.out.println("The value is " + number);
The value is 5
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (9 of 11)
• Performing Calculations – Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Examples:
total = price + tax;
sale = price - discount;
population = population * 2;
half = number / 2;
leftOver = 17 % 3;
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (10 of 11)
• Named constants – use the final key word in declarations:
This declares a double constant named INTEREST_RATE, set to the value 0.069.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (11 of 11)
• Documenting a program with comments
/*
This is a multiline comment.
*/
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (1 of 8)
• Displaying Screen Output
– print() function
Displays a line of output, then advances the output cursor to the next line.
Program Output
My major is Computer Science.
I plan to be a software developer.
Programming is fun!
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (2 of 8)
• You do not declare variables in Python.
age = 25
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (3 of 8)
• Pass multiple arguments to the print function, and Python will print each argument's
value on the screen, separated by a space.
age = 25
print('I am', age, 'years old.');
Program Output
I am 25 years old.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (4 of 8)
• Reading Keyboard Input (reading a string)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (5 of 8)
• Reading Keyboard Input (reading an integer)
hours = int(input('How many hours did you work? '))
print('Here is the value that you entered:', hours)
Program Output
How many hours did you work? 40 [Enter]
Here is the value that you entered: 40
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (6 of 8)
• Reading Keyboard Input (reading a real number)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (7 of 8)
• Performing Calculations – Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
**Exponent
Examples:
total = price + tax
sale = price - discount
population = population * 2
half = number / 2
leftOver = 17 % 3
result = 4**2
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (8 of 8)
• Documenting a program with comments
# This is a comment
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (1 of 11)
• The typical C++ program contains the following code:
#include <iostream>
using namespace std;
int main()
{
You will write statements that appear in this area.
return 0;
}
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (2 of 11)
• Displaying Screen Output with the cout Statement
– Begins with the word cout
– followed by the << operator
– followed by an item of data that is to be displayed.
– The statement ends with a semicolon.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
return 0;
}
Program Output
Hello world
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (3 of 11)
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
cout << "Hello world"; cout << "Hello " << "world";
return 0; return 0;
} }
Program Output Program Output
Hello world Hello world
int speed;
double distance;
string name;
Data Type What It Can Hold
short Integers in the range of -32,768 to +32,767
int Integers in the range of -2,147,483,648 to +2,147,483,647
long Integers in the range of -2,147,483,648 to +2,147,483,647
float Floating-point numbers in the range of ±3.4 * 10 -38 to ±3.4 * 1038, with 7 digits of
accuracy
double Floating-point numbers in the range of ±1.7 * 10 -308 to ±1.7 * 10308, with 15 digits of
accuracy
char Can store integers in the range of -128 to +127. Typically used to store characters.
string Strings of text
bool Stores the values true or false
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (5 of 11)
• Reading Keyboard Input (reading an integer)
#include <iostream>
using namespace std;
int main()
{
int age;
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
double payRate;
int hours;
cout << "Here are the values that you entered:" << endl;
cout << name << endl;
cout << payRate << endl;
cout << hours << endl;
return 0;
}
Program Output
Enter your first name.
Connie [Enter]
Enter your hourly pay rate.
55.25 [Enter]
Enter the number of hours worked.
40 [Enter]
Here are the values that you entered:
Connie
55.25
40
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (7 of 11)
• Using getline To Read String Input Containing Spaces
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
Program Output
Please enter your name.
Kate Smith [Enter]
Hello, Kate Smith
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (8 of 11)
• Performing Calculations – Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Examples:
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (9 of 11)
• Named constants – use the const key word in declarations:
This declares a double constant named INTEREST_RATE, set to the value 0.069.
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (10 of 11)
• Documenting a program with comments
/*
This is a multiline comment.
*/
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Copyright
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved