Psuedocode
Phase 4: Problem Solving with Loops
Total order
Description
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 Profits
Description
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, Scissors
Description
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 loop
Flowchart
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 Game
Description
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>