SlideShare a Scribd company logo
1 of 23
Download to read offline
© 2016 Cengage Learning®. May not be scanned, copied or
Introduction to Programming in C++
Eighth Edition
Lesson 9.2:
Guessing the Game Program
duplicated, or posted to a publicly accessible website, in whole
or in part.
• Program generates arandom number from 1 to 10and
then allows the user asmany choices asneeded to
guessthe number.
• Thesrand, time, and randfunctions are all utilized
in the program.
© 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
The Guessing Game Program
The Guessing Game Program(cont’d.)
Figure 9-10 Problem specification, IPO chart information, and C++
instructions for the Guessing Game Program
© 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
The Guessing Game Program(cont’d.)
Figure 9-11 Guessing Game Program and sample run
© 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
• Aprogram-defined value-returning function definition
is composed of aheader and abody
• Header (first line) contains return data type,name of
function, and an optional parameterList
– Rules for function namesare sameasfor variables
– Goodidea to usemeaningful namesthat describe
function’s purpose
– Memory locations in parameterList are calledformal
parameters
• Eachstores an item of information passedtothe function
when it iscalled
© 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
Creating Program-Defined Value-
Returning Functions
• Function body contains instructions for performingthe
function’s assigned task
• Surrounded by braces({})
• Laststatement is usually the return statement
– Returns one value (must match return data typein
function header)
• After returnstatement is processed, program
execution continues in callingfunction
• Good idea to include comment (such as//end of
functionName) to mark end offunction
© 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
Creating Program-Defined Value-
Returning Functions (cont’d.)
Figure 9-12 How to create a program-defined value-returning function
Creating Program-Defined Value-
Returning Functions (cont’d.)
© 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 9-12 How to create a program-defined value-returning function
Creating Program-Defined Value-Returning
Functions (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
• Afunction must be called (invoked) to perform itstask
• mainis automatically called when program is run
• Other functions must be called by astatement
• Syntax for calling afunction:
functionName([argumentList]);
– argumentList is list of actual arguments (ifany)
– Anactual argument canbe avariable, named constant,
literal constant, orkeyword
© 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
Calling a Function
• Value-returning functions are typically calledfrom
statements that:
– Assignthe return value to avariable
– Usethe return value in acalculation orcomparison
– Display the return value
• Acall to avoid function is an independent statement
because void functions do not returnvalues
© 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
Calling a Function (cont’d.)
• C++allows you to passeither avariable’s value or its
addressto afunction
• Passingavariable’s value is referred to aspassingbyvalue
• Passingavariable’s address is referred to aspassingby
reference
• Default is passing by value
• Number, data type, and ordering ofactual arguments must
match the formal parameters in functionheader
– Namesdo not need to match (differentnamesare better)
© 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
Calling a Function (cont’d.)
Figure 9-13 How to call (invoke) a value-returning function
Calling a Function (cont’d.)
© 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
Figure 9-14 Function call and function definition
Calling a Function (cont’d.)
© 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
• Theprogram allows the user to enter the initial deposit
made into asavings account and the annual interest rate.
• Theprogram displays the amount of money in the
account at the end of1 through 3 years, assumingno
additional deposits or withdrawals aremade.
© 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
The Savings Account Program
The Savings Account Program
(cont’d.)
Figure 9-15 Problem specification, IPO chart information,
and C++ instructions for the Savings Account Program
© 2016 Cengage Learning®. May not be scanned, copied or 15An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole
or in part.
The Savings Account Program
(cont’d.)
Figure 9-15 Problem specification, IPO chart information,
and C++ instructions for the Savings Account Program
© 2016 Cengage Learning®. May not be scanned, copied or 16An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole
or in part.
Figure 9-16 Flowcharts for the Savings Account Program
The Savings Account 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.
An Introduction to Programming with C++, Eighth Edition 17
• When afunction definition appears below the main
function, you must enter afunction prototypeabove the
main function
• Afunction prototype is astatement that specifies the
function’s name, data type of its return value, and data
type of each of its formal parameters (if any)
– Namesfor the formal parameters are notrequired
• Programmers usually place function prototypes at
beginning of program, after the #includedirectives
© 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 18
Function Prototypes
Figure 9-17 How to write a function prototype
Function Prototypes (cont’d.)
© 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 19
Figure 9-18 Savings Account Program
Completing the Savings Account
Program
© 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 20
• Avariable’s scope indicates where in the programthe
variable can be used
• Avariable’s lifetime indicates how long the variable
remains in the computer’s internalmemory
• Both scope and lifetime are determined by whereyou
declare the variable in theprogram
• Variables declared within afunction and those that
appear in afunction’s parameterList have alocal scope
and are referred to aslocalvariables
© 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 21
The Scope and Lifetime of a Variable
• Localvariables can be used only by the function inwhich
they are declared or in whose parameterList theyappear
– Remainin internal memory until the function ends
• Global variables are declared outside of any functionin
the program
– Remainin memory until the programends
• Any statement can useaglobal variable
© 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 22
The Scope and Lifetime of a Variable
(cont’d.)
• Declaring a variable as global can allow unintentional
errors to occur
– e.g., a function that should not have access to the variable
inadvertently changesthe variable’s contents
• Youshould avoid using global variables unless necessary
• If more than one function needs to accessthe same
variable, it is better to create a local variable in one
function and pass it to other functions that need it
© 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 23
The Scope and Lifetime of a Variable
(cont’d.)

More Related Content

What's hot

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 Processmshellman
 
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 Structuremshellman
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection Structuremshellman
 
Chapter 7 - The Repetition Structure
Chapter 7 - The Repetition StructureChapter 7 - The Repetition Structure
Chapter 7 - The Repetition Structuremshellman
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constantsmshellman
 
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 Processmshellman
 
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 Structuremshellman
 
test(3)arithmetic in c
test(3)arithmetic in ctest(3)arithmetic in c
test(3)arithmetic in cJaya Malathy
 
Abap objects in action
Abap objects in actionAbap objects in action
Abap objects in actionFaina Fridman
 
XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...
XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...
XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...Publicis Sapient Engineering
 

What's hot (20)

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
 
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
 
Chapter 5 - The Selection Structure
Chapter 5 - The Selection StructureChapter 5 - The Selection Structure
Chapter 5 - The Selection 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
 
Chapter 3 - Variables and Constants
Chapter 3 - Variables and ConstantsChapter 3 - Variables and Constants
Chapter 3 - Variables and Constants
 
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 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
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
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 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
 
Lesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving processLesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving process
 
test(3)arithmetic in c
test(3)arithmetic in ctest(3)arithmetic in c
test(3)arithmetic in c
 
A tutorial on C++ Programming
A tutorial on C++ ProgrammingA tutorial on C++ Programming
A tutorial on C++ Programming
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
Abap objects in action
Abap objects in actionAbap objects in action
Abap objects in action
 
XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...
XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...
XebiCon'17 : Faites chauffer les neurones de votre Smartphone avec du Deep Le...
 

Similar to Lesson 9.2 guessing the game program

Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Kamran Zafar
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programmingmshellman
 
DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...
DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...
DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...Gene Kim
 
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....at315244
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.pptMalathyN6
 
API Design Best Practices by Igor Miniailo
API Design Best Practices by Igor MiniailoAPI Design Best Practices by Igor Miniailo
API Design Best Practices by Igor MiniailoMagecom UK Limited
 
9781337102087 ppt ch06
9781337102087 ppt ch069781337102087 ppt ch06
9781337102087 ppt ch06Terry Yoast
 
Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"Sigma Software
 
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 AdvancedSenturus
 
Accelerate Go-To-Market Speed in a CI/CD Environment
Accelerate Go-To-Market Speed in a CI/CD EnvironmentAccelerate Go-To-Market Speed in a CI/CD Environment
Accelerate Go-To-Market Speed in a CI/CD EnvironmentAmazon Web Services
 
How to Architect and Develop Cloud Native Applications
How to Architect and Develop Cloud Native ApplicationsHow to Architect and Develop Cloud Native Applications
How to Architect and Develop Cloud Native ApplicationsSufyaan Kazi
 
How to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and DataflowHow to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and DataflowDaniel Zivkovic
 
Retrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architectureRetrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architectureManuel Rivero
 
Djangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable ApplicationsDjangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable ApplicationsNowell Strite
 
Ab1011 module pool programming
Ab1011   module pool programmingAb1011   module pool programming
Ab1011 module pool programmingSatheesh Kanna
 
Oracle What's New In Primavera P6 16.2
Oracle What's New In Primavera P6 16.2Oracle What's New In Primavera P6 16.2
Oracle What's New In Primavera P6 16.2p6academy
 
Lecture six- Discovering computers (2018)
Lecture six- Discovering computers (2018)Lecture six- Discovering computers (2018)
Lecture six- Discovering computers (2018)Aiman Niazi
 

Similar to Lesson 9.2 guessing the game program (20)

Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)Lecture 1 programming fundamentals (PF)
Lecture 1 programming fundamentals (PF)
 
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
 
DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...
DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...
DOES16 San Francisco - Marc Ng - SAP’s DevOps Journey: From Building an App t...
 
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
DC16_Ch09_Operating Systems Managing, Coordinating, and Monitoring Resources....
 
chapter4.ppt
chapter4.pptchapter4.ppt
chapter4.ppt
 
API Design Best Practices by Igor Miniailo
API Design Best Practices by Igor MiniailoAPI Design Best Practices by Igor Miniailo
API Design Best Practices by Igor Miniailo
 
9781337102087 ppt ch06
9781337102087 ppt ch069781337102087 ppt ch06
9781337102087 ppt ch06
 
Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"Петро Коренєв, "Presentation state containers"
Петро Коренєв, "Presentation state containers"
 
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
 
Accelerate Go-To-Market Speed in a CI/CD Environment
Accelerate Go-To-Market Speed in a CI/CD EnvironmentAccelerate Go-To-Market Speed in a CI/CD Environment
Accelerate Go-To-Market Speed in a CI/CD Environment
 
Open mp
Open mpOpen mp
Open mp
 
How to Architect and Develop Cloud Native Applications
How to Architect and Develop Cloud Native ApplicationsHow to Architect and Develop Cloud Native Applications
How to Architect and Develop Cloud Native Applications
 
How to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and DataflowHow to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
 
Retrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architectureRetrofitting a legacy SPA to use a functional architecture
Retrofitting a legacy SPA to use a functional architecture
 
Djangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable ApplicationsDjangocon 09 Presentation - Pluggable Applications
Djangocon 09 Presentation - Pluggable Applications
 
programs and apps
programs and appsprograms and apps
programs and apps
 
Ab1011 module pool programming
Ab1011   module pool programmingAb1011   module pool programming
Ab1011 module pool programming
 
Oracle What's New In Primavera P6 16.2
Oracle What's New In Primavera P6 16.2Oracle What's New In Primavera P6 16.2
Oracle What's New In Primavera P6 16.2
 
Lecture six- Discovering computers (2018)
Lecture six- Discovering computers (2018)Lecture six- Discovering computers (2018)
Lecture six- Discovering computers (2018)
 

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

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

Lesson 9.2 guessing the game program

  • 1. © 2016 Cengage Learning®. May not be scanned, copied or Introduction to Programming in C++ Eighth Edition Lesson 9.2: Guessing the Game Program duplicated, or posted to a publicly accessible website, in whole or in part.
  • 2. • Program generates arandom number from 1 to 10and then allows the user asmany choices asneeded to guessthe number. • Thesrand, time, and randfunctions are all utilized in the program. © 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 The Guessing Game Program
  • 3. The Guessing Game Program(cont’d.) Figure 9-10 Problem specification, IPO chart information, and C++ instructions for the Guessing Game Program © 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
  • 4. The Guessing Game Program(cont’d.) Figure 9-11 Guessing Game Program and sample run © 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. • Aprogram-defined value-returning function definition is composed of aheader and abody • Header (first line) contains return data type,name of function, and an optional parameterList – Rules for function namesare sameasfor variables – Goodidea to usemeaningful namesthat describe function’s purpose – Memory locations in parameterList are calledformal parameters • Eachstores an item of information passedtothe function when it iscalled © 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 Creating Program-Defined Value- Returning Functions
  • 6. • Function body contains instructions for performingthe function’s assigned task • Surrounded by braces({}) • Laststatement is usually the return statement – Returns one value (must match return data typein function header) • After returnstatement is processed, program execution continues in callingfunction • Good idea to include comment (such as//end of functionName) to mark end offunction © 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 Creating Program-Defined Value- Returning Functions (cont’d.)
  • 7. Figure 9-12 How to create a program-defined value-returning function Creating Program-Defined Value- Returning Functions (cont’d.) © 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 9-12 How to create a program-defined value-returning function Creating Program-Defined Value-Returning Functions (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. • Afunction must be called (invoked) to perform itstask • mainis automatically called when program is run • Other functions must be called by astatement • Syntax for calling afunction: functionName([argumentList]); – argumentList is list of actual arguments (ifany) – Anactual argument canbe avariable, named constant, literal constant, orkeyword © 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 Calling a Function
  • 10. • Value-returning functions are typically calledfrom statements that: – Assignthe return value to avariable – Usethe return value in acalculation orcomparison – Display the return value • Acall to avoid function is an independent statement because void functions do not returnvalues © 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 Calling a Function (cont’d.)
  • 11. • C++allows you to passeither avariable’s value or its addressto afunction • Passingavariable’s value is referred to aspassingbyvalue • Passingavariable’s address is referred to aspassingby reference • Default is passing by value • Number, data type, and ordering ofactual arguments must match the formal parameters in functionheader – Namesdo not need to match (differentnamesare better) © 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 Calling a Function (cont’d.)
  • 12. Figure 9-13 How to call (invoke) a value-returning function Calling a Function (cont’d.) © 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
  • 13. Figure 9-14 Function call and function definition Calling a Function (cont’d.) © 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. • Theprogram allows the user to enter the initial deposit made into asavings account and the annual interest rate. • Theprogram displays the amount of money in the account at the end of1 through 3 years, assumingno additional deposits or withdrawals aremade. © 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 The Savings Account Program
  • 15. The Savings Account Program (cont’d.) Figure 9-15 Problem specification, IPO chart information, and C++ instructions for the Savings Account Program © 2016 Cengage Learning®. May not be scanned, copied or 15An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole or in part.
  • 16. The Savings Account Program (cont’d.) Figure 9-15 Problem specification, IPO chart information, and C++ instructions for the Savings Account Program © 2016 Cengage Learning®. May not be scanned, copied or 16An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole or in part.
  • 17. Figure 9-16 Flowcharts for the Savings Account Program The Savings Account 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. An Introduction to Programming with C++, Eighth Edition 17
  • 18. • When afunction definition appears below the main function, you must enter afunction prototypeabove the main function • Afunction prototype is astatement that specifies the function’s name, data type of its return value, and data type of each of its formal parameters (if any) – Namesfor the formal parameters are notrequired • Programmers usually place function prototypes at beginning of program, after the #includedirectives © 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 18 Function Prototypes
  • 19. Figure 9-17 How to write a function prototype Function Prototypes (cont’d.) © 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 19
  • 20. Figure 9-18 Savings Account Program Completing the Savings Account Program © 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 20
  • 21. • Avariable’s scope indicates where in the programthe variable can be used • Avariable’s lifetime indicates how long the variable remains in the computer’s internalmemory • Both scope and lifetime are determined by whereyou declare the variable in theprogram • Variables declared within afunction and those that appear in afunction’s parameterList have alocal scope and are referred to aslocalvariables © 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 21 The Scope and Lifetime of a Variable
  • 22. • Localvariables can be used only by the function inwhich they are declared or in whose parameterList theyappear – Remainin internal memory until the function ends • Global variables are declared outside of any functionin the program – Remainin memory until the programends • Any statement can useaglobal variable © 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 22 The Scope and Lifetime of a Variable (cont’d.)
  • 23. • Declaring a variable as global can allow unintentional errors to occur – e.g., a function that should not have access to the variable inadvertently changesthe variable’s contents • Youshould avoid using global variables unless necessary • If more than one function needs to accessthe same variable, it is better to create a local variable in one function and pass it to other functions that need it © 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 23 The Scope and Lifetime of a Variable (cont’d.)