KEMBAR78
ch02-IPO-Input-Process-Output-Input-Process-Output | PDF
Starting out with Programming Logic and
Design
Sixth Edition
Chapter 2
Input, Processing, and
Output
Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Copyright © 2022, 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 © 2022, 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 © 2022, 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.
The program development cycle
Copyright © 2022, 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 © 2022, 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
Display "Enter the number of hours"
Input hours
Display "Enter the hourly pay rate"
Input payRate
Set grossPay = hours * payRate
Display "The gross pay is $", grossPay
Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.1 Designing a Program (5 of 7)
Flowcharts
• A diagram that graphically
depicts the steps that take
place in a program
Figure 2-2 Flowchart for the pay
calculating program
Copyright © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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
hours
worked
Hourly pay rate
Multiply the number of hours
worked by the hourly pay rate.
The result is the gross pay.
Gross pay
Copyright © 2022, 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
execute in order
Figure 2-8 Output of
Program 2-1
Copyright © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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 © 2022, 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
// Comments are short notes placed in a program.
// They explain how different parts of the
// program work.
Copyright © 2022, 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 © 2022, 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.8 Designing Your First Program (3 of 5)
Copyright © 2022, 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 © 2022, 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java
Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Java (1 of 11)
• Setting up a Java Program
public class Simple
{
public static void main(String[] args)
{
}
}
Class Name
Class Header
main Method
Copyright © 2022, 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.
public class Output
{
public static void main(String[] args)
{
System.out.println("My major is Computer Science.");
System.out.println("I plan to be a software developer.");
System.out.println("Programming is fun!");
}
}
Program Output
My major is Computer Science.
I plan to be a software developer.
Programming is fun!
Copyright © 2022, 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.
public class Output2
{
public static void main(String[] args)
{
System.out.print("Programming");
System.out.print("is");
System.out.print("fun!");
}
}
Program Output
Programmingisfun!
Copyright © 2022, 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
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 © 2022, 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 © 2022, 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;
public class GetAge
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int age;
System.out.println("What is your age?");
age = keyboard.nextInt();
System.out.println("Here is the value that you entered:");
System.out.println(age);
}
}
Program Output
What is your age?
24 [Enter]
Here is the value that you entered:
24
Create a Scanner object
Read an integer
Copyright © 2022, 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;
public class GetAge
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double temp;
System.out.println("What is the temperature?");
temp = keyboard.nextDouble();
System.out.println("Here is the value that you entered:");
System.out.println(temp);
}
}
Program Output
What is the temperature?
74.5 [Enter]
Here is the value that you entered:
74.5
Create a Scanner object
Read a double
Copyright © 2022, 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;
public class GetAge
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String name;
System.out.println("What is your name?");
name = keyboard.nextLine();
System.out.println("Here is the value that you entered:");
System.out.println(name);
}
}
Program Output
What is your name?
Jimmy [Enter]
Here is the value that you entered:
Jimmy
Create a Scanner object
Read a String
Copyright © 2022, 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:
System.out.println("This is " + "one string.");
This statement will display:
This is one string.
Example:
number = 5;
System.out.println("The value is " + number);
This code will display:
The value is 5
Copyright © 2022, 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 © 2022, 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:
final double INTEREST_RATE = 0.069;
This declares a double constant named INTEREST_RATE, set to the value 0.069.
Copyright © 2022, 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 line comment
/*
This is a multiline comment.
*/
Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python
Copyright © 2022, 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.
print('My major is Computer Science.');
print('I plan to be a software developer.');
print('Programming is fun!');
Program Output
My major is Computer Science.
I plan to be a software developer.
Programming is fun!
Copyright © 2022, 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.
• Instead, you use an assignment statement to create a variable.
• See the rules for variable names on page 81 of your book.
age = 25
title = 'Vice President'
Copyright © 2022, 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: Python (4 of 8)
• Reading Keyboard Input (reading a string)
first_name = input('Enter your first name: ')
last_name = input('Enter your last name: ')
print('Hello', first_name, last_name)
Program Output
Enter your first name: Vinny [Enter]
Enter your last name: Brown [Enter]
Hello Vinny Brown
Copyright © 2022, 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 © 2022, 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)
pay_rate = float(input('What is your hourly pay rate? '))
print('Here is the value that you entered:', pay_rate)
Program Output
What is your hourly pay rate? 20 [Enter]
Here is the value that you entered: 20
Copyright © 2022, 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 © 2022, 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++
Copyright © 2022, 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()
{
return 0;
}
You will write statements that appear in this area.
Copyright © 2022, 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (3 of 11)
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
return 0;
}
Program Output
Hello world
#include <iostream>
using namespace std;
int main()
{
cout << "Hello " << "world";
return 0;
}
Program Output
Hello world
#include <iostream>
using namespace std;
int main()
{
cout << "Hello ";
cout << "world";
return 0;
}
Program Output
Hello world
#include <iostream>
using namespace std;
int main()
{
cout << "Hello" << endl;
cout << "world" << endl;
return 0;
}
Program Output
Hello
world
Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
2.9 Focus on Languages: C++ (4 of 11)
• Variable Declarations (see the rules for variable names on page 87 of your book)
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 © 2022, 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;
cout << "What is your age?" << endl;
cin >> age;
cout << "Here is the value that you entered:" << endl;
cout << age;
return 0;
}
Program Output
What is your age?
24 [Enter]
Here is the value that you entered:
24
Read a value into the age variable
Copyright © 2022, 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 << "Enter your first name." << endl;
cin >> name;
cout << "Enter your hourly pay rate." << endl;
cin >> payRate;
cout << "Enter the number of hours worked." << endl;
cin >> 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 © 2022, 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;
cout << "Please enter your name." << endl;
getline(cin, name);
cout << "Hello, " << name << endl;
return 0;
}
Program Output
Please enter your name.
Kate Smith [Enter]
Hello, Kate Smith
Copyright © 2022, 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:
total = price + tax;
sale = price - discount;
population = population * 2;
half = number / 2;
leftOver = 17 % 3;
Copyright © 2022, 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:
const double INTEREST_RATE = 0.069;
This declares a double constant named INTEREST_RATE, set to the value 0.069.
Copyright © 2022, 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 line comment
/*
This is a multiline comment.
*/
Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Copyright

