1

Okay so classes got out a month ago and fall semester starts back up in 3 weeks. I wanted to do the homework in the book that we were never assigned. This one question gotta got me stuck because its in the second chapter. Its a program to give change back (the 92 cents is a compile time initialization). My question is... Is there any way to make this program more "dummy downed" than it is. And also I had to put (int) in front of my assignment of what anQuarter, anDime, etc were. Otherwise I was getting decimals afterwards. Why is that? Can an experienced programmer explain?

Also, the chapter talked about MOD so that is why I used it. This is before calling class methods are introduced and obviously before loops and arrays. So I couldnt use any of those tools. Supposed to be just a very basic "cave man" program...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication273
{
    class Program
    {
        static void Main(string[] args)
        {
        double change = 0.92;

        double quarter = 0.25;
        double dime = 0.10;
        double nickel = 0.05;
        double pennies = 0.01;

        double anQuarter = (int)(change / quarter);
        double anDime = (int)((change % quarter) / dime);
        double anNickel = (int)(((change % quarter) % dime) / nickel);
        double anPennies = (int)((((change % quarter) % dime) % nickel) / pennies);

        Console.WriteLine("The amount of quarters are....{0}", anQuarter);
        Console.WriteLine("The amount of dimes are....{0}", anDime);
        Console.WriteLine("The amount of nickels are....{0}", anNickel);
        Console.WriteLine("The amount of pennies are....{0}", anPennies);
        }
    }
}
AJP
  • 2,125
  • 3
  • 16
  • 22
Zoro
  • 695
  • 2
  • 7
  • 23

3 Answers3

7

At the very least employ decimal: Difference between decimal, float and double in .NET?

Then removing the duplication of code: See it live on http://ideone.com/ZY9qBm

using System;

class Program
{
    static void Main()
    {
        decimal change    = 0.92m;

        var coins = new [] { // ordered
            new { name = "quarter", nominal   = 0.25m }, 
            new { name = "dime", nominal      = 0.10m },
            new { name = "nickel", nominal    = 0.05m },
            new { name = "pennies", nominal   = 0.01m }
        };

        foreach (var coin in coins)
        {
            int count = (int) (change / coin.nominal);
            change -= count * coin.nominal;

            Console.WriteLine("{0} {1}", count, coin.name);
        }
    }
}

Prints

3 quarter
1 dime
1 nickel
2 pennies
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Rewrote the coin distribution to get rid of code duplication. For fun, also see http://stackoverflow.com/questions/15532840/any-better-way-to-implement-this/15534025#15534025 – sehe Aug 01 '13 at 22:43
  • 1
    +1 for the anonymous structure. I never thought to use them this way. – Steve Aug 02 '13 at 00:03
2

And also I had to put (int) in front of my assignment of what anQuarter, anDime, etc were. Otherwise I was getting decimals afterwards. Why is that? Can an experienced programmer explain

The (int) is converting the value in the expression to integer type. Using type casts like this are useful ways of converting from one data type to another. In this case, your calculation is resulting in a double data type. Casting this to int is the easiest way of knocking off all the decimal digits without rounding.

Constablebrew
  • 816
  • 1
  • 7
  • 23
1

A few thoughts...

I'd recommend decimal over double - with decimal, you get exact decimal representations, which is generally what you want with money. With double (or float), you get binary representations, which aren't always equal.

The reason you're getting decimals when you divide is that a double divided by a double will give you a double in C#. If you want to get int, you either have to truncate it like you're doing, or start with ints in the first place. An int divided by an int gets you an int.

To make the whole thing simpler, you can treat everything as whole numbers of cents, like:

int change = 92;

int quarter = 25;
int dime = 10;
int nickel = 5;
int pennies = 1;

int anQuarter = change / quarter;
int anDime = ((change % quarter) / dime);
int anNickel = (((change % quarter) % dime) / nickel);
int anPennies = ((((change % quarter) % dime) % nickel) / pennies);

For readability though, you can do it lots of different ways. One way (probably not the best way) is to just keep reducing the amount owed until you get to zero.

int anQuarter = 0, anDime = 0, anNickel = 0, anPennies = 0;
while (change >= 25)
{
    anQuarter++;
    change -= 25;
}
while (change >= 10)
{
    anDime++;
    change -= 10;
}
while (change >= 5)
{
    anNickel++;
    change -= 5;
}
while (change >= 1)
{
    anPennies++;
    change -= 1;
}
Joe Enos
  • 39,478
  • 11
  • 80
  • 136