SlideShare a Scribd company logo
© 2016 Cengage Learning®. May not be scanned, copied or
Introduction to Programming in C++
Eighth Edition
Lesson 7.2:
Using Counters and Accumulators
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Someproblems require you to calculate atotal or
average
• Todo this, you use acounter, accumulator, or both
• Acounter is anumeric variable used for counting
something
• An accumulator is anumeric variable used for
accumulating (adding together) multiple values
• Two tasks are associated with counters and
accumulators: initializing andupdating
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
2An Introduction to Programming with C++, Eighth Edition
Using Counters and Accumulators
• Initializing means assigningabeginning value to a
counter or accumulator (usually 0) – happensonce,
before the loop isprocessed
• Updating (or incrementing) means adding anumber to
the value of acounter oraccumulator
• Acounter is updated by aconstant value (usually1)
• An accumulator is updated by avalue that varies
• Update statements are placed in the body ofaloop
since they must be performed at eachiteration
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
3An Introduction to Programming with C++, Eighth Edition
Using Counters and Accumulators
(cont’d.)
Using Counters and Accumulators
(cont’d.)
Figure 7-13 Example of a partial game program that uses a counter
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
4An Introduction to Programming with C++, Eighth Edition
Using Counters and Accumulators
(cont’d.)
Figure 7-14 Syntax and examples of update statements for counters
and accumulators
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
5An Introduction to Programming with C++, Eighth Edition
• Example problem and program solution (following
slides)
– Program takes in asequence of stock prices fromthe
keyboard and outputs theiraverage
– Usesacounter to keep track of the number of prices
entered and an accumulator to keep track of the total
– Both are initialized to 0
– Theloop endswhen the user enters asentinel value(-1)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
6An Introduction to Programming with C++, Eighth Edition
The Stock Price Program
The Stock Price Program (cont’d.)
Figure 7-15 Problem specification for the Sales Express program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
7An Introduction to Programming with C++, Eighth Edition
Figure 7-15 IPO chart information and C++ instructions for
the Stock Price program
The Stock Price Program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
8An Introduction to Programming with C++, Eighth Edition
The Stock Price Program (cont’d.)
Figure 7-16 Flowchart for the Stock Price program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
9An Introduction to Programming with C++, Eighth Edition
The Stock Price Program (cont’d.)
Figure 7-17 Desk-check table after the first update to the counter
and accumulator variables
Figure 7-18 Desk-check table after the second update to the
counter and accumulator variables
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
10An Introduction to Programming with C++, Eighth Edition
The Stock Price Program (cont’d.)
Figure 7-19 Completed desk-check table and sample runs for the
Stock Price program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
11An Introduction to Programming with C++, Eighth Edition
• Loopscan be controlled using acounter rather thana
sentinel value
• Theseare called counter-controlled loops
• Example problem and program solution (following
slides)
– Counter-controlled loop is used thattotals the quarterly
salesfrom three regions
– Looprepeats three times, once for eachregion, usinga
counter to keeptrack
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
12An Introduction to Programming with C++, Eighth Edition
Counter-Controlled Pretest Loops
Counter-Controlled Pretest Loops
(cont’d.)
Figure 7-20 Problem specification, IPO chart information, and C++
instructions for the modified Stock Price program
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
13An Introduction to Programming with C++, Eighth Edition
Counter-Controlled Pretest Loops
(cont’d.)
Figure 7-20 Problem specification, IPO chart information, and C++
instructions for the modified Stock Price program (cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
14An Introduction to Programming with C++, Eighth Edition
Figure 7-21 Flowchart for the modified Stock Price program
Counter-Controlled Pretest Loops
(cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
15An Introduction to Programming with C++, Eighth Edition
Figure 7-22 Completed desk-check table and sample run for the
modified Stock Price program
Counter-Controlled Pretest Loops
(cont’d.)
© 2016 Cengage Learning®. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole
or in part.
16An Introduction to Programming with C++, Eighth Edition

More Related Content

Similar to Lesson 7.2 using counters and accumulators (20)

PPT
Chapter 02 - Problem Solving
Luis Daniel Serrano
 
PDF
Starting out with C from control structures through objects Eighth Edition To...
chridimhere
 
PDF
Starting out with C from control structures through objects Eighth Edition To...
hatleezzatcs
 
PDF
Starting out with C from control structures through objects Eighth Edition To...
hatleezzatcs
 
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
jovaerili
 
PPT
Intro. to prog. c++
KurdGul
 
PPT
Introduction to C++
Bharat Kalia
 
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
nukjobp1848
 
PDF
Starting out with C from control structures through objects Eighth Edition To...
sanasymiddei
 
PDF
Starting out with C from control structures through objects Eighth Edition To...
rsjhvvmvz3985
 
PDF
Lesson 8 more on repitition structure
MLG College of Learning, Inc
 
PPT
Chapter 5 - The Selection Structure
mshellman
 
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
payotbrocks
 
PDF
(eBook PDF) C++ How to Program 10th Edition by Paul J. Deitel
ipyburtm937
 
PPT
Programming of c++
Ateeq Sindhu
 
PDF
Computer Programming With C Kunal Pimparkhede
namotoanipe
 
PDF
(eBook PDF) C++ How to Program 10th Edition by Paul J. Deitel
yonilkares
 
PPT
Chapter 2 - Beginning the Problem-Solving Process
mshellman
 
PDF
(Ebook) C Primer Plus Sixth Edition by Stephen Prata
thihuedoryan
 
PDF
C++ How to Program 10th Edition Deitel Solutions Manual
kerpelgoulis
 
Chapter 02 - Problem Solving
Luis Daniel Serrano
 
Starting out with C from control structures through objects Eighth Edition To...
chridimhere
 
Starting out with C from control structures through objects Eighth Edition To...
hatleezzatcs
 
Starting out with C from control structures through objects Eighth Edition To...
hatleezzatcs
 
C++ How to Program 10th Edition Deitel Solutions Manual
jovaerili
 
Intro. to prog. c++
KurdGul
 
Introduction to C++
Bharat Kalia
 
C++ How to Program 10th Edition Deitel Solutions Manual
nukjobp1848
 
Starting out with C from control structures through objects Eighth Edition To...
sanasymiddei
 
Starting out with C from control structures through objects Eighth Edition To...
rsjhvvmvz3985
 
Lesson 8 more on repitition structure
MLG College of Learning, Inc
 
Chapter 5 - The Selection Structure
mshellman
 
C++ How to Program 10th Edition Deitel Solutions Manual
payotbrocks
 
(eBook PDF) C++ How to Program 10th Edition by Paul J. Deitel
ipyburtm937
 
Programming of c++
Ateeq Sindhu
 
Computer Programming With C Kunal Pimparkhede
namotoanipe
 
(eBook PDF) C++ How to Program 10th Edition by Paul J. Deitel
yonilkares
 
Chapter 2 - Beginning the Problem-Solving Process
mshellman
 
(Ebook) C Primer Plus Sixth Edition by Stephen Prata
thihuedoryan
 
C++ How to Program 10th Edition Deitel Solutions Manual
kerpelgoulis
 

More from MLG College of Learning, Inc (20)

PPTX
PC111.Lesson1
MLG College of Learning, Inc
 
PPTX
PC111-lesson1.pptx
MLG College of Learning, Inc
 
PPTX
PC LEESOON 6.pptx
MLG College of Learning, Inc
 
PPTX
PC 106 PPT-09.pptx
MLG College of Learning, Inc
 
PPTX
PC 106 PPT-07
MLG College of Learning, Inc
 
PPTX
PC 106 PPT-01
MLG College of Learning, Inc
 
PPTX
PC 106 Slide 04
MLG College of Learning, Inc
 
PPTX
PC 106 Slide no.02
MLG College of Learning, Inc
 
PPTX
pc-106-slide-3
MLG College of Learning, Inc
 
PPTX
PC 106 Slide 2
MLG College of Learning, Inc
 
PPTX
PC 106 Slide 1.pptx
MLG College of Learning, Inc
 
PDF
Db2 characteristics of db ms
MLG College of Learning, Inc
 
PDF
Db1 introduction
MLG College of Learning, Inc
 
Ad

Recently uploaded (20)

PPTX
WEEK5 ENGLISH.pptx Grade 2 MATATAG POWERPOINT QUARTER 1 WEEK 5
MelanieEdradanAlcant
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
ARAL-Guidelines-Learning-Resources_v3.pdf.pptx
canetevenus07
 
PPTX
Company - Meaning - Definition- Types of Company - Incorporation of Company
DevaRam6
 
PPTX
How to Consolidate Subscription Billing in Odoo 18 Sales
Celine George
 
PPTX
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PPTX
Qweb Templates and Operations in Odoo 18
Celine George
 
PPTX
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
PPTX
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
PPTX
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
PPTX
10CLA Term 3 Week 4 Study Techniques.pptx
mansk2
 
PPTX
Room booking management - Meeting Room In Odoo 17
Celine George
 
PPTX
THE HUMAN INTEGUMENTARY SYSTEM#MLT#BCRAPC.pptx
Subham Panja
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PDF
A guide to responding to Section C essay tasks for the VCE English Language E...
jpinnuck
 
PDF
Comprehensive Guide to Writing Effective Literature Reviews for Academic Publ...
AJAYI SAMUEL
 
PPTX
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
WEEK5 ENGLISH.pptx Grade 2 MATATAG POWERPOINT QUARTER 1 WEEK 5
MelanieEdradanAlcant
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
ARAL-Guidelines-Learning-Resources_v3.pdf.pptx
canetevenus07
 
Company - Meaning - Definition- Types of Company - Incorporation of Company
DevaRam6
 
How to Consolidate Subscription Billing in Odoo 18 Sales
Celine George
 
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
Qweb Templates and Operations in Odoo 18
Celine George
 
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
Mrs Mhondiwa Introduction to Algebra class
sabinaschimanga
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
10CLA Term 3 Week 4 Study Techniques.pptx
mansk2
 
Room booking management - Meeting Room In Odoo 17
Celine George
 
THE HUMAN INTEGUMENTARY SYSTEM#MLT#BCRAPC.pptx
Subham Panja
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
A guide to responding to Section C essay tasks for the VCE English Language E...
jpinnuck
 
Comprehensive Guide to Writing Effective Literature Reviews for Academic Publ...
AJAYI SAMUEL
 
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Ad

Lesson 7.2 using counters and accumulators

  • 1. © 2016 Cengage Learning®. May not be scanned, copied or Introduction to Programming in C++ Eighth Edition Lesson 7.2: Using Counters and Accumulators duplicated, or posted to a publicly accessible website, in whole or in part.
  • 2. • Someproblems require you to calculate atotal or average • Todo this, you use acounter, accumulator, or both • Acounter is anumeric variable used for counting something • An accumulator is anumeric variable used for accumulating (adding together) multiple values • Two tasks are associated with counters and accumulators: initializing andupdating © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2An Introduction to Programming with C++, Eighth Edition Using Counters and Accumulators
  • 3. • Initializing means assigningabeginning value to a counter or accumulator (usually 0) – happensonce, before the loop isprocessed • Updating (or incrementing) means adding anumber to the value of acounter oraccumulator • Acounter is updated by aconstant value (usually1) • An accumulator is updated by avalue that varies • Update statements are placed in the body ofaloop since they must be performed at eachiteration © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3An Introduction to Programming with C++, Eighth Edition Using Counters and Accumulators (cont’d.)
  • 4. Using Counters and Accumulators (cont’d.) Figure 7-13 Example of a partial game program that uses a counter © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4An Introduction to Programming with C++, Eighth Edition
  • 5. Using Counters and Accumulators (cont’d.) Figure 7-14 Syntax and examples of update statements for counters and accumulators © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 5An Introduction to Programming with C++, Eighth Edition
  • 6. • Example problem and program solution (following slides) – Program takes in asequence of stock prices fromthe keyboard and outputs theiraverage – Usesacounter to keep track of the number of prices entered and an accumulator to keep track of the total – Both are initialized to 0 – Theloop endswhen the user enters asentinel value(-1) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 6An Introduction to Programming with C++, Eighth Edition The Stock Price Program
  • 7. The Stock Price Program (cont’d.) Figure 7-15 Problem specification for the Sales Express program © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7An Introduction to Programming with C++, Eighth Edition
  • 8. Figure 7-15 IPO chart information and C++ instructions for the Stock Price program The Stock Price Program (cont’d.) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8An Introduction to Programming with C++, Eighth Edition
  • 9. The Stock Price Program (cont’d.) Figure 7-16 Flowchart for the Stock Price program © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9An Introduction to Programming with C++, Eighth Edition
  • 10. The Stock Price Program (cont’d.) Figure 7-17 Desk-check table after the first update to the counter and accumulator variables Figure 7-18 Desk-check table after the second update to the counter and accumulator variables © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10An Introduction to Programming with C++, Eighth Edition
  • 11. The Stock Price Program (cont’d.) Figure 7-19 Completed desk-check table and sample runs for the Stock Price program © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11An Introduction to Programming with C++, Eighth Edition
  • 12. • Loopscan be controlled using acounter rather thana sentinel value • Theseare called counter-controlled loops • Example problem and program solution (following slides) – Counter-controlled loop is used thattotals the quarterly salesfrom three regions – Looprepeats three times, once for eachregion, usinga counter to keeptrack © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12An Introduction to Programming with C++, Eighth Edition Counter-Controlled Pretest Loops
  • 13. Counter-Controlled Pretest Loops (cont’d.) Figure 7-20 Problem specification, IPO chart information, and C++ instructions for the modified Stock Price program © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13An Introduction to Programming with C++, Eighth Edition
  • 14. Counter-Controlled Pretest Loops (cont’d.) Figure 7-20 Problem specification, IPO chart information, and C++ instructions for the modified Stock Price program (cont’d.) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14An Introduction to Programming with C++, Eighth Edition
  • 15. Figure 7-21 Flowchart for the modified Stock Price program Counter-Controlled Pretest Loops (cont’d.) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15An Introduction to Programming with C++, Eighth Edition
  • 16. Figure 7-22 Completed desk-check table and sample run for the modified Stock Price program Counter-Controlled Pretest Loops (cont’d.) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 16An Introduction to Programming with C++, Eighth Edition