1

I'm working on a program for my C# class that is supposed to take an entered amount, as a double or not, and to find change in dollars, quarters, etc.,

I'm using the Greedy Algorithm, and I keep getting some sort of error that reads, "An unhandled exception of type 'System.IndexOutOfRangeException' occurred in 2D.exe".

I'm still relatively new to C#, and come from a Java and C++ Background.

So far I have my Money class:

using System;
using static System.Console;

namespace _2D
{
    class Money
    {
    private double dollars, cents;

    public void IncrementMoney() { }
    public void DecrementMoney() { }

    public Money(double dollarsncents)
    {
        double amountLeftOfDecimal = Math.Truncate(dollarsncents);
        double amountRightOfDecimal = Math.Floor(dollarsncents);

        this.dollars = Math.Round(amountLeftOfDecimal);
        //the following LOGIC needs to be wokred out:
        this.cents = Math.Round((amountRightOfDecimal * 100) / 100, 2);

    }

    public Money(int ddollars, int ccents)
    {
        this.dollars = ddollars;
        this.cents = ccents;
    }

    public override string ToString()
    {
        return String.Format(dollars + " dollars and " + cents + " cents.");
    }

    public void CoinAmounts(int inAmount, int remainder, int[] coins)
    {

        if((inAmount % 0.25) < inAmount)
    {
            coins[3] = (int)(inAmount / 0.25);
            remainder = inAmount % (1/4);
            inAmount = remainder;
        }
        if ((inAmount % 0.1) < inAmount)
        {
            coins[2] = (int)(inAmount / 0.1);
            remainder = inAmount % (1/10);
            inAmount = remainder;
        }
        if ((inAmount % 0.05) < inAmount)
        {
            coins[1] = (int)(inAmount / 0.05);
            remainder = inAmount % (1/20);
            inAmount = remainder;
        }
        if ((inAmount % 0.01) < inAmount)
        {
            coins[0] = (int)(inAmount / 0.01);
            remainder = inAmount % (1/100);
        }
    }
    public void PrintChange(int[] arr)
    {


        if (arr[3] > 0)
            Console.WriteLine("Number of quarters: " + arr[3]);
        if (arr[2] > 0)
            Console.WriteLine("Number of dimes: " + arr[2]);
        if (arr[1] > 0)
            Console.WriteLine("Number of nickels: " + arr[1]);
        if (arr[0] > 0)
            Console.WriteLine("Number of pennies: " + arr[0]);
    }
}

And my Main :

using System;

namespace _2D
{
    class Program
    {
        static void Main(string[] args)
        {
            Money MyMoney = new Money(23, 24);
            Console.WriteLine(MyMoney.ToString());

            Money dollarCentAmount = new Money(12.45);
            Console.WriteLine(dollarCentAmount.ToString());
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            Console.Clear();

            Console.WriteLine("Enter an amount you'd like change for: ");
            double inAmountDouble = Convert.ToDouble(Console.ReadLine());
            int inAmount = Convert.ToInt32(inAmountDouble);
            int tochange = inAmount;
            int remainder = 0;
            int[] coins = new int[3];

            MyMoney.CoinAmounts(inAmount, remainder, coins);
            Console.WriteLine(" Change for " + inAmount + " is: ");

            if (inAmount > 1.0)
            {
                Console.WriteLine("Number of dollars: " + Convert.ToInt32(inAmount));
            }
            MyMoney.PrintChange(coins);

            Console.ReadKey();
        }
    }
}
DiskJunky
  • 4,750
  • 3
  • 37
  • 66
  • 2
    `int[] coins = new int[3];` ... `coins` only holds 3 `int`s, so `coins[3] = (int)(inAmount / 0.25);` is out of range because array indexes start at 0. – 001 Oct 19 '17 at 19:33
  • `coins` should be part of the `Money` class, not in your `Main` program. – NetMage Oct 19 '17 at 19:34
  • can you elaborate @johnnyMopp ? – Lucas Toledo Oct 19 '17 at 19:42
  • Arrays are zero indexed: an array with n elements is indexed from 0 to n-1. [Single-Dimensional Arrays (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays) – 001 Oct 19 '17 at 19:49

1 Answers1

1

You declared coins to be an array going from 0 to 2

array[size] //size is how many elements are in the array, not the upper bound of the array
coins[3] //means the array contains three elements, elements: 0, 1, 2

//so you want:
int[] coins = new int[4]; //Goes from 0 to 3, elements: 0, 1, 2, 3

//This will allow you to later access:
//since coins[3] is the 4th element, this is as high as the array can go now
coins[3] = (int)(inAmount / 0.25);
Ryan
  • 25
  • 10