SlideShare a Scribd company logo
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++
Eighth Edition
Chapter 4:
Completing the Problem-Solving Process
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Get numeric and character data from the keyboard
• Display information on the computer screen
• Write arithmetic expressions
• Type cast a value
• Write an assignment statement
• Code the algorithm into a program
• Desk-check a program
• Evaluate and modify a program
An Introduction to Programming with C++, Eighth Edition
Objectives
2
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• The fourth step in the problem-solving process is to
code algorithm into a program
• Begin by declaring a memory location for each input,
processing, and output value in IPO chart
• Optionally initialize each value (highly preferred)
• Next, you code the instructions for the algorithm
An Introduction to Programming with C++, Eighth Edition
Finishing Step 4 in the Problem-Solving
Process
3
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-1 Problem specification, IPO chart
information, and variable declaration
4
Finishing Step 4 in the Problem-Solving
Process (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• C++ uses stream objects to perform input/output
operations
• A stream is a sequence of characters
• The cin object is used to obtain information from the
keyboard (program pauses while user enters data)
• The extraction operator (>>) takes information out of
cin object and stores it in internal memory
– Syntax: cin >> variableName;
An Introduction to Programming with C++, Eighth Edition
Getting Data from the Keyboard
5
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Getting Data from the Keyboard (cont’d.)
Figure 4-2 Relationship among the keyboard, cin object,
extraction operator, and internal memory
6
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Getting Data from the Keyboard (cont’d.)
Figure 4-3 How to use cin and >> to
get numeric or character data
7
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-4 Input statements for
the Addison O’Reilly problem
8
Getting Data from the Keyboard (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• You use a prompt (message) to let the user know what
data is to be entered
• The cout object is used with the insertion operator
(<<) to display information on the screen
• Information can be any combination of literal constants,
named constants, and variables
• Multiple items can be printed in the same statement
– Syntax: cout << item1 [<< item2 << itemN];
– Part in brackets is optional
An Introduction to Programming with C++, Eighth Edition
Displaying Messages on the Computer
Screen
9
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• A stream manipulator is used to manipulate (manage)
the characters in an input or output string
• endl is a stream manipulator that advances the cursor
to the next line on the screen
– Equivalent to pressing the Enter key
(carriage return and line feed)
An Introduction to Programming with C++, Eighth Edition
Displaying Messages on the Computer
Screen (cont’d.)
10
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Displaying Messages on the Computer
Screen (cont’d.)
Figure 4-5 How to use the cout object
11
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-6 Prompts and output statement
for the Addison O’Reilly problem
12
Displaying Messages on the Computer
Screen (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• You can evaluate arithmetic expressions in C++ using
arithmetic operators
• Operators are negation (-), addition (+), subtraction
(-), multiplication (*), division (/), and modulus (%)
• Negation and subtraction use the same symbol, but
negation is a unary operator (one operand) and
subtraction is a binary operator (two operands)
• Modulus gives remainder when dividing two integers
An Introduction to Programming with C++, Eighth Edition
Arithmetic Operators in C++
13
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Each operator has a precedence: determines in which
order operators in an expression are evaluated
• Operators with lower-precedence numbers are
evaluated before higher ones
• Parentheses have lowest-precedence number, so they
can be used to override precedence order
• Operators with the same precedence number are
evaluated from left to right
An Introduction to Programming with C++, Eighth Edition
Arithmetic Operators in C++ (cont’d.)
14
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-7 Standard arithmetic operators and their order of precedence
15
Arithmetic Operators in C++ (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-8 Expressions containing more than one
operator having the same precedence
16
Arithmetic Operators in C++ (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Recall that the compiler will implicitly promote or
demote data types to match when possible
• Sometimes it is necessary to explicitly cast from one
data type into another
– Example: dividing two integers gives the result of integer
division (no remainder), but you would really like a
double result
– If one or both of the integers is a literal, you can cast it to
a double by adding .0 to the end of it
– If both are variables, you must use the static_cast
operator
An Introduction to Programming with C++, Eighth Edition
Type Conversions in Arithmetic
Expressions
17
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-9 Examples of expressions that
require implicit type conversions
18
Type Conversions in Arithmetic
Expressions (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Used to explicitly convert data from one data type to
another
• Called an explicit type conversion or type cast
• Syntax: static_cast<dataType>(data)
– data can be a literal constant, named constant, or
variable
– dataType is the data type to which you want the data
converted
An Introduction to Programming with C++, Eighth Edition
The static_cast Operator
19
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-10 How to use the static_cast operator
20
The static_cast Operator (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-10 How to use the static_cast operator (cont’d.)
21
The static_cast Operator (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• You use an assignment statement to assign a value to a
variable while a program is running
• Syntax: variableName = expression
– The = symbol is the assignment operator
• Tells computer to evaluate expression on right side of
assignment operator and store result in variable on left
side of the operator
– expression can include one or more literal constants,
named constants, variables, or arithmetic operators
An Introduction to Programming with C++, Eighth Edition
Assignment Statements
22
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Data type of expression in an assignment statement
must match data type of the variable
• If they don’t match, compiler will use implicit type
casting to get them to match
– Doesn’t always produce correct result
– Better to explicitly cast to correct data type yourself
• Remember:
– Declaration statement creates a new variable
– Assignment statement assigns a new value to an existing
variable
An Introduction to Programming with C++, Eighth Edition
Assignment Statements (cont’d.)
23
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Assignment Statements (cont’d.)
Figure 4-11 How to write an assignment statement
24
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Figure 4-12 Calculation statements
for the Addison O’Reilly problem
25
Assignment Statements (cont’d.)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Allow you to abbreviate assignment statements that
contain an arithmetic operator
• Statement must be of the form variableName =
variableName arithmeticOperator value
• Abbreviated as variableName arithmeticOperator =
value
– Example: price = price*1.05; can be abbreviated
as price *= 1.05;
• Most common operators are += , -= , *= , /= , and
%=
An Introduction to Programming with C++, Eighth Edition
Arithmetic Assignment Operators
26
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition 27
Figure 4-13 How to use an arithmetic assignment operator
Arithmetic Assignment Operators (cont’d)
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Fifth step is to desk-check the program to make sure
instructions were translated correctly
• You should desk-check the program using sample data
used to desk-check the algorithm
• Results of both desk-checks should be the same
• First, place names of the declared memory locations in
a new desk-check table along with each memory
location’s initial value
• Next, desk-check remaining C++ instructions in order,
recording any changes made to the variables
An Introduction to Programming with C++, Eighth Edition
Step 5–Desk-Check the Program
28
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Step 5–Desk-Check the Program (cont’d.)
Figure 4-15 Variable names and initial values entered in the
program’s desk-check table
Figure 4-14 Algorithm’s desk-check table from Chapter 2
29
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Step 5–Desk-Check the Program (cont’d.)
Figure 4-16 Input values entered in the program’s desk-check table
Figure 4-17 Sales tax amount entered in the desk-check table
30
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Step 5–Desk-Check the Program (cont’d.)
Figure 4-18 Cost amount entered in the desk-check table
Figure 4-19 Program’s desk-check table showing the
results of the second desk-check
31
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Final step in the problem-solving process
• You evaluate a program by running the program on the
computer and entering the sample data used when
desk-checking the program
• If evaluation reveals errors (known as bugs), they must
be fixed
• Process of locating and fixing errors is called debugging
• Two types of bugs: syntax errors and logic errors
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
32
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Syntax errors result from breaking programming
language’s rules; cause compiler errors
• Logic errors don’t cause compiler errors; can be hard to
identify
– Example: entering instructions in the wrong order
• Need a text editor to enter C++ instructions
• Instructions are called source code and are saved in
source files with extension .cpp
• Need a compiler to translate source code into machine
code (also called object code)
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
33
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Compiler saves object code in object files with extension
.obj
• Linker combines .obj files with other machine code
necessary to run the program and produces an
executable file with extension .exe
• An IDE (integrated development environment) is a
development tool that contains both an editor and
compiler
• A command-line compiler contains only the compiler and
requires a separate editor to enter source code
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
34
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
Figure 4-20 Process by which source code
is translated into executable code
35
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• A comment is a form of internal documentation; written
by placing // in front of the comment text
– Ignored by the compiler
– Considered good programming practice; makes code more
readable
• A #include directive allows you to merge the source
code in one file with that in another file
• The #include <iostream> is required when using
the cin or cout stream objects
– Not a statement, so no semicolon needed at the end
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
36
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• A using directive tells the compiler where in internal
memory it can find definitions of C++ keywords and
classes like double or string
• The using namespace std; directive indicates
that the definitions of the standard C++ keywords and
classes are located in the std (standard) namespace
– Is a statement, so semicolon required at the end
• A namespace is a special area in internal memory
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
37
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• A function is a block of code that performs a task
• Functions have parentheses following their name
(Example: main())
• Some functions require information between the
parentheses; others do not
• Every C++ program has one (and only one) main
function; this is where program execution begins
• Some functions return a value, and the data type they
return appears to the left of the function name
– Example: int main()
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
38
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Other functions do not return a value, and void
appears to the left of the function name
• The return type, name, and parameters (information in
parentheses) constitute the function header, which
marks the beginning of the function
• After the function header, you enter the function’s code
• You enclose a function’s code in a set of braces ({})
• The code between the braces is called the function body
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
39
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
Figure 4-21 Addison O’Reilly program
40
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Step 6–Evaluate and Modify the Program
(cont’d.)
Figure 4-22 Command Prompt window
41
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Fourth step in problem-solving process is coding the
algorithm into a program
• C++ uses stream objects for standard input/output
operations
• Use cin with extraction operator (>>) to get numeric
or character data from the keyboard
• Use cout with insertion operator (<<) to display
information on the screen
• The endl stream manipulator advances cursor to next
line
An Introduction to Programming with C++, Eighth Edition
Summary
42
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• You do calculations by writing arithmetic expressions
using arithmetic operators
• Each operator has a precedence: determines the order
of evaluation in an expression
• Parentheses are used to override this order
• Compiler implicitly casts data types when possible, but
you should explicitly cast to ensure correctness
• Use the static_cast operator to explicitly cast
variables from one data type to another
An Introduction to Programming with C++, Eighth Edition
Summary (cont’d.)
43
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• An assignment statement assigns a value to a variable
during runtime
• The expression on the right side of the assignment
operator (=) in an assignment statement is stored in the
variable on its left
• Fifth step of the problem-solving process is to desk-
check the program using the same data used to desk-
check the algorithm
• The sixth step is to evaluate and modify (if necessary)
the program
An Introduction to Programming with C++, Eighth Edition
Summary (cont’d.)
44
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Errors (called bugs) can either be syntax errors or logic
errors
• You need a text editor and compiler to enter C++
instructions and compile them into a program
• C++ instructions are called source code and are saved in
source files with the extension .cpp
• The compiler translates source code into machine code,
also called object code
• A linker produces an executable file that contains all
machine code necessary to run a C++ program
An Introduction to Programming with C++, Eighth Edition
Summary (cont’d.)
45
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Programmers use comments to document a program
internally
– Comments are not processed by the compiler
• The #include <filename> directive allows you to
include multiple source files in a program
• The using namespace std; directive tells the
compiler where definitions of standard C++ keywords
and classes are in internal memory
• A namespace is a special area in the computer’s internal
memory
An Introduction to Programming with C++, Eighth Edition
Summary (cont’d.)
46
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Execution of every C++ program begins with the main()
function
• The first line of a function is the function header
• The function body follows the header and is enclosed in
braces
• Some functions return a data type; others return void
• Arithmetic assignment operators can be used to
abbreviate certain assignment statements with
arithmetic operators in them
An Introduction to Programming with C++, Eighth Edition
Summary (cont’d.)
47
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
An Introduction to Programming with C++, Eighth Edition
Lab 4-1: Stop and Analyze
Figure 4-23 Examples for Lab 4-1
48
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
In Chapter 3’s Lab 3-2, you planned, created, and desk-
checked an algorithm that displays 10% commission on
a sales amount. Figure 4-24 shows the algorithm, along
with the input and output items and their
corresponding C++ statements. It also includes the
desk-check table you completed in Lab 3-2.
Figure 4-25 shows the completed IPO chart and C++
instructions for Lab 4-2.
Figure 4-27 shows the completed commission program.
An Introduction to Programming with C++, Eighth Edition
Lab 4-2: Plan and Create
49
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
Figure 4-27 Commission program
An Introduction to Programming with C++, Eighth Edition
Lab 4-2: Plan and Create (cont’d.)
50
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
• Modify the program in Lab 4-2 to allow the user to
enter the commission rate (in decimal form).
• Test the program twice. For the first test, use 1328.50
and .1 as the sales amount and commission rate. For
the second test, use 267.90 and .15.
An Introduction to Programming with C++, Eighth Edition
Lab 4-3: Modify
51
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
The program in this lab should calculate and display the
volume of a cylinder, given the cylinder’s radius (r) and
height (h), and using 3.14 as the value of pi.
An Introduction to Programming with C++, Eighth Edition
Lab 4-4: What’s Missing?
52
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
Desk-check the seven lines of code shown in Figure 4.28
below
An Introduction to Programming with C++, Eighth Edition
Lab 4-5: Desk-Check
53
v© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or
in part.
Following the instructions for Lab 4-6. The program should
calculate and display the area of a triangle, but it is not
working properly. Run and debug the program.
An Introduction to Programming with C++, Eighth Edition
Lab 4-6: Debug
54

More Related Content

What's hot

Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Salahaddin University-Erbil
 
How to execute a C program
How to execute a C  program How to execute a C  program
How to execute a C program
Leela Koneru
 
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
Kamran Zafar
 
Shortcuts & functions
Shortcuts & functionsShortcuts & functions
Shortcuts & functionsRichard Wood
 
Maximo 75 shortcuts
Maximo 75 shortcutsMaximo 75 shortcuts
Maximo 75 shortcuts
Jason Brock
 
C language
C languageC language
C language
marar hina
 
CS3251-_PIC
CS3251-_PICCS3251-_PIC
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detection
Võ Hòa
 
Structure c
Structure cStructure c
Structure c
thirumalaikumar3
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Deepak Singh
 
Grade 10 flowcharting
Grade 10  flowchartingGrade 10  flowcharting
Grade 10 flowcharting
Rafael Balderosa
 
C Programming
C ProgrammingC Programming
C Programming
Rumman Ansari
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
MOHAMAD NOH AHMAD
 
C programming presentation for university
C programming presentation for universityC programming presentation for university
C programming presentation for university
Sheikh Monirul Hasan
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
C programming Tutorial Session 1
C programming Tutorial Session 1C programming Tutorial Session 1
C programming Tutorial Session 1
Muhammad Ehtisham Siddiqui
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rokonuzzaman Rony
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
C Language
C LanguageC Language
C Language
Aakash Singh
 
Word exercises (1)
Word exercises (1)Word exercises (1)
Word exercises (1)
ruelcdogma
 

What's hot (20)

Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
How to execute a C program
How to execute a C  program How to execute a C  program
How to execute a C program
 
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
 
Shortcuts & functions
Shortcuts & functionsShortcuts & functions
Shortcuts & functions
 
Maximo 75 shortcuts
Maximo 75 shortcutsMaximo 75 shortcuts
Maximo 75 shortcuts
 
C language
C languageC language
C language
 
CS3251-_PIC
CS3251-_PICCS3251-_PIC
CS3251-_PIC
 
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detection
 
Structure c
Structure cStructure c
Structure c
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
 
Grade 10 flowcharting
Grade 10  flowchartingGrade 10  flowcharting
Grade 10 flowcharting
 
C Programming
C ProgrammingC Programming
C Programming
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C programming presentation for university
C programming presentation for universityC programming presentation for university
C programming presentation for university
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
C programming Tutorial Session 1
C programming Tutorial Session 1C programming Tutorial Session 1
C programming Tutorial Session 1
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
C Language
C LanguageC Language
C Language
 
Word exercises (1)
Word exercises (1)Word exercises (1)
Word exercises (1)
 

Similar to Chapter 4 - Completing the Problem-Solving Process

Chapter 9 Value-Returning Functions
Chapter 9 Value-Returning FunctionsChapter 9 Value-Returning Functions
Chapter 9 Value-Returning Functions
mshellman
 
Lesson 9.2 guessing the game program
Lesson 9.2 guessing the game programLesson 9.2 guessing the game program
Lesson 9.2 guessing the game program
MLG College of Learning, Inc
 
Lesson 9.1 value returning
Lesson 9.1 value returningLesson 9.1 value returning
Lesson 9.1 value returning
MLG College of Learning, Inc
 
Lesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulatorsLesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulators
MLG College of Learning, Inc
 
Chapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition StructureChapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition Structure
mshellman
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
MLG College of Learning, Inc
 
Lesson 8 more on repitition structure
Lesson 8 more on repitition structureLesson 8 more on repitition structure
Lesson 8 more on repitition structure
MLG College of Learning, Inc
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
MLG College of Learning, Inc
 
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
mshellman
 
Lesson 7.1 repitition structure
Lesson 7.1 repitition structureLesson 7.1 repitition structure
Lesson 7.1 repitition structure
MLG College of Learning, Inc
 
Lesson 6.1 more on selection structure
Lesson 6.1 more on selection structureLesson 6.1 more on selection structure
Lesson 6.1 more on selection structure
MLG College of Learning, Inc
 
Simulation lab
Simulation labSimulation lab
Simulation lab
Ezhilarasi Nagarajan
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.ppt
MalathyN6
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
madihamaqbool6
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
TanayKashid
 
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjdC+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
itzvenkatesh21
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
Terry Yoast
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 

Similar to Chapter 4 - Completing the Problem-Solving Process (20)

Chapter 9 Value-Returning Functions
Chapter 9 Value-Returning FunctionsChapter 9 Value-Returning Functions
Chapter 9 Value-Returning Functions
 
Lesson 9.2 guessing the game program
Lesson 9.2 guessing the game programLesson 9.2 guessing the game program
Lesson 9.2 guessing the game program
 
Lesson 9.1 value returning
Lesson 9.1 value returningLesson 9.1 value returning
Lesson 9.1 value returning
 
Lesson 6.2 logic error
Lesson  6.2 logic errorLesson  6.2 logic error
Lesson 6.2 logic error
 
Lesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulatorsLesson 7.2 using counters and accumulators
Lesson 7.2 using counters and accumulators
 
Chapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition StructureChapter 8 - More on the Repetition Structure
Chapter 8 - More on the Repetition Structure
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Lesson 8 more on repitition structure
Lesson 8 more on repitition structureLesson 8 more on repitition structure
Lesson 8 more on repitition structure
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
 
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
 
Lesson 7.1 repitition structure
Lesson 7.1 repitition structureLesson 7.1 repitition structure
Lesson 7.1 repitition structure
 
Lesson 6.1 more on selection structure
Lesson 6.1 more on selection structureLesson 6.1 more on selection structure
Lesson 6.1 more on selection structure
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
Simulation lab
Simulation labSimulation lab
Simulation lab
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.ppt
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
 
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjdC+for+beginners (2).pdf hsudiksbdjdidkdnjd
C+for+beginners (2).pdf hsudiksbdjdidkdnjd
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 

Recently uploaded

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 

Recently uploaded (20)

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 

Chapter 4 - Completing the Problem-Solving Process

  • 1. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++ Eighth Edition Chapter 4: Completing the Problem-Solving Process
  • 2. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Get numeric and character data from the keyboard • Display information on the computer screen • Write arithmetic expressions • Type cast a value • Write an assignment statement • Code the algorithm into a program • Desk-check a program • Evaluate and modify a program An Introduction to Programming with C++, Eighth Edition Objectives 2
  • 3. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • The fourth step in the problem-solving process is to code algorithm into a program • Begin by declaring a memory location for each input, processing, and output value in IPO chart • Optionally initialize each value (highly preferred) • Next, you code the instructions for the algorithm An Introduction to Programming with C++, Eighth Edition Finishing Step 4 in the Problem-Solving Process 3
  • 4. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-1 Problem specification, IPO chart information, and variable declaration 4 Finishing Step 4 in the Problem-Solving Process (cont’d.)
  • 5. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • C++ uses stream objects to perform input/output operations • A stream is a sequence of characters • The cin object is used to obtain information from the keyboard (program pauses while user enters data) • The extraction operator (>>) takes information out of cin object and stores it in internal memory – Syntax: cin >> variableName; An Introduction to Programming with C++, Eighth Edition Getting Data from the Keyboard 5
  • 6. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Getting Data from the Keyboard (cont’d.) Figure 4-2 Relationship among the keyboard, cin object, extraction operator, and internal memory 6
  • 7. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Getting Data from the Keyboard (cont’d.) Figure 4-3 How to use cin and >> to get numeric or character data 7
  • 8. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-4 Input statements for the Addison O’Reilly problem 8 Getting Data from the Keyboard (cont’d.)
  • 9. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • You use a prompt (message) to let the user know what data is to be entered • The cout object is used with the insertion operator (<<) to display information on the screen • Information can be any combination of literal constants, named constants, and variables • Multiple items can be printed in the same statement – Syntax: cout << item1 [<< item2 << itemN]; – Part in brackets is optional An Introduction to Programming with C++, Eighth Edition Displaying Messages on the Computer Screen 9
  • 10. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A stream manipulator is used to manipulate (manage) the characters in an input or output string • endl is a stream manipulator that advances the cursor to the next line on the screen – Equivalent to pressing the Enter key (carriage return and line feed) An Introduction to Programming with C++, Eighth Edition Displaying Messages on the Computer Screen (cont’d.) 10
  • 11. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Displaying Messages on the Computer Screen (cont’d.) Figure 4-5 How to use the cout object 11
  • 12. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-6 Prompts and output statement for the Addison O’Reilly problem 12 Displaying Messages on the Computer Screen (cont’d.)
  • 13. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • You can evaluate arithmetic expressions in C++ using arithmetic operators • Operators are negation (-), addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) • Negation and subtraction use the same symbol, but negation is a unary operator (one operand) and subtraction is a binary operator (two operands) • Modulus gives remainder when dividing two integers An Introduction to Programming with C++, Eighth Edition Arithmetic Operators in C++ 13
  • 14. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Each operator has a precedence: determines in which order operators in an expression are evaluated • Operators with lower-precedence numbers are evaluated before higher ones • Parentheses have lowest-precedence number, so they can be used to override precedence order • Operators with the same precedence number are evaluated from left to right An Introduction to Programming with C++, Eighth Edition Arithmetic Operators in C++ (cont’d.) 14
  • 15. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-7 Standard arithmetic operators and their order of precedence 15 Arithmetic Operators in C++ (cont’d.)
  • 16. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-8 Expressions containing more than one operator having the same precedence 16 Arithmetic Operators in C++ (cont’d.)
  • 17. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Recall that the compiler will implicitly promote or demote data types to match when possible • Sometimes it is necessary to explicitly cast from one data type into another – Example: dividing two integers gives the result of integer division (no remainder), but you would really like a double result – If one or both of the integers is a literal, you can cast it to a double by adding .0 to the end of it – If both are variables, you must use the static_cast operator An Introduction to Programming with C++, Eighth Edition Type Conversions in Arithmetic Expressions 17
  • 18. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-9 Examples of expressions that require implicit type conversions 18 Type Conversions in Arithmetic Expressions (cont’d.)
  • 19. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Used to explicitly convert data from one data type to another • Called an explicit type conversion or type cast • Syntax: static_cast<dataType>(data) – data can be a literal constant, named constant, or variable – dataType is the data type to which you want the data converted An Introduction to Programming with C++, Eighth Edition The static_cast Operator 19
  • 20. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-10 How to use the static_cast operator 20 The static_cast Operator (cont’d.)
  • 21. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-10 How to use the static_cast operator (cont’d.) 21 The static_cast Operator (cont’d.)
  • 22. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • You use an assignment statement to assign a value to a variable while a program is running • Syntax: variableName = expression – The = symbol is the assignment operator • Tells computer to evaluate expression on right side of assignment operator and store result in variable on left side of the operator – expression can include one or more literal constants, named constants, variables, or arithmetic operators An Introduction to Programming with C++, Eighth Edition Assignment Statements 22
  • 23. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Data type of expression in an assignment statement must match data type of the variable • If they don’t match, compiler will use implicit type casting to get them to match – Doesn’t always produce correct result – Better to explicitly cast to correct data type yourself • Remember: – Declaration statement creates a new variable – Assignment statement assigns a new value to an existing variable An Introduction to Programming with C++, Eighth Edition Assignment Statements (cont’d.) 23
  • 24. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Assignment Statements (cont’d.) Figure 4-11 How to write an assignment statement 24
  • 25. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Figure 4-12 Calculation statements for the Addison O’Reilly problem 25 Assignment Statements (cont’d.)
  • 26. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Allow you to abbreviate assignment statements that contain an arithmetic operator • Statement must be of the form variableName = variableName arithmeticOperator value • Abbreviated as variableName arithmeticOperator = value – Example: price = price*1.05; can be abbreviated as price *= 1.05; • Most common operators are += , -= , *= , /= , and %= An Introduction to Programming with C++, Eighth Edition Arithmetic Assignment Operators 26
  • 27. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition 27 Figure 4-13 How to use an arithmetic assignment operator Arithmetic Assignment Operators (cont’d)
  • 28. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Fifth step is to desk-check the program to make sure instructions were translated correctly • You should desk-check the program using sample data used to desk-check the algorithm • Results of both desk-checks should be the same • First, place names of the declared memory locations in a new desk-check table along with each memory location’s initial value • Next, desk-check remaining C++ instructions in order, recording any changes made to the variables An Introduction to Programming with C++, Eighth Edition Step 5–Desk-Check the Program 28
  • 29. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Step 5–Desk-Check the Program (cont’d.) Figure 4-15 Variable names and initial values entered in the program’s desk-check table Figure 4-14 Algorithm’s desk-check table from Chapter 2 29
  • 30. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Step 5–Desk-Check the Program (cont’d.) Figure 4-16 Input values entered in the program’s desk-check table Figure 4-17 Sales tax amount entered in the desk-check table 30
  • 31. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Step 5–Desk-Check the Program (cont’d.) Figure 4-18 Cost amount entered in the desk-check table Figure 4-19 Program’s desk-check table showing the results of the second desk-check 31
  • 32. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Final step in the problem-solving process • You evaluate a program by running the program on the computer and entering the sample data used when desk-checking the program • If evaluation reveals errors (known as bugs), they must be fixed • Process of locating and fixing errors is called debugging • Two types of bugs: syntax errors and logic errors An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program 32
  • 33. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Syntax errors result from breaking programming language’s rules; cause compiler errors • Logic errors don’t cause compiler errors; can be hard to identify – Example: entering instructions in the wrong order • Need a text editor to enter C++ instructions • Instructions are called source code and are saved in source files with extension .cpp • Need a compiler to translate source code into machine code (also called object code) An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) 33
  • 34. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Compiler saves object code in object files with extension .obj • Linker combines .obj files with other machine code necessary to run the program and produces an executable file with extension .exe • An IDE (integrated development environment) is a development tool that contains both an editor and compiler • A command-line compiler contains only the compiler and requires a separate editor to enter source code An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) 34
  • 35. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) Figure 4-20 Process by which source code is translated into executable code 35
  • 36. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A comment is a form of internal documentation; written by placing // in front of the comment text – Ignored by the compiler – Considered good programming practice; makes code more readable • A #include directive allows you to merge the source code in one file with that in another file • The #include <iostream> is required when using the cin or cout stream objects – Not a statement, so no semicolon needed at the end An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) 36
  • 37. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A using directive tells the compiler where in internal memory it can find definitions of C++ keywords and classes like double or string • The using namespace std; directive indicates that the definitions of the standard C++ keywords and classes are located in the std (standard) namespace – Is a statement, so semicolon required at the end • A namespace is a special area in internal memory An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) 37
  • 38. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • A function is a block of code that performs a task • Functions have parentheses following their name (Example: main()) • Some functions require information between the parentheses; others do not • Every C++ program has one (and only one) main function; this is where program execution begins • Some functions return a value, and the data type they return appears to the left of the function name – Example: int main() An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) 38
  • 39. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Other functions do not return a value, and void appears to the left of the function name • The return type, name, and parameters (information in parentheses) constitute the function header, which marks the beginning of the function • After the function header, you enter the function’s code • You enclose a function’s code in a set of braces ({}) • The code between the braces is called the function body An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) 39
  • 40. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) Figure 4-21 Addison O’Reilly program 40
  • 41. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Step 6–Evaluate and Modify the Program (cont’d.) Figure 4-22 Command Prompt window 41
  • 42. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Fourth step in problem-solving process is coding the algorithm into a program • C++ uses stream objects for standard input/output operations • Use cin with extraction operator (>>) to get numeric or character data from the keyboard • Use cout with insertion operator (<<) to display information on the screen • The endl stream manipulator advances cursor to next line An Introduction to Programming with C++, Eighth Edition Summary 42
  • 43. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • You do calculations by writing arithmetic expressions using arithmetic operators • Each operator has a precedence: determines the order of evaluation in an expression • Parentheses are used to override this order • Compiler implicitly casts data types when possible, but you should explicitly cast to ensure correctness • Use the static_cast operator to explicitly cast variables from one data type to another An Introduction to Programming with C++, Eighth Edition Summary (cont’d.) 43
  • 44. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • An assignment statement assigns a value to a variable during runtime • The expression on the right side of the assignment operator (=) in an assignment statement is stored in the variable on its left • Fifth step of the problem-solving process is to desk- check the program using the same data used to desk- check the algorithm • The sixth step is to evaluate and modify (if necessary) the program An Introduction to Programming with C++, Eighth Edition Summary (cont’d.) 44
  • 45. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Errors (called bugs) can either be syntax errors or logic errors • You need a text editor and compiler to enter C++ instructions and compile them into a program • C++ instructions are called source code and are saved in source files with the extension .cpp • The compiler translates source code into machine code, also called object code • A linker produces an executable file that contains all machine code necessary to run a C++ program An Introduction to Programming with C++, Eighth Edition Summary (cont’d.) 45
  • 46. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Programmers use comments to document a program internally – Comments are not processed by the compiler • The #include <filename> directive allows you to include multiple source files in a program • The using namespace std; directive tells the compiler where definitions of standard C++ keywords and classes are in internal memory • A namespace is a special area in the computer’s internal memory An Introduction to Programming with C++, Eighth Edition Summary (cont’d.) 46
  • 47. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Execution of every C++ program begins with the main() function • The first line of a function is the function header • The function body follows the header and is enclosed in braces • Some functions return a data type; others return void • Arithmetic assignment operators can be used to abbreviate certain assignment statements with arithmetic operators in them An Introduction to Programming with C++, Eighth Edition Summary (cont’d.) 47
  • 48. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. An Introduction to Programming with C++, Eighth Edition Lab 4-1: Stop and Analyze Figure 4-23 Examples for Lab 4-1 48
  • 49. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. In Chapter 3’s Lab 3-2, you planned, created, and desk- checked an algorithm that displays 10% commission on a sales amount. Figure 4-24 shows the algorithm, along with the input and output items and their corresponding C++ statements. It also includes the desk-check table you completed in Lab 3-2. Figure 4-25 shows the completed IPO chart and C++ instructions for Lab 4-2. Figure 4-27 shows the completed commission program. An Introduction to Programming with C++, Eighth Edition Lab 4-2: Plan and Create 49
  • 50. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Figure 4-27 Commission program An Introduction to Programming with C++, Eighth Edition Lab 4-2: Plan and Create (cont’d.) 50
  • 51. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. • Modify the program in Lab 4-2 to allow the user to enter the commission rate (in decimal form). • Test the program twice. For the first test, use 1328.50 and .1 as the sales amount and commission rate. For the second test, use 267.90 and .15. An Introduction to Programming with C++, Eighth Edition Lab 4-3: Modify 51
  • 52. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. The program in this lab should calculate and display the volume of a cylinder, given the cylinder’s radius (r) and height (h), and using 3.14 as the value of pi. An Introduction to Programming with C++, Eighth Edition Lab 4-4: What’s Missing? 52
  • 53. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Desk-check the seven lines of code shown in Figure 4.28 below An Introduction to Programming with C++, Eighth Edition Lab 4-5: Desk-Check 53
  • 54. v© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Following the instructions for Lab 4-6. The program should calculate and display the area of a triangle, but it is not working properly. Run and debug the program. An Introduction to Programming with C++, Eighth Edition Lab 4-6: Debug 54