473,943 Members | 17,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coin Change Program

1 New Member
Homework Assignment:

Write program to compute the coins necessary to return change made up of quarters, dimes, nickels, and pennies.
We are not allowed to use if/else or loops in this program.

I am a beginner who knows very little about C++. We are working from Deitel's fourth edition and using the Visual C++ 6.0 compiler. I have worked for days on this problem and cannot figure out the logic part of it.

I am using Windows 95 right now and don't have a compiler but have access to one later.

This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. void main ()
  5. {
  6.  
  7. float Qvalue=.25;//The value of a quarter
  8. float Dvalue=.10;//The value of a dime
  9. float Nvalue=.05;//The value of a nickel
  10. float Pvalue=.01;//The value of a penny
  11.  
  12. float change_amt;//Memory for user input
  13.  
  14. int Qcount=0;//Initialize quarter count to 0
  15. int Dcount=0;//Initialize dime count to 0
  16. int Ncount=0;//Initialize nickel count to 0
  17. int Pcount=0;//Initialize penny count to 0
  18.  
  19. Qcount+=1;//Increment Qcount by 1
  20. Dcount+=1;//Increment Dcount by 1
  21. Ncount+=1;//Increment Ncount by 1
  22. Pcount+=1;//Increment Pcount by 1
  23.  
  24. cout<<"Enter change amount in 2 digit decimal format" <<"\n";
  25. cin>>change_amt;//2 digit decimal format only
  26.  
  27. (change_amt !<=.00) || (change_amt !>.99);
  28.  
  29. cout<<"Change is comprised of:" 
  30.  
  31. <<Qcount "Quarter(s)" "\n"
  32. <<Dcount "Dime(s)" "\n"
  33. <<Ncount "Nickel(s)" "\n"
  34. <<Pcount "Penny/Pennies" "\n";
  35.  
  36. return 0;
  37. }
  38.  
The computer has to do the calculations but I don't know how to set it up. I was thinking:

Divide change by Qvalue and somehow using % to go from quarters to dimes, nickels and pennies using the leftovers?

The maximum count for Quarters is 3
The maximum count for Dimes is 2
The maximum count for Nickels is 1
The maximum count for Pennies is 4

The Coin counters have to be reset somehow for each input amount.

The assignment is due on February 20th.

Thanks for your help.
Feb 18 '07 #1
2 11372
Ganon11
3,652 Recognized Expert Specialist
The computer has to do the calculations but I don't know how to set it up. I was thinking:

Divide change by Qvalue and somehow using % to go from quarters to dimes, nickels and pennies using the leftovers?
This is exactly how you would solve this problem. Think about this:

If I have x cents in change, then x / 25 will tell me how many quarters I need. Round down, because you can't have a fraction of a quarter, and if you rounded up, there would be more money than original. Now that I know how many quarters I have, I can either

A: Subtract the value of my quarters from the total amount
B: Use mod to do it for me.

How would you use % to make sure the new change amount is less than 25 cents?
Feb 18 '07 #2
Ganon11
3,652 Recognized Expert Specialist
From a PM:

If I have (xcents/.25 % <.25)/.10 %<.10 and so on would this work???
I'm not sure if that sentence is syntactically correct, but if I'm correct, this is what you're trying to do:

Expand|Select|Wrap|Line Numbers
  1. xcents / 0.25;
  2. xcents = xcents % 0.25;
etc.

Is that correct?
Feb 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1692
by: Jon Monteleone | last post by:
Greetings, I posted a few days back and didnt get much of a response, so I figured I would post again with more detail. I am running gnome under fedora core 4. I want a kid to be able to drop a quarter into a coin slot and get 15 minutes of time on an account that has "restricted" Internet access via the firefox web browser. We are experimenting with different ways to allow kids to safely surf the web during school hours while...
12
2876
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
3
16953
by: viewsonic | last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t should have the following parameters. output is: Name Date total number of coins number of quarters number of dimes
52
5496
by: celerysoup16 | last post by:
I've written this coin toss program, and can't figure out why it isn't giving accurate results... cheers, Ben #include <stdlib.h> #include <stdio.h> #define H 1 #define T 0 #define SENTINEL -1
0
1570
by: coolindienc | last post by:
Honestly, I am totally lost about this program. I don't even know where to start. At first I thought I can ask a user to flip a coin. But then again one will get bored flipping coin fot 100 times. How do I get a computer to flip coin and get result? Any ideas??? Any help will be greatly appreciated greatly!!!!!!!! Andy
5
3854
by: sallyk57 | last post by:
I have to make a program that would ask a user for their guess (heads or tails) and tells them if its correct or not then it has to ask the user if they want to play again. I figured out how to do this but the part that i am stuck on is how to print a summary at the end of this program tracking how many wins and how many losses. import cs1.Keyboard; public class CoinToss6 { public static void main(String args) { ...
6
6093
blackstormdragon
by: blackstormdragon | last post by:
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine: #include<iostream> #include<cstdlib> using namespace std; int flip(); void main () { int coin, counter, tails = 0, heads = 0; for (counter = 1; counter <= 100; counter++)
1
16468
by: monkey1001 | last post by:
my program is suppose to show my due change and i got it working but my change and coins are wrong how can i improve it thank you..(its supposed to be in java)
8
24949
by: PAK11 | last post by:
This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result should be return to the main program. I'm having trouble figuring it out, can anyone please help? #include <iostream> using namespace std; # include <ctime> int coin();
0
9970
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11133
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10666
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9866
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8228
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6311
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4515
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.