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 = comp.
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>