Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please make sure your code will compile on Visual Studio 2015 correctly, and ple

ID: 3676782 • Letter: P

Question

Please make sure your code will compile on Visual Studio 2015 correctly, and please do not copy someone's code that is close to this assignment( but not really for this assignment). Really appreciate.

Develop a recursive function to determine the number of distinct ways in which a given amount of money in cents could be changed into quarters, dimes, nickels, and pennies. For example, if the amount is 17 cents, then there are six ways to make changes. Here is the sample output:

1) 1 dime, 1 nickel, and 2 pennies
2) 1 dime and 7 pennies
3) 3 nickels, and 2 pennies
4) 2 nickels, and 7 pennies
5) 1 nickels, and 12 pennies
6) 17 pennies


Here is the function interface:

// precondition: denomination = 1 (for penny), 2 (for nickel), 3 (for dime), or 4 (for quarter).
//
// postcondtion: If amount < 0, then 0 has been returned. Otherwise, the value returned is the
//               number of ways that amount can be changed into coins whose denomination is
//               no larger than denomination
int ways (int amount, int denomination);


For the sake of simplifying the ways function, develop a function coins that return the value of each denomination.

Thus coins(1) returns 1, coins(2) returns 5, coins(3) returns 10, and coins(4) returns 25.

Develop a main driver function that takes the user input of initial amount in cents and outputs the entire number of ways that amount can be changed into quarters, dimes, nickels, and pennies.

Also printout an optimal way (with coin count) that minimize the number of coins for the specified amount of cents.

Explanation / Answer

class Program
{

    // precondition: denomination = 1 (for penny), 2 (for nickel), 3 (for dime), or 4 (for quarter).

    const int penny = 1;
    const int nickel = 2;
    const int dime = 3;
    const int quarter = 4;

    const int way =17; //1-n where n<=99

    static void Main(string[] args)
    {       
        int minPenniesNeeded = 0;
        int minNickelsNeeded = 0;
        int minDimesNeeded = 0;
        int minQuartersNeeded = 0;


        if (way == penny)
        {
            minPenniesNeeded = 1;
        }
        else if (way < nickel)
        {
            minPenniesNeeded = MinCountNeeded(penny, way);              
        }
        else if (way < dime)
        {
            minPenniesNeeded = MinCountNeeded(penny, nickel - 1);
            minNickelsNeeded = MinCountNeeded(nickel, way);              
        }
        else if (way < quarter)
        {
            minPenniesNeeded = MinCountNeeded(penny, nickel - 1);
            minNickelsNeeded = MinCountNeeded(nickel, dime - 1);
            minDimesNeeded = MinCountNeeded(dime, way);
        }
        else
        {
            minPenniesNeeded = MinCountNeeded(penny, nickel - 1);
            minNickelsNeeded = MinCountNeeded(nickel, dime - 1);
            minDimesNeeded = MinCountNeeded(dime, quarter - 1);

            var maxPossilbleValueWithoutQuarters = (minPenniesNeeded * penny + minNickelsNeeded * nickel + minDimesNeeded * dime);
            if (way > maxPossilbleValueWithoutQuarters)
            {             
                minQuartersNeeded = (((way - maxPossilbleValueWithoutQuarters)-1) / quarter) + 1;
            }
        }


        var minCoinsNeeded = minPenniesNeeded + minNickelsNeeded+minDimesNeeded+minQuartersNeeded;

        Console.WriteLine(String.Format("Min Number of coins needed: {0}", minCoinsNeeded));
        Console.WriteLine(String.Format("Penny: {0} needed", minPenniesNeeded));
        Console.WriteLine(String.Format("Nickels: {0} needed", minNickelsNeeded));
        Console.WriteLine(String.Format("Dimes: {0} needed", minDimesNeeded));
        Console.WriteLine(String.Format("Quarters: {0} needed", minQuartersNeeded));
        Console.ReadLine();
    }

    private static int MinCountNeeded(int denomination, int upperRange)
    {
        int remainder;
        return System.Math.DivRem(upperRange, denomination,out remainder);
    }
}