Results 1 to 7 of 7

Thread: [RESOLVED] Trying to find best code

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] Trying to find best code

    Friend I am trying to find best code so I can convert cents into minimum format here is my function

    Code:
     public static Change getChange(int cents, Change availableChange)
            {
                /*
                * This method should take two parameters
                1.cents as an Integer, and
                2.availableChange as a Change object (see below)
                * It should return the same amount, or the maximum amount
                possible,
                in dollars and coins given what change is available in the
                availableChange parameter.
                * It should use the minimum number of coins possible,
                given what is available in availableChange.
                For example: 164 cents = 1 dollar, 2 quarters, 1 dime and 4
                cents.
                * Return null if the parameter is negative.
                */
                int dollars; // 100 cents
                int quarters; //25 cents
                int dimes; // 10 cents
                int nickels; // 5 cents
                int centbalance; // 1 cent
                if (cents < 0)
                    return null;
                if (cents >= 100)
                    quarters = Math.DivRem(cents, 100, out dollars);
                //if (quarters >= 25)
                //    quarters = Math.DivRem(quarters, 25, out dollars);
    
                //availableChange = new Change(dollars, quarters, dimes, nickels, centbalance);
                return availableChange;
            }
    
       public class Change
            {
                private int _dollars; // 100 cents
                private int _quarters; //25 cents
                private int _dimes; // 10 cents
                private int _nickels; // 5 cents
                private int _cents; // 1 cent
                public Change(int dollars, int quarters, int dimes, int nickels,
                int cents)
                {
                    _dollars = dollars;
                    _quarters = quarters;
                    _dimes = dimes;
                    _nickels = nickels;
                    _cents = cents;
                }
                public int getDollars()
                {
                    return _dollars;
                }
                public int getQuarters()
                {
    
                    return _quarters;
                }
                public int getDimes()
                {
                    return _dimes;
                }
                public int getNickels()
                {
                    return _nickels;
                }
                public int getCents()
                {
                    return _cents;
                }
            }
    Update me if you have idea about it or have shortest code!

    Shakt

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Trying to find best code

    If you are using 3.0, you can use auto-implemented properties for variables.
    c# Code:
    1. public int dollars {get; set;}

    will do that job instead of
    c# Code:
    1. private int _dollars;
    2. public int dollars{
    3. return _dollars;
    4. }

    That's one way of making your code more readable.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question Re: Trying to find best code

    I did this code let me know some one have better way!

    C# Code:
    1. public static Change getChange(int cents, Change availableChange)
    2.         {
    3.             int dollars = 0; // 100 cents
    4.             int quarters = 0; //25 cents
    5.             int dimes = 0; // 10 cents
    6.             int nickels = 0; // 5 cents
    7.             int centbalance = 0; // 1 cent
    8.             if (cents >= 100)
    9.                 dollars = Math.DivRem(cents, 100, out quarters);
    10.             else
    11.                 quarters = cents;
    12.             if (quarters >= 25)
    13.                 quarters = Math.DivRem(quarters, 25, out dimes);
    14.             else
    15.                 dimes = quarters;
    16.             if (dimes >= 10)
    17.                 dimes = Math.DivRem(dimes, 10, out nickels);
    18.             else
    19.                 nickels = dimes;
    20.             if (nickels >= 5)
    21.                 nickels = Math.DivRem(nickels, 5, out centbalance);
    22.             else
    23.                 centbalance = nickels;
    24.             availableChange = new Change(dollars, quarters, dimes, nickels, centbalance);
    25.             return availableChange;
    26.         }

  4. #4
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Trying to find best code

    Here's what I did to your code
    c# Code:
    1. public struct Change
    2.     {
    3.        public int dollars;
    4.        public int quarters;
    5.        public int dimes;
    6.        public int nickles;
    7.        public int pennies;
    8.     }
    9.        public static Change getChange(int cents)
    10.        {
    11.            Change mychange = new Change();
    12.  
    13.            if (cents > 100)
    14.                mychange.dollars = Math.DivRem(cents, 100, out mychange.quarters);
    15.            else
    16.                mychange.quarters = cents;
    17.            if (mychange.quarters > 25)
    18.                mychange.quarters = Math.DivRem(mychange.quarters, 25, out mychange.dimes);
    19.            else
    20.                mychange.dimes = mychange.quarters;
    21.            if (mychange.dimes > 10)
    22.                mychange.dimes = Math.DivRem(mychange.dimes, 10, out mychange.nickles);
    23.            else
    24.                mychange.nickles = mychange.dimes;
    25.            if (mychange.nickles > 5)
    26.                mychange.nickles = Math.DivRem(mychange.nickles, 5, out mychange.pennies);
    27.            return mychange;
    28.        }

    You can use a class called change instead of a struct

    Here's the calling program
    c# Code:
    1. static void Main(string[] args)
    2.        {
    3.            Change myc = new Change();
    4.            myc = getChange(2430);
    5.  
    6.            Console.WriteLine(String.Format("Dollars:{0},Quarters:{1},Dimes:{2},Nickles:{3},Pennies{4}",myc.dollars, myc.quarters, myc.dimes, myc.nickles, myc.pennies));
    7.  
    8.        }

    The only thing I would like to change is send the value as an argument to Main instead of hardcoding it like that.

    Does this make it easier to read?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  5. #5
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Trying to find best code

    I just realized that above code has a bug in it.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  6. #6
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Trying to find best code

    I think this change should fix it.
    c# Code:
    1. public static Change getChange(int cents)
    2.        {
    3.            Change mychange = new Change();
    4.  
    5.            if (cents > 100)
    6.                mychange.dollars = Math.DivRem(cents, 100, out mychange.quarters);
    7.            else
    8.            {
    9.                mychange.quarters = cents;
    10.                mychange.dollars = 0;
    11.            }
    12.            if (mychange.quarters > 25)
    13.                mychange.quarters = Math.DivRem(mychange.quarters, 25, out mychange.dimes);
    14.            else
    15.            {
    16.                mychange.dimes = mychange.quarters;
    17.                mychange.quarters = 0;
    18.            }
    19.            if (mychange.dimes > 10)
    20.                mychange.dimes = Math.DivRem(mychange.dimes, 10, out mychange.nickles);
    21.            else
    22.            {
    23.                mychange.nickles = mychange.dimes;
    24.                mychange.dimes = 0;
    25.            }
    26.            if (mychange.nickles > 5)
    27.                mychange.nickles = Math.DivRem(mychange.nickles, 5, out mychange.pennies);
    28.            else
    29.            {
    30.                mychange.pennies = mychange.nickles;
    31.                mychange.nickles = 0;
    32.            }
    33.            return mychange;
    34.        }
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  7. #7
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Trying to find best code

    Code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MoneyChange
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Console.WriteLine("How much money should be changed to the best common amounts?");
    			string sAmount = Console.ReadLine();
    
    			decimal iAmount = 0m;
    			if(!decimal.TryParse(sAmount.Replace("$", ""), out iAmount))
    			{
    				Console.WriteLine("The amount entered could not be converted to a number. Program exiting.");
    				return;
    			}
    
    			int iCents = (int) (iAmount * 100m);
    
    			List<MoneyDenomination> MoneyList = MoneyDenomination.CreateKnownMoney();
    
    			List<MoneyAmount> MoneyAmounts = new List<MoneyAmount>();
    
    			//The following assumes the money list is sorted from greatest to least
    			foreach(MoneyDenomination MoneyItem in MoneyList)
    			{
    				//See if we can divide it in to the remaining amount
    				int iNumberInAmount = iCents / MoneyItem.CentsValue;
    				if(iNumberInAmount > 0)
    				{
    					//This is significant change
    					MoneyAmounts.Add(new MoneyAmount(MoneyItem, iNumberInAmount));
    					iCents -= MoneyItem.CentsValue * iNumberInAmount;
    
    					if(iCents == 0)
    						break;
    				}
    			}
    
    			Console.WriteLine("-----------------------------------");
    			Console.WriteLine(string.Format("The best way to represent ${0} in change is:", iAmount));
    
    			foreach(MoneyAmount Amount in MoneyAmounts)
    			{
    				Console.WriteLine(string.Format("\t{0}\t{1}", Amount.Amount, Amount.MoneyItem.CommonName));
    			}
    
    			Console.WriteLine("Press <ENTER> Key To Exit");
    			Console.ReadLine();
    		}
    	}
    
    	public class MoneyAmount
    	{
    		public MoneyDenomination MoneyItem { get; set; }
    		public int Amount { get; set; }
    
    		public MoneyAmount(MoneyDenomination Money, int Amounts)
    		{
    			MoneyItem = Money;
    			Amount = Amounts;
    		}
    	}
    
    	public class MoneyDenomination
    	{
    		public string CommonName { get; set; }
    		public int CentsValue { get; set; }
    
    		public MoneyDenomination(string Name, int Cents)
    		{
    			CommonName = Name;
    			CentsValue = Cents;
    		}
    
    		public static List<MoneyDenomination> CreateKnownMoney()
    		{
    			List<MoneyDenomination> Money = new List<MoneyDenomination>();
    			Money.Add(new MoneyDenomination("Hundred Dollar Bills", 10000));
    			Money.Add(new MoneyDenomination("Ten Dollar Bills", 1000));
    			Money.Add(new MoneyDenomination("Five Dollar Bills", 500));
    			Money.Add(new MoneyDenomination("One Dollar Bills", 100));
    			Money.Add(new MoneyDenomination("Half Dollars", 50));
    			Money.Add(new MoneyDenomination("Quarters", 25));
    			Money.Add(new MoneyDenomination("Dimes", 10));
    			Money.Add(new MoneyDenomination("Nickels", 5));
    			Money.Add(new MoneyDenomination("Pennies", 1));
    
    			return Money;
    		}
    	}
    }
    Sample Output (tested):
    Code:
    How much money should be changed to the best common amounts?
    233.68
    -----------------------------------
    The best way to represent $233.68 in change is:
            2       Hundred Dollar Bills
            3       Ten Dollar Bills
            3       One Dollar Bills
            1       Half Dollars
            1       Dimes
            1       Nickels
            3       Pennies
    Press <ENTER> Key To Exit
    Comment out the Half Dollars line if you don't want them.
    Last edited by Lord_Rat; Jan 31st, 2011 at 06:19 PM.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width