Computer Science Homework Question

In summary, the conversation discussed the creation of pseudocode for an algorithm to calculate and display the change a customer should receive when purchasing items worth less than $20. The variables needed for this algorithm were also listed. Suggestions were given to use a series of if/else statements and a while loop to optimize the code. The conversation also touched on brainstorming and exploring different possibilities for the algorithm.
  • #1
NDiggity
54
0

Homework Statement


A customer brings purchases worth less than $20 to the till, and hands over a $20 bill. Write pseudocode for an algorithm to figure out what change the customer should receive -- i.e., how many $10 bills, how many $5 bills, how many toonies ($2), loonies ($1), quarters (0.25), dimes (0.10), nickels (0.05), and pennies (0.01). Begin by listing the variables you will need. Keep in mind that a proper solution will use as few coins as possible!

The Attempt at a Solution


I don't know how to start this. I named some variables, ie numberOfTens, numberOfFives, change, totalOfPurchases etc but I don't even know where to start. I just want a helping hand so I can get started. Thanks so much!
 
Physics news on Phys.org
  • #2
Note, do not use integer variable types. What variable types would allow you to store xx.xx, where xx are real #s?

Customer's money - price of goods = change.

You could use a series of if/else statements to test for the conditions, but you have to becareful. Well, from the problem statement we know that he must buy something totalling less than 20 (even if the item he buy is at minimum $0.01 cent).

AFTER you calculate the difference, you know the amount of change.

You can say if the change is GREATER than 10, subtract 10 from the change and increase the variable that holds the # of tens bills to hand out. You are guaranteed $9.99 as your max value (after subtracting $10), even if the customer purchases something of $0.01 cent value. Note, if the change is $8.31 for ex, the if condition will hold false and continue throughout the program (through the other if-else statements)

You can't give another ten, if you've subtracted one already and have $9.99. If you give him another $10 (in addition to the first one), you'll lose 1 cent. So now, check for fives. If you subtract 5 from 9.99, you'll receive 4.99, which means that numberofFives++... right? keep it going. 4.99 is your maximum after you've subtracted the appropriate number of fives.

Note, this program would only be good for handing the customer the highest # of bills possible, then the remaining cents if any. In real life, a customer may ask for 3 fives instead of a five and a ten.
 
Last edited:
  • #3
Thank you so much for your help! I'll reply back if I have any more question, but this should get me on the right track.
 
  • #4
• if change is >= ten dollars
o subtract ten dollars from change and add 1 to numberOfTens
• if change is >= than five dollars
o subtract five dollars from change and add 1 to numberOfFives
• if change is >= four dollars
o subtract four dollars from change and add 2 to numberOfToonies
• if change is >= two dollars but less than four dollars
o subtract two dollars from change and add 1 to numberOfToonies
• if change is greater than one dollar but less than 2 dollars
o subtract one dollar from change and add 1 to numberOfLoonies
• if change is less than one dollars
o if change is >= 75 cents
• subtract 75 cents from change and add 3 to numberOfQuarters
o if change is >= 50 cents but less than 75 cents
• subtract 50 cents from change and add 2 to numberOfQuarters
o if change is >= 25 cents but less than 50 cents
• subtract 25 cents from change and add 1 to numberOfQuarters
• if change is less than 25 cents
o if change is >= 20 cents
• subtract 20 cents from change and add 2 to numberOfDimes
o if change is >= 10 cents but less than 20 cents
• subtract 10 cents from change and add 1 to numberOfDimes
• if change is less than 10 cents
o if change is >= 5 cents
• subtract 5 cents from change and add 1 to numberOfNickels
• if change is less than 5 cents
o if change is 4 cents
• subtract four from change and add four to numberOfPennies
o if change is 3 cents
• subtract three from change and add three to numberOfPennies
o if change is 2 cents
• subtract two from change and add two to numberOfPennies
o if change is 1 cents
• subtract one from change and add one to numberOfPennies

It didn't tab like i did on word but you should get the general idea. Is this right or is there a more efficient way. Also, now that this is done, how do i set up my variables and display the change?
 
  • #5
That is one way of doing it. Your method seems to be correct, I didnt read each line, but the first few are good. Now, if the change is $4.99, the way you could optimize that code a little bit is to have a while loop, basically saying while the change is less than 4, and greater than or equal to 1, it will continue to subtract $1 from it.

Note, the method I suggested and what you did is very tedious. At least you have something to hand in for your HW.

I would sit and brainstorm other possibilities. Write all the things you know about C++ and see how you can implement them into your algorithm design.
 

Related to Computer Science Homework Question

What is Computer Science?

Computer Science is the study of computers and computational systems, including their principles, their hardware and software designs, their applications, and their impact on society.

Why is Computer Science important?

Computer Science is important because it enables us to solve complex problems, automate tasks, and create new technologies that improve our daily lives. It also helps us understand how computers work and how we can use them to further advance various fields, such as medicine, finance, and communication.

What skills do I need to excel in Computer Science?

To excel in Computer Science, you need to have a strong foundation in mathematics, logic, and critical thinking. You also need to have good problem-solving skills, attention to detail, and the ability to think creatively and analytically. Strong programming skills and the ability to learn and adapt to new technologies are also essential.

What are some common programming languages used in Computer Science?

Some common programming languages used in Computer Science include Java, Python, C++, JavaScript, and SQL. The choice of programming language depends on the task at hand and personal preference. It is important to have a good understanding of multiple programming languages to be a well-rounded computer scientist.

How can I prepare for a career in Computer Science?

To prepare for a career in Computer Science, you can start by taking courses in mathematics, computer science, and programming in high school. You can also enroll in a computer science degree program and gain hands-on experience through internships and personal projects. Staying up-to-date with the latest technologies and continuously learning and practicing programming are also crucial for a successful career in Computer Science.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • STEM Academic Advising
Replies
9
Views
2K
Replies
2
Views
159
  • STEM Career Guidance
Replies
2
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Precalculus Mathematics Homework Help
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
3
Views
2K
Back
Top