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

What's hot

Jsp lifecycle
Jsp   lifecycleJsp   lifecycle
Jsp lifecycle
chauhankapil
 
Pipelining powerpoint presentation
Pipelining powerpoint presentationPipelining powerpoint presentation
Pipelining powerpoint presentation
bhavanadonthi
 
The Future of Business Planning with BPC 10.1 and SAP HANA
The Future of Business Planning with BPC 10.1 and SAP  HANAThe Future of Business Planning with BPC 10.1 and SAP  HANA
The Future of Business Planning with BPC 10.1 and SAP HANA
Dickinson + Associates
 
Parallelism
ParallelismParallelism
Parallelism
Md Raseduzzaman
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Bharat Bhushan
 
Discrete mathematics [LOGICAL CONNECTIVES]
Discrete mathematics [LOGICAL CONNECTIVES]Discrete mathematics [LOGICAL CONNECTIVES]
Discrete mathematics [LOGICAL CONNECTIVES]
KalingoAurobindo
 
Zone IT Solutions SAP Consulting Services
Zone IT Solutions SAP Consulting ServicesZone IT Solutions SAP Consulting Services
Zone IT Solutions SAP Consulting Services
Zone IT Solutions
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
Rajendran
 
SAP Adaptive Computing Design
SAP Adaptive Computing DesignSAP Adaptive Computing Design
SAP Adaptive Computing Design
Gary Jackson MBCS
 
Distributed & parallel system
Distributed & parallel systemDistributed & parallel system
Distributed & parallel systemManish Singh
 
Lesson 05
Lesson 05Lesson 05
Hm system programming class 1
Hm system programming class 1Hm system programming class 1
Hm system programming class 1
Hitesh Mohapatra
 
Memory organization
Memory organizationMemory organization
Memory organization
ishapadhy
 
First Come First Serve & Shortest Job First-(FCFS & SJF)
First Come First Serve & Shortest Job First-(FCFS & SJF)First Come First Serve & Shortest Job First-(FCFS & SJF)
First Come First Serve & Shortest Job First-(FCFS & SJF)
Adeel Rasheed
 
Ch2 finite automaton
Ch2 finite automatonCh2 finite automaton
Ch2 finite automaton
meresie tesfay
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
Akshaya Arunan
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
varun arora
 
What's New in SAP HANA View Modeling
What's New in SAP HANA View ModelingWhat's New in SAP HANA View Modeling
What's New in SAP HANA View Modeling
SAP Technology
 

What's hot (20)

Jsp lifecycle
Jsp   lifecycleJsp   lifecycle
Jsp lifecycle
 
Pipelining powerpoint presentation
Pipelining powerpoint presentationPipelining powerpoint presentation
Pipelining powerpoint presentation
 
The Future of Business Planning with BPC 10.1 and SAP HANA
The Future of Business Planning with BPC 10.1 and SAP  HANAThe Future of Business Planning with BPC 10.1 and SAP  HANA
The Future of Business Planning with BPC 10.1 and SAP HANA
 
Parallelism
ParallelismParallelism
Parallelism
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Discrete mathematics [LOGICAL CONNECTIVES]
Discrete mathematics [LOGICAL CONNECTIVES]Discrete mathematics [LOGICAL CONNECTIVES]
Discrete mathematics [LOGICAL CONNECTIVES]
 
Zone IT Solutions SAP Consulting Services
Zone IT Solutions SAP Consulting ServicesZone IT Solutions SAP Consulting Services
Zone IT Solutions SAP Consulting Services
 
Conditional jump
Conditional jumpConditional jump
Conditional jump
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
SAP Adaptive Computing Design
SAP Adaptive Computing DesignSAP Adaptive Computing Design
SAP Adaptive Computing Design
 
Distributed & parallel system
Distributed & parallel systemDistributed & parallel system
Distributed & parallel system
 
Lesson 05
Lesson 05Lesson 05
Lesson 05
 
Hm system programming class 1
Hm system programming class 1Hm system programming class 1
Hm system programming class 1
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Memory organization
Memory organizationMemory organization
Memory organization
 
First Come First Serve & Shortest Job First-(FCFS & SJF)
First Come First Serve & Shortest Job First-(FCFS & SJF)First Come First Serve & Shortest Job First-(FCFS & SJF)
First Come First Serve & Shortest Job First-(FCFS & SJF)
 
Ch2 finite automaton
Ch2 finite automatonCh2 finite automaton
Ch2 finite automaton
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
What's New in SAP HANA View Modeling
What's New in SAP HANA View ModelingWhat's New in SAP HANA View Modeling
What's New in SAP HANA View Modeling
 

Similar to Lesson 7.2 using counters and accumulators

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
 
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 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 7.1 repitition structure
Lesson 7.1 repitition structureLesson 7.1 repitition structure
Lesson 7.1 repitition structure
MLG College of Learning, Inc
 
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving ProcessChapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Process
mshellman
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
mshellman
 
Lesson 9.1 value returning
Lesson 9.1 value returningLesson 9.1 value returning
Lesson 9.1 value returning
MLG College of Learning, Inc
 
Chapter 9 Value-Returning Functions
Chapter 9 Value-Returning FunctionsChapter 9 Value-Returning Functions
Chapter 9 Value-Returning Functions
mshellman
 
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
mshellman
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
mshellman
 
Chapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection StructureChapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection Structure
mshellman
 
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
Kamran Zafar
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
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
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
mshellman
 
PMI-ACP Lesson 10 Agile Metrics
PMI-ACP Lesson 10 Agile MetricsPMI-ACP Lesson 10 Agile Metrics
PMI-ACP Lesson 10 Agile Metrics
Thanh Nguyen
 
O que fazer (ou não) quando códigos com erro estão parando a produção
O que fazer (ou não) quando códigos com erro estão parando a produçãoO que fazer (ou não) quando códigos com erro estão parando a produção
O que fazer (ou não) quando códigos com erro estão parando a produção
Jose Nunes
 
Questions Log: Transitioning to Cognos Workspace Advanced
Questions Log: Transitioning to Cognos Workspace AdvancedQuestions Log: Transitioning to Cognos Workspace Advanced
Questions Log: Transitioning to Cognos Workspace Advanced
Senturus
 

Similar to Lesson 7.2 using counters and accumulators (20)

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
 
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 8 more on repitition structure
Lesson 8 more on repitition structureLesson 8 more on repitition structure
Lesson 8 more on repitition structure
 
Lesson 7.1 repitition structure
Lesson 7.1 repitition structureLesson 7.1 repitition structure
Lesson 7.1 repitition structure
 
Lesson 6.2 logic error
Lesson  6.2 logic errorLesson  6.2 logic error
Lesson 6.2 logic error
 
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving ProcessChapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Process
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
 
Lesson 9.1 value returning
Lesson 9.1 value returningLesson 9.1 value returning
Lesson 9.1 value returning
 
Chapter 9 Value-Returning Functions
Chapter 9 Value-Returning FunctionsChapter 9 Value-Returning Functions
Chapter 9 Value-Returning Functions
 
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structure
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structure
 
Chapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection StructureChapter 6 - More on the Selection Structure
Chapter 6 - More on the Selection Structure
 
Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
 
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
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
PMI-ACP Lesson 10 Agile Metrics
PMI-ACP Lesson 10 Agile MetricsPMI-ACP Lesson 10 Agile Metrics
PMI-ACP Lesson 10 Agile Metrics
 
O que fazer (ou não) quando códigos com erro estão parando a produção
O que fazer (ou não) quando códigos com erro estão parando a produçãoO que fazer (ou não) quando códigos com erro estão parando a produção
O que fazer (ou não) quando códigos com erro estão parando a produção
 
Questions Log: Transitioning to Cognos Workspace Advanced
Questions Log: Transitioning to Cognos Workspace AdvancedQuestions Log: Transitioning to Cognos Workspace Advanced
Questions Log: Transitioning to Cognos Workspace Advanced
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

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
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
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
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
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
 
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
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 

Recently uploaded (20)

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...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
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
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
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
 
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
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 

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