ch02-IPO-Input-Process-Output-Input-Process-Output

  • 1.
    Starting out withProgramming Logic and Design Sixth Edition Chapter 2 Input, Processing, and Output Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
  • 2.
    Copyright © 2022,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++
  • 3.
    Copyright © 2022,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.
  • 4.
    Copyright © 2022,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. The program development cycle
  • 5.
    Copyright © 2022,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.
  • 6.
    Copyright © 2022,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 Display "Enter the number of hours" Input hours Display "Enter the hourly pay rate" Input payRate Set grossPay = hours * payRate Display "The gross pay is $", grossPay
  • 7.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.1 Designing a Program (5 of 7) Flowcharts • A diagram that graphically depicts the steps that take place in a program Figure 2-2 Flowchart for the pay calculating program
  • 8.
    Copyright © 2022,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.
  • 9.
    Copyright © 2022,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
  • 10.
    Copyright © 2022,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
  • 11.
    Copyright © 2022,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:
  • 12.
    Copyright © 2022,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 hours worked Hourly pay rate Multiply the number of hours worked by the hourly pay rate. The result is the gross pay. Gross pay
  • 13.
    Copyright © 2022,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 execute in order Figure 2-8 Output of Program 2-1
  • 14.
    Copyright © 2022,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
  • 15.
    Copyright © 2022,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
  • 16.
    Copyright © 2022,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
  • 17.
    Copyright © 2022,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 ∗ ∧
  • 18.
    Copyright © 2022,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
  • 19.
    Copyright © 2022,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
  • 20.
    Copyright © 2022,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
  • 21.
    Copyright © 2022,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
  • 22.
    Copyright © 2022,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 // Comments are short notes placed in a program. // They explain how different parts of the // program work.
  • 23.
    Copyright © 2022,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?
  • 24.
    Copyright © 2022,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
  • 25.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.8 Designing Your First Program (3 of 5)
  • 26.
    Copyright © 2022,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
  • 27.
    Copyright © 2022,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
  • 28.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.9 Focus on Languages: Java
  • 29.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.9 Focus on Languages: Java (1 of 11) • Setting up a Java Program public class Simple { public static void main(String[] args) { } } Class Name Class Header main Method
  • 30.
    Copyright © 2022,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. public class Output { public static void main(String[] args) { System.out.println("My major is Computer Science."); System.out.println("I plan to be a software developer."); System.out.println("Programming is fun!"); } } Program Output My major is Computer Science. I plan to be a software developer. Programming is fun!
  • 31.
    Copyright © 2022,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. public class Output2 { public static void main(String[] args) { System.out.print("Programming"); System.out.print("is"); System.out.print("fun!"); } } Program Output Programmingisfun!
  • 32.
    Copyright © 2022,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 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.
  • 33.
    Copyright © 2022,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
  • 34.
    Copyright © 2022,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; public class GetAge { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int age; System.out.println("What is your age?"); age = keyboard.nextInt(); System.out.println("Here is the value that you entered:"); System.out.println(age); } } Program Output What is your age? 24 [Enter] Here is the value that you entered: 24 Create a Scanner object Read an integer
  • 35.
    Copyright © 2022,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; public class GetAge { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double temp; System.out.println("What is the temperature?"); temp = keyboard.nextDouble(); System.out.println("Here is the value that you entered:"); System.out.println(temp); } } Program Output What is the temperature? 74.5 [Enter] Here is the value that you entered: 74.5 Create a Scanner object Read a double
  • 36.
    Copyright © 2022,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; public class GetAge { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String name; System.out.println("What is your name?"); name = keyboard.nextLine(); System.out.println("Here is the value that you entered:"); System.out.println(name); } } Program Output What is your name? Jimmy [Enter] Here is the value that you entered: Jimmy Create a Scanner object Read a String
  • 37.
    Copyright © 2022,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: System.out.println("This is " + "one string."); This statement will display: This is one string. Example: number = 5; System.out.println("The value is " + number); This code will display: The value is 5
  • 38.
    Copyright © 2022,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;
  • 39.
    Copyright © 2022,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: final double INTEREST_RATE = 0.069; This declares a double constant named INTEREST_RATE, set to the value 0.069.
  • 40.
    Copyright © 2022,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 line comment /* This is a multiline comment. */
  • 41.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.9 Focus on Languages: Python
  • 42.
    Copyright © 2022,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. print('My major is Computer Science.'); print('I plan to be a software developer.'); print('Programming is fun!'); Program Output My major is Computer Science. I plan to be a software developer. Programming is fun!
  • 43.
    Copyright © 2022,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. • Instead, you use an assignment statement to create a variable. • See the rules for variable names on page 81 of your book. age = 25 title = 'Vice President'
  • 44.
    Copyright © 2022,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.
  • 45.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.9 Focus on Languages: Python (4 of 8) • Reading Keyboard Input (reading a string) first_name = input('Enter your first name: ') last_name = input('Enter your last name: ') print('Hello', first_name, last_name) Program Output Enter your first name: Vinny [Enter] Enter your last name: Brown [Enter] Hello Vinny Brown
  • 46.
    Copyright © 2022,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
  • 47.
    Copyright © 2022,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) pay_rate = float(input('What is your hourly pay rate? ')) print('Here is the value that you entered:', pay_rate) Program Output What is your hourly pay rate? 20 [Enter] Here is the value that you entered: 20
  • 48.
    Copyright © 2022,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
  • 49.
    Copyright © 2022,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
  • 50.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.9 Focus on Languages: C++
  • 51.
    Copyright © 2022,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() { return 0; } You will write statements that appear in this area.
  • 52.
    Copyright © 2022,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
  • 53.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.9 Focus on Languages: C++ (3 of 11) #include <iostream> using namespace std; int main() { cout << "Hello world"; return 0; } Program Output Hello world #include <iostream> using namespace std; int main() { cout << "Hello " << "world"; return 0; } Program Output Hello world #include <iostream> using namespace std; int main() { cout << "Hello "; cout << "world"; return 0; } Program Output Hello world #include <iostream> using namespace std; int main() { cout << "Hello" << endl; cout << "world" << endl; return 0; } Program Output Hello world
  • 54.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 2.9 Focus on Languages: C++ (4 of 11) • Variable Declarations (see the rules for variable names on page 87 of your book) 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
  • 55.
    Copyright © 2022,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; cout << "What is your age?" << endl; cin >> age; cout << "Here is the value that you entered:" << endl; cout << age; return 0; } Program Output What is your age? 24 [Enter] Here is the value that you entered: 24 Read a value into the age variable
  • 56.
    Copyright © 2022,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 << "Enter your first name." << endl; cin >> name; cout << "Enter your hourly pay rate." << endl; cin >> payRate; cout << "Enter the number of hours worked." << endl; cin >> 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
  • 57.
    Copyright © 2022,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; cout << "Please enter your name." << endl; getline(cin, name); cout << "Hello, " << name << endl; return 0; } Program Output Please enter your name. Kate Smith [Enter] Hello, Kate Smith
  • 58.
    Copyright © 2022,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: total = price + tax; sale = price - discount; population = population * 2; half = number / 2; leftOver = 17 % 3;
  • 59.
    Copyright © 2022,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: const double INTEREST_RATE = 0.069; This declares a double constant named INTEREST_RATE, set to the value 0.069.
  • 60.
    Copyright © 2022,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 line comment /* This is a multiline comment. */
  • 61.
    Copyright © 2022,2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved Copyright