174k views
1 vote
Write the pseudocode that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in 5-cent increments (25, 30, 35, .. . 90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. Requirements and example output. (bold items are user input,) . Only coins with a non-zero value should be displayed Item price must be 25 cents to a dollar, in 5-cent increments. Enter item price: 75 You bought an item for 75 cents and gave me a dollar. Your change is: 1 quarter If more than one coin is returned, the word should be plural with an "s" at the end Ifonly one coin is returned, the word should not have an "s" at the end. Item price must be 25 cents to a dollar, in 5-cent increments Enter item price: 35 You bought an item for 35 cents and gave me a dollar. Your change is: 2 quarters 1 dime 1 nickel

User Mlathe
by
7.9k points

1 Answer

4 votes

Answer:

input price

set change to hundred minus price

set quarter to change divided by 25

set change to change mod 25

set dime to change divided by 10

set change to change mod 10

set nickel to change divided by 5

set change to change mod 5

set penny to change

print "You bought an item for price cents and gave me a dollar. Your change is: "

if quarter is equal to 1

print quarter

else if quarter is greater than 1

print quarter with "quarters"

if dime is equal to 1

print dime

else if dime is greater than 1

print dime with "dimes"

if nickel is equal to 1

print nickel

else if nickel is greater than 1

print nickel with "nickels"

if penny is equal to 1

print penny

else if penny is greater than 1

print penny with "pennies"

Step-by-step explanation:

One more understandable way of writing the above code is to use different names for change as:

input price

set change to hundred minus price

set quarter to change divided by 25

set quarter_remainder to change mod 25

set dime to quarter_remainder divided by 10

set dimes_remainder to quarter_remainder mod 10

set nickel to dimes_remainder divided by 5

set nickel_remainder to dimes_remainder mod 5

set penny to nickel_remainder

print "You bought an item for" price"cents and gave me a dollar. Your change is: "

if quarter is equal to 1

print quarter "quarter"

else if quarter is greater than 1

print quarter "quarters"

if dime is equal to 1

print dime "dime"

else if dime is greater than 1

print dime "dimes"

if nickel is equal to 1

print nickel "nickel"

else if nickel is greater than 1

print nickel "nickels"

if penny is equal to 1

print penny "penny"

else if penny is greater than 1

print penny "pennies"

I will explain the pseudocode with help of an example:

Suppose price = 75

Then change = 100 - price = 100 - 75 = 25

change = 25

quarter = change/25 = 25/25 = 1

quarter = 1

quarter_remainder = change % 25 = 25%25 = 0

dime = 0/10 = 0

Now all other values are 0.

Now the following line is printed on screen:

You bought an item for 75 cents and gave me a dollar. Your change is:

Now program moves to if part

if quarter == 1

This is true because value of quarter is 1

print quarter "quarter"

Now the following line is printed on screen:

1 quarter

So the complete output of the pseudocode is:

You bought an item for 75 cents and gave me a dollar. Your change is:

1 quarter

User Nitzanj
by
7.1k points