|  |
02-14-2008, 11:11 PM
|
#1 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Ontario, Canada Posts: 456
| C# Ideas - Working With Coins & Bills Eventually, Id like to work the program out so that it asks you how much the first name stored in a list made, then you put it in, then asks you how much the second name in the list made, etc, and once you type "done" it spits out the following info into an array and dumps it to a text file so it can be printed out.
Right now, Id like to keep it console-based, as thats what Im comfortable with, and Id like to understand what Im doing.
The entire idea behind the program is to help my mom out when she does payroll. Basically, youd enter the dollar value that the person made (above, when it asks), and the program would spit out (text for now is fine, Ill work it out into an array later) how many 100's, 50's, 20's etc, all the way down to pennies you have to give the person to make up the value that they earned.
I was planning on using the remainder operation, but Im at a loss for how it works. I was hoping somebody here is fluent in C# and would, at least, be able to give me some pointers or things to look at, or an example of a snippet of code that can do what Im looking for.
__________________ Asus P5E | Q6600 | 2x2GB Corsair Dominator DDR2-1066 | BFG 8800GTS | 5x1.5TB SATA2 | 2x500GB SATA2 in RAID1 |
| |
02-15-2008, 08:29 AM
|
#2 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: C# Ideas - Working With Coins & Bills This sounds like a homework question. If this program is really for your mother, she would be better off using a spreadsheet program, like Excel. Anyhow, you do not need to use the modulus operator (which gives the remainder of a division) for this problem. If you still want help, post what code you have written already, and I'll help you out. I'm just not going to do it for you -- especially if it's homework. |
| |
02-15-2008, 09:33 AM
|
#3 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Ontario, Canada Posts: 456
| Re: C# Ideas - Working With Coins & Bills Im in grade 12, we dont even have programming courses in highschool. I have a spreadsheet that Ive been using (Ill upload it, if youd like) but it gives wierd values (like 10 pennies) for certain values.
This is what I have so far (as you can see, Im stumped as for what to do after printing "converting x to change...") Code: double money, hundreds; //, fifties, twenties, tens, fives, toonies,
// loonies, quarters, dimes, nickels, pennies;
string name1 = "Jordan"; // , name2 = "", name3 = "";
Console.WriteLine("How much did " + name1 + " make?");
Console.Write("$");
money = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Converting {0:C} into change ...", money);
hundreds = (change / 100);
Console.WriteLine("Hundreds: "+ hundreds +"");
//Console.WriteLine("Fifties: "+ fifties +"");
//Console.WriteLine("Twenties: " + twenties +"");
//Console.WriteLine("Tens: " + tens +"");
//Console.WriteLine("Fives: " + fives +"");
//Console.WriteLine("Toonies: " + toonies +"");
//Console.WriteLine("Loonies: " + loonies +"");
//Console.WriteLine("Quarters: " + quarters +"");
//Console.WriteLine("Dimes: " + dimes +"");
//Console.WriteLine("Nickels: " + nickels +"");
//Console.WriteLine("Pennies: " + pennies +"");
Console.ReadKey();
__________________ Asus P5E | Q6600 | 2x2GB Corsair Dominator DDR2-1066 | BFG 8800GTS | 5x1.5TB SATA2 | 2x500GB SATA2 in RAID1 |
| |
02-15-2008, 10:57 AM
|
#4 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: C# Ideas - Working With Coins & Bills The algorithm for solving the problem is shown below. However, you'll probably want to put it in its own method so you don't keep copying and pasting code that performs the same operation several times in a row. Code: double money = 1234.70; // arbitrary value chosen
int hundreds = money / 100; // determine the number of hundreds
money -= hundreds * 100; // subtract all the hundreds
// now do the same thing for each group (fifties, tens, fives, ...)
Does this solve your problem? |
| |
02-15-2008, 04:29 PM
|
#5 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Ontario, Canada Posts: 456
| Re: C# Ideas - Working With Coins & Bills Looks like it would make sense. Ill try it when I get home from work.
And yeah, I was planning on just making a big list of names, and doing a foreach name in namelist {} kinda thing.
EDIT Hang on a sec. Isnt it going to cry that money is a double but hundreds is an int (wouldnt I need to Convert.ToInt32(money / 100) kinda thing?
__________________ Asus P5E | Q6600 | 2x2GB Corsair Dominator DDR2-1066 | BFG 8800GTS | 5x1.5TB SATA2 | 2x500GB SATA2 in RAID1
Last edited by SpikedCola; 02-15-2008 at 04:31 PM.
|
| |
02-15-2008, 09:49 PM
|
#6 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Ontario, Canada Posts: 456
| Re: C# Ideas - Working With Coins & Bills Hmm yeah kinda what I thought. When it converts to an int, it rounds, so lets say I put in $359.99, it spits out 4 hundreds. This is what Im using (since I was right about it crying): Code: double money; //, hundreds; //, fifties, twenties, tens, fives, toonies,
// loonies, quarters, dimes, nickels, pennies;
string name1 = "Jordan"; // , name2 = "", name3 = "";
Console.WriteLine("How much did " + name1 + " make?");
Console.Write("$");
money = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Converting {0:C} into change ...", money);
int hundreds;
hundreds = Convert.ToInt32(money / 100); // determine the number of hundreds
money -= hundreds * 100; // subtract all the hundreds
Console.WriteLine("Hundreds: "+ hundreds +"");
__________________ Asus P5E | Q6600 | 2x2GB Corsair Dominator DDR2-1066 | BFG 8800GTS | 5x1.5TB SATA2 | 2x500GB SATA2 in RAID1 |
| |
02-16-2008, 04:36 AM
|
#7 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Ontario, Canada Posts: 456
| Re: C# Ideas - Working With Coins & Bills My friend suggested another way of doing it: "Start with the highest denomination you want to give out, say $100s, and then go through a loop repeatedly subtracting $100 until you get a negative number, at which point you've reached the max number of hundreds. Then you move down to the next denomination."
Made perfect sense to me, so I came up with: Code: double money = 0;
string name1 = "Jordan"; // , name2 = "", name3 = "";
Console.WriteLine("How much did " + name1 + " make?");
Console.Write("$");
money = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Converting {0:C} into change ...", money);
int h = -1;
while (money >= 0)
{
money = money - 100;
h++;
}
Console.WriteLine("Hundreds: "+ h +"");
And it works perfectly! My next question, though, is how do I take what's left over before you hit a negative number, and find out how many fifties it takes to make that up?
__________________ Asus P5E | Q6600 | 2x2GB Corsair Dominator DDR2-1066 | BFG 8800GTS | 5x1.5TB SATA2 | 2x500GB SATA2 in RAID1 |
| |
02-16-2008, 04:51 AM
|
#8 (permalink)
|
Super Techie Join Date: Jan 2006 Location: Ontario, Canada Posts: 456
| Re: C# Ideas - Working With Coins & Bills Triple post, I know, but it helps me learn when I can go back and see what Ive done. Anyways, got it 100% done! Im sure its a bit crude, so what can I do to clean it up? Code: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Change_Thinger
{
class Program
{
static void Main(string[] args)
{
double money = 0;
string name1 = "Jordan"; // , name2 = "", name3 = "";
Console.WriteLine("How much did " + name1 + " make?");
Console.Write("$");
money = Convert.ToDouble(Console.ReadLine());
Console.WriteLine();
// take the total value, and keep subtracting the amount (100, 50, etc)
// until no more can be taken away (before going negative)
int h = -1;
while (money >= 0)
{
money = money - 100;
h++;
}
// somehow we end up with a negative, so add the original value back
money = money + 100;
int f = -1;
while (money >= 0)
{
money = money - 50;
f++;
}
money = money + 50;
int t = -1;
while (money >= 0)
{
money = money - 20;
t++;
}
money = money + 20;
int te = -1;
while (money >= 0)
{
money = money - 10;
te++;
}
money = money + 10;
int fi = -1;
while (money >= 0)
{
money = money - 5;
fi++;
}
money = money + 5;
int to = -1;
while (money >= 0)
{
money = money - 2;
to++;
}
money = money + 2;
int l = -1;
while (money >= 0)
{
money = money - 1;
l++;
}
money = money + 1;
int q = -1;
while (money >= 0)
{
money = money - .25;
q++;
}
money = money + .25;
int d = -1;
while (money >= 0)
{
money = money - .1;
d++;
}
money = money + .1;
int n = -1;
while (money >= 0)
{
money = money - .05;
n++;
}
money = money + .05;
int p = -1;
while (money >= 0)
{
money = money - .01;
p++;
}
money = money + .01;
Console.WriteLine("Hundreds: "+ h +"");
Console.WriteLine("Fifties: "+ f +"");
Console.WriteLine("Twenties: " + t +"");
Console.WriteLine("Tens: " + te +"");
Console.WriteLine("Fives: " + fi+"");
Console.WriteLine("Toonies: " + to +"");
Console.WriteLine("Loonies: " + l +"");
Console.WriteLine("Quarters: " + q +"");
Console.WriteLine("Dimes: " + d +"");
Console.WriteLine("Nickels: " + n +"");
Console.WriteLine("Pennies: " + p +"");
Console.ReadKey();
}
}
}
__________________ Asus P5E | Q6600 | 2x2GB Corsair Dominator DDR2-1066 | BFG 8800GTS | 5x1.5TB SATA2 | 2x500GB SATA2 in RAID1
Last edited by SpikedCola; 02-16-2008 at 01:51 PM.
|
| |
02-17-2008, 02:08 PM
|
#9 (permalink)
|
Software Developer Join Date: Mar 2006 Location: Columbus, OH Posts: 569
| Re: C# Ideas - Working With Coins & Bills The algorithm I specified above is just that -- an algorithm. The implementation details are left to you. You need to cast double values to ints and use the Math.Round() function once in the implementation of my algorithm. You do not need to use the Convert() function. Just use a simple cast. I think the algorithm you're using is a bit convoluted, but if it works and you understand what it's doing then it will serve your purpose.
To clean up what you have, you need to move similar logic into a single method. Anytime you're copying/pasting code or writing several blocks of code that does the same thing repeatedly, you need to move it into a method and just call that method repeatedly. |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |