SlideShare une entreprise Scribd logo
1  sur  9
Phase 4: Problem Solving with Loops
Total orderDescription
Once the final prices are calculated for each product, we need to
total up the order and then add in sales tax. We need to go
through the line items in an order and figure out the final
price.Flowchart
Create variable Double: subtotal
Return
calculateSubtotal (subtotal, salesTaxRate)
Subtotal = subtotal +
lineItemTotal
index = each item in lineItemPrice
lineItemTotal=
lineItemPrice[index] *
lineItemQuantity[index]
Pseudocode
function totalOrder (lineItemPrice: Array,
lineItemQuantity : Array,
saleTaxRate : Double)
returns double
Create variable Double : subtotal
for (index = each item in lineItemPrice)
lineItemTotal = lineItemPrice[index] *
lineItemQuantity[index]
subtotal = subtotal + lineItemTotal
end loop
return calculateSubtotal (subtotal, salesTaxRate)
end function
Problems to Solve
Fill in the following table by walking through the logic above.
The idea is to analyze how the chart and pseudocode was
created, because you will be doing this in a few minutes, so do
not just jump to the easy answer. Follow the steps as if you are
the computer executing the software designed.
Problem
lineItemPrice
lineItemQuantity
saleTaxRate
Return amount
1
[5.50, 3.00, 2.25]
[12, 3, 10]
5%
2
[5.50]
[5]
5%
3
[]
[]
5%
Calculate ProfitsDescription
Given the list of all the product prices and wholesale prices as
well as a list of all the items sold for each product calculate the
net profit for all the products.Flowchart
<insert here>Pseudocode
function totalProfits (allProductSalesNumbers: Array,
allProductPrices : Array,
allProductWholesalePrices : Array)
returns double
<add your logic here>
end function
Rock, Paper, ScissorsDescription
To expand further, we want to try out logic in interacting with
users. Here is a sample of a game, using loops and decisions to
allow a person to play rock-paper-scissors with a
computer.Pseudocode
We have a helper function that we will use here that tells us
who won. It will result in +1 if the player wins, a -1 if the
computer wins, and a 0 if it is a tie.
function compareOutcome(computerMove: Integer,
playerChoice : Integer) returns Integer
And the main flow of our logic is as such.
while (true)
Integer : computerChoice = randomChooser()
Integer : playerEntry = prompt user to enter a value
If (playerEntry is not a valid input)
Prompt user they have picked a bad number and
try again
Else
Integer : outcome = compareOutcome(computerMove,
playerEntry)
If (outcome = 0)
Prompt user it is a tie, and try again
Else if (outcome = -1)
Prompt user they lost
Break out of loop
Else
Prompt user they won
Break out of loop
End if
End if
end loopFlowchart
true
playerEntry is not a valid input
outcome =
0
outcome =
-1
Integer : computerChoice = ramdonChooser()
Integer : playerEntry = prompt user to enter a value
Prompt user they have picked a bad number and try again
Integer : outcome = compareOutcome(computerMove,
playerEntry)
Prompt user it is a tie, and try again
Prompt user they lost
Break
Prompt user they won
End of game
Break
Note: In this case, note the advantages and disadvantages of
flowcharts and pseudocode. The flowchart is easy to follow the
logic for one scenario, but does it make it easier or harder to
understand the code as a whole? There is no strict answer, but
one that tells you a bit about your way of thinking and personal
preference.Interaction Scenario
For either of the solutions above, follow the logic and map out
your interaction step-by-step, trying to exercise all paths of the
logic. You will have to take at least 2 times through the game to
map all scenarios.
Scenario 1
Step
User Input
System Response
1
User chooses 50
System says lower
2
…
..
3
4
Scenario 2
Step
User Input
System Response
1
2
3
4
(copy and add more if needed/desired)
Number Guessing GameDescription
Now it is your turn to try out user interaction with a simple
game. This goes back to the number guessing game. The user
will input a number each time, say between 1 and 100. The
computer will randomly select 1 number each game (note this is
different than the example above where it selects a new answer
each round), and the game continues until the user guesses the
right number.
Like above, your solution should accommodate for invalid
input, but do not worry about how to make that logic work for
now. Your solution should give some hints though if the guess
the user made was too high or too low, to help them on their
way.Flowchart
<Your solution here>Pseudocode
<your solution here>
Phase 5: Using Abstractions in DesignSeeing Abstractions
Throughout this course, we have been realizing the detailed
design for the pieces of the whole picture we started with at the
beginning of the course. We have built many abstractions, so
let’s go back and document what we have found and see the
abstractions we have created.
Abstraction Name
Parameter List
Scope/Purpose
calculateSubtotal
(totalPurchaseAmount :double,
salesTaxRate : double)
Applies sales tax and computes the final amount due
<keep going>
Refactoring
Sometimes it is easy to see how the whole solution can be
broken into pieces, and other times it is done as you see
abstractions and opportunities for reuse in the resulting design.
This is called refactoring. For the pseudocode you see below,
look for abstractions you could create and create a flowchart
using your simplifications.
Psuedocode in Need of Refactoring
Array : namesInSystem
Array : phoneNumbersInSystem
String : nameInput
While (nameInput is not valid)
nameInput = prompt user for input
if (nameInput is provided and not blank)
break out of loop
end loop
Prompt user the input is required and not blank
End loop
Integer : indexForName = -1
For (index = all items in namesInSystem
If (nameInput = namesInSystem[index])
indexForName = index
end if
End loop
If (indexForName = -1)
Prompt user “We cannot find your account, please call us”
Else
String : phoneNumberInput
While (phoneNumber is not valid)
phoneNumber = prompt user for input
if (phoneNumber is provided and not blank)
break out of loop
end loop
Prompt user the input is required and not blank
End loop
If (phoneNumber =
phoneNumbersInSystem[indexForName])
Prompt “we found your account welcome”
Else
Prompt “we cannot validate your account, try
again later”
End if
End ifRefactored Flowchart
The above pseudocode is long, but you can omit much of the
details. You can accomplish the same logic removing more than
30 lines. You do not have to show all of the details removed if
moved into an abstraction, simply name the abstraction and
define the parameters to be passed to the call replacing the lines
of code.
<insert your flowchart here>

Contenu connexe

Similaire à Phase 4 Problem Solving with LoopsTotal orderDescriptionOnce .docx

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 
Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6
cmontanez
 
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docxSkip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
edgar6wallace88877
 

Similaire à Phase 4 Problem Solving with LoopsTotal orderDescriptionOnce .docx (20)

Cis 115 Education Organization -- snaptutorial.com
Cis 115   Education Organization -- snaptutorial.comCis 115   Education Organization -- snaptutorial.com
Cis 115 Education Organization -- snaptutorial.com
 
Python_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptxPython_Unit-1_PPT_Data Types.pptx
Python_Unit-1_PPT_Data Types.pptx
 
Cis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.comCis 115 Education Organization / snaptutorial.com
Cis 115 Education Organization / snaptutorial.com
 
Cis 115 Enhance teaching / snaptutorial.com
Cis 115  Enhance teaching / snaptutorial.comCis 115  Enhance teaching / snaptutorial.com
Cis 115 Enhance teaching / snaptutorial.com
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
 
Loop structures chpt_6
Loop structures chpt_6Loop structures chpt_6
Loop structures chpt_6
 
The java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfThe java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdf
 
Cis 115 Effective Communication / snaptutorial.com
Cis 115  Effective Communication / snaptutorial.comCis 115  Effective Communication / snaptutorial.com
Cis 115 Effective Communication / snaptutorial.com
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
CIS 115 Become Exceptional--cis115.com
CIS 115 Become Exceptional--cis115.comCIS 115 Become Exceptional--cis115.com
CIS 115 Become Exceptional--cis115.com
 
Cis 115 Extraordinary Success/newtonhelp.com
Cis 115 Extraordinary Success/newtonhelp.com  Cis 115 Extraordinary Success/newtonhelp.com
Cis 115 Extraordinary Success/newtonhelp.com
 
Python-review1.ppt
Python-review1.pptPython-review1.ppt
Python-review1.ppt
 
Python-review1.pdf
Python-review1.pdfPython-review1.pdf
Python-review1.pdf
 
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docxSkip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
Skip To ContentOpen Quick LinksQuick LinksPage LandmarksCont.docx
 
08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx
 
CIS 115 Achievement Education--cis115.com
CIS 115 Achievement Education--cis115.comCIS 115 Achievement Education--cis115.com
CIS 115 Achievement Education--cis115.com
 
Python in details
Python in detailsPython in details
Python in details
 

Plus de randymartin91030

Ask Michael E. Mark about his company’s procedures for making a big .docx
Ask Michael E. Mark about his company’s procedures for making a big .docxAsk Michael E. Mark about his company’s procedures for making a big .docx
Ask Michael E. Mark about his company’s procedures for making a big .docx
randymartin91030
 
ask an expertwww.NursingMadeIncrediblyEasy.com JanuaryFe.docx
ask an expertwww.NursingMadeIncrediblyEasy.com  JanuaryFe.docxask an expertwww.NursingMadeIncrediblyEasy.com  JanuaryFe.docx
ask an expertwww.NursingMadeIncrediblyEasy.com JanuaryFe.docx
randymartin91030
 
Asian American ResearchHello class, I hope this finds you all we.docx
Asian American ResearchHello class, I hope this finds you all we.docxAsian American ResearchHello class, I hope this finds you all we.docx
Asian American ResearchHello class, I hope this finds you all we.docx
randymartin91030
 
ASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docx
ASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docxASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docx
ASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docx
randymartin91030
 
Asian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docx
Asian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docxAsian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docx
Asian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docx
randymartin91030
 
Ashry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docx
Ashry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docxAshry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docx
Ashry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docx
randymartin91030
 
Ashford Graduate Intro Week Six Discussion Two 2 Examples.docx
Ashford Graduate Intro Week Six Discussion Two 2 Examples.docxAshford Graduate Intro Week Six Discussion Two 2 Examples.docx
Ashford Graduate Intro Week Six Discussion Two 2 Examples.docx
randymartin91030
 
Ashford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docx
Ashford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docxAshford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docx
Ashford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docx
randymartin91030
 
ASD Services ResourcesAutism ResourcesFlorida Department of H.docx
ASD Services ResourcesAutism ResourcesFlorida Department of H.docxASD Services ResourcesAutism ResourcesFlorida Department of H.docx
ASD Services ResourcesAutism ResourcesFlorida Department of H.docx
randymartin91030
 
ASCI 615 AviationAerospace Accident Investigation and.docx
ASCI 615 AviationAerospace Accident Investigation and.docxASCI 615 AviationAerospace Accident Investigation and.docx
ASCI 615 AviationAerospace Accident Investigation and.docx
randymartin91030
 
ASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docx
ASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docxASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docx
ASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docx
randymartin91030
 
ASB3109 Strategic ManagementYour Individual Assignment.docx
ASB3109 Strategic ManagementYour Individual Assignment.docxASB3109 Strategic ManagementYour Individual Assignment.docx
ASB3109 Strategic ManagementYour Individual Assignment.docx
randymartin91030
 
ASB 100Spring 2019Writing Assignment 3In this assignme.docx
ASB 100Spring 2019Writing Assignment 3In this assignme.docxASB 100Spring 2019Writing Assignment 3In this assignme.docx
ASB 100Spring 2019Writing Assignment 3In this assignme.docx
randymartin91030
 
Asam100bbXinyu ShangReading journal week1In the article Im.docx
Asam100bbXinyu ShangReading journal week1In the article Im.docxAsam100bbXinyu ShangReading journal week1In the article Im.docx
Asam100bbXinyu ShangReading journal week1In the article Im.docx
randymartin91030
 

Plus de randymartin91030 (20)

Ask Michael E. Mark about his company’s procedures for making a big .docx
Ask Michael E. Mark about his company’s procedures for making a big .docxAsk Michael E. Mark about his company’s procedures for making a big .docx
Ask Michael E. Mark about his company’s procedures for making a big .docx
 
ask an expertwww.NursingMadeIncrediblyEasy.com JanuaryFe.docx
ask an expertwww.NursingMadeIncrediblyEasy.com  JanuaryFe.docxask an expertwww.NursingMadeIncrediblyEasy.com  JanuaryFe.docx
ask an expertwww.NursingMadeIncrediblyEasy.com JanuaryFe.docx
 
Ask clarifying or thought provoking questions.Provide personal or .docx
Ask clarifying or thought provoking questions.Provide personal or .docxAsk clarifying or thought provoking questions.Provide personal or .docx
Ask clarifying or thought provoking questions.Provide personal or .docx
 
Asian American ResearchHello class, I hope this finds you all we.docx
Asian American ResearchHello class, I hope this finds you all we.docxAsian American ResearchHello class, I hope this finds you all we.docx
Asian American ResearchHello class, I hope this finds you all we.docx
 
ASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docx
ASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docxASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docx
ASIAN CASE RESEARCH JOURNAL, VOL. 23, ISSUE 1, 153–191 (2019).docx
 
Asian Americans had been excluded from entering the U.S. for more th.docx
Asian Americans had been excluded from entering the U.S. for more th.docxAsian Americans had been excluded from entering the U.S. for more th.docx
Asian Americans had been excluded from entering the U.S. for more th.docx
 
Asia; Opera and Society and a DilemmaPlease respond to t.docx
Asia; Opera and Society and a DilemmaPlease respond to t.docxAsia; Opera and Society and a DilemmaPlease respond to t.docx
Asia; Opera and Society and a DilemmaPlease respond to t.docx
 
Asian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docx
Asian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docxAsian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docx
Asian Academy of Management Journal, Vol. 18, No. 1, 1 –17, 20.docx
 
Ashry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docx
Ashry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docxAshry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docx
Ashry 1Nedal AshryProf. GuzikENGL 301B15 February 20.docx
 
Ashford Graduate Intro Week Six Discussion Two 2 Examples.docx
Ashford Graduate Intro Week Six Discussion Two 2 Examples.docxAshford Graduate Intro Week Six Discussion Two 2 Examples.docx
Ashford Graduate Intro Week Six Discussion Two 2 Examples.docx
 
Ashford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docx
Ashford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docxAshford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docx
Ashford 6 - Week 5 - Final ProjectFinal ProjectImagine that you.docx
 
ASD Services ResourcesAutism ResourcesFlorida Department of H.docx
ASD Services ResourcesAutism ResourcesFlorida Department of H.docxASD Services ResourcesAutism ResourcesFlorida Department of H.docx
ASD Services ResourcesAutism ResourcesFlorida Department of H.docx
 
ASCI 615 AviationAerospace Accident Investigation and.docx
ASCI 615 AviationAerospace Accident Investigation and.docxASCI 615 AviationAerospace Accident Investigation and.docx
ASCI 615 AviationAerospace Accident Investigation and.docx
 
ASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docx
ASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docxASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docx
ASCM 631 – Integrative Supply Chain Management – Midterm Examinati.docx
 
ASB3109 Strategic ManagementYour Individual Assignment.docx
ASB3109 Strategic ManagementYour Individual Assignment.docxASB3109 Strategic ManagementYour Individual Assignment.docx
ASB3109 Strategic ManagementYour Individual Assignment.docx
 
asapnursingProvide a Topic of Health Promotion Paper for App.docx
asapnursingProvide a Topic of Health Promotion Paper for App.docxasapnursingProvide a Topic of Health Promotion Paper for App.docx
asapnursingProvide a Topic of Health Promotion Paper for App.docx
 
Asap Essay Need, it needs to be 4-5pages long. I really want to get .docx
Asap Essay Need, it needs to be 4-5pages long. I really want to get .docxAsap Essay Need, it needs to be 4-5pages long. I really want to get .docx
Asap Essay Need, it needs to be 4-5pages long. I really want to get .docx
 
ASB 100Spring 2019Writing Assignment 3In this assignme.docx
ASB 100Spring 2019Writing Assignment 3In this assignme.docxASB 100Spring 2019Writing Assignment 3In this assignme.docx
ASB 100Spring 2019Writing Assignment 3In this assignme.docx
 
asapnursingHealth policy unfolds daily and drives clinical p.docx
asapnursingHealth policy unfolds daily and drives clinical p.docxasapnursingHealth policy unfolds daily and drives clinical p.docx
asapnursingHealth policy unfolds daily and drives clinical p.docx
 
Asam100bbXinyu ShangReading journal week1In the article Im.docx
Asam100bbXinyu ShangReading journal week1In the article Im.docxAsam100bbXinyu ShangReading journal week1In the article Im.docx
Asam100bbXinyu ShangReading journal week1In the article Im.docx
 

Dernier

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Phase 4 Problem Solving with LoopsTotal orderDescriptionOnce .docx

  • 1. Phase 4: Problem Solving with Loops Total orderDescription Once the final prices are calculated for each product, we need to total up the order and then add in sales tax. We need to go through the line items in an order and figure out the final price.Flowchart Create variable Double: subtotal Return calculateSubtotal (subtotal, salesTaxRate) Subtotal = subtotal + lineItemTotal index = each item in lineItemPrice lineItemTotal= lineItemPrice[index] * lineItemQuantity[index] Pseudocode function totalOrder (lineItemPrice: Array, lineItemQuantity : Array, saleTaxRate : Double) returns double Create variable Double : subtotal for (index = each item in lineItemPrice) lineItemTotal = lineItemPrice[index] * lineItemQuantity[index] subtotal = subtotal + lineItemTotal end loop return calculateSubtotal (subtotal, salesTaxRate) end function
  • 2. Problems to Solve Fill in the following table by walking through the logic above. The idea is to analyze how the chart and pseudocode was created, because you will be doing this in a few minutes, so do not just jump to the easy answer. Follow the steps as if you are the computer executing the software designed. Problem lineItemPrice lineItemQuantity saleTaxRate Return amount 1 [5.50, 3.00, 2.25] [12, 3, 10] 5% 2 [5.50] [5] 5% 3 [] [] 5% Calculate ProfitsDescription Given the list of all the product prices and wholesale prices as well as a list of all the items sold for each product calculate the net profit for all the products.Flowchart <insert here>Pseudocode function totalProfits (allProductSalesNumbers: Array, allProductPrices : Array,
  • 3. allProductWholesalePrices : Array) returns double <add your logic here> end function Rock, Paper, ScissorsDescription To expand further, we want to try out logic in interacting with users. Here is a sample of a game, using loops and decisions to allow a person to play rock-paper-scissors with a computer.Pseudocode We have a helper function that we will use here that tells us who won. It will result in +1 if the player wins, a -1 if the computer wins, and a 0 if it is a tie. function compareOutcome(computerMove: Integer, playerChoice : Integer) returns Integer And the main flow of our logic is as such. while (true) Integer : computerChoice = randomChooser() Integer : playerEntry = prompt user to enter a value If (playerEntry is not a valid input) Prompt user they have picked a bad number and try again Else Integer : outcome = compareOutcome(computerMove, playerEntry) If (outcome = 0) Prompt user it is a tie, and try again Else if (outcome = -1) Prompt user they lost Break out of loop Else Prompt user they won Break out of loop End if
  • 4. End if end loopFlowchart true playerEntry is not a valid input outcome = 0 outcome = -1 Integer : computerChoice = ramdonChooser() Integer : playerEntry = prompt user to enter a value Prompt user they have picked a bad number and try again Integer : outcome = compareOutcome(computerMove, playerEntry) Prompt user it is a tie, and try again Prompt user they lost Break Prompt user they won End of game Break Note: In this case, note the advantages and disadvantages of flowcharts and pseudocode. The flowchart is easy to follow the logic for one scenario, but does it make it easier or harder to understand the code as a whole? There is no strict answer, but one that tells you a bit about your way of thinking and personal preference.Interaction Scenario For either of the solutions above, follow the logic and map out your interaction step-by-step, trying to exercise all paths of the logic. You will have to take at least 2 times through the game to map all scenarios. Scenario 1
  • 5. Step User Input System Response 1 User chooses 50 System says lower 2 … .. 3 4 Scenario 2 Step User Input System Response 1 2 3 4 (copy and add more if needed/desired) Number Guessing GameDescription Now it is your turn to try out user interaction with a simple
  • 6. game. This goes back to the number guessing game. The user will input a number each time, say between 1 and 100. The computer will randomly select 1 number each game (note this is different than the example above where it selects a new answer each round), and the game continues until the user guesses the right number. Like above, your solution should accommodate for invalid input, but do not worry about how to make that logic work for now. Your solution should give some hints though if the guess the user made was too high or too low, to help them on their way.Flowchart <Your solution here>Pseudocode <your solution here> Phase 5: Using Abstractions in DesignSeeing Abstractions Throughout this course, we have been realizing the detailed design for the pieces of the whole picture we started with at the beginning of the course. We have built many abstractions, so let’s go back and document what we have found and see the abstractions we have created. Abstraction Name Parameter List Scope/Purpose calculateSubtotal (totalPurchaseAmount :double, salesTaxRate : double) Applies sales tax and computes the final amount due <keep going>
  • 7. Refactoring Sometimes it is easy to see how the whole solution can be broken into pieces, and other times it is done as you see abstractions and opportunities for reuse in the resulting design. This is called refactoring. For the pseudocode you see below, look for abstractions you could create and create a flowchart using your simplifications.
  • 8. Psuedocode in Need of Refactoring Array : namesInSystem Array : phoneNumbersInSystem String : nameInput While (nameInput is not valid) nameInput = prompt user for input if (nameInput is provided and not blank) break out of loop end loop Prompt user the input is required and not blank End loop Integer : indexForName = -1 For (index = all items in namesInSystem If (nameInput = namesInSystem[index]) indexForName = index end if End loop If (indexForName = -1) Prompt user “We cannot find your account, please call us” Else String : phoneNumberInput While (phoneNumber is not valid) phoneNumber = prompt user for input if (phoneNumber is provided and not blank) break out of loop end loop Prompt user the input is required and not blank End loop If (phoneNumber = phoneNumbersInSystem[indexForName]) Prompt “we found your account welcome” Else Prompt “we cannot validate your account, try again later”
  • 9. End if End ifRefactored Flowchart The above pseudocode is long, but you can omit much of the details. You can accomplish the same logic removing more than 30 lines. You do not have to show all of the details removed if moved into an abstraction, simply name the abstraction and define the parameters to be passed to the call replacing the lines of code. <insert your flowchart here>