473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create 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 11343
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
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...
12
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
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...
52
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...
0
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....
5
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...
6
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...
1
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.