❔ Improvements on my Algorithm?

Hi. I don't know whether I can ask such questions here, as this is my homework, but do you see how I could improve my code?
internal static void GetAllChange(double amount, int maxCoins, List<double> denominations, List<double> combination)
{
if (amount < 0 || maxCoins == 0 || denominations.Count == 0) return;

if (amount == 0)
{
if (combination.Count <= maxCoins)
{
Console.WriteLine((string.Join("; ", combination)));
}
return;
}

List<double> copy = new List<double>(denominations);
copy.RemoveAt(0);

GetAllChange(amount, maxCoins, copy, combination);

combination = new List<double>(combination) { denominations[0] };
GetAllChange(amount - denominations[0], maxCoins, denominations, new List<double>(combination));

return;
}

internal static string GetSmallestChange(double amount, int maxCoins, double[] denominations)
{
if (maxCoins <= 0 || amount <= 0 || denominations.Length == 0) return null;

for (int i = 0; i < denominations.Length; i++)
{
if (denominations[i] == Math.Round(amount, 2))
{
return denominations[i].ToString() + "; ";
}
}

for (int i = 0; i < denominations.Length; i++)
{
if (denominations[i] > amount) continue;

string res = GetSmallestChange(Math.Round(amount - denominations[i], 2), maxCoins - 1, denominations);

if (res != null)
{
return res + denominations[i].ToString() + "; ";
}
}

return "The ATM does not have the provided change! Please enter a smaller amount...";
}
internal static void GetAllChange(double amount, int maxCoins, List<double> denominations, List<double> combination)
{
if (amount < 0 || maxCoins == 0 || denominations.Count == 0) return;

if (amount == 0)
{
if (combination.Count <= maxCoins)
{
Console.WriteLine((string.Join("; ", combination)));
}
return;
}

List<double> copy = new List<double>(denominations);
copy.RemoveAt(0);

GetAllChange(amount, maxCoins, copy, combination);

combination = new List<double>(combination) { denominations[0] };
GetAllChange(amount - denominations[0], maxCoins, denominations, new List<double>(combination));

return;
}

internal static string GetSmallestChange(double amount, int maxCoins, double[] denominations)
{
if (maxCoins <= 0 || amount <= 0 || denominations.Length == 0) return null;

for (int i = 0; i < denominations.Length; i++)
{
if (denominations[i] == Math.Round(amount, 2))
{
return denominations[i].ToString() + "; ";
}
}

for (int i = 0; i < denominations.Length; i++)
{
if (denominations[i] > amount) continue;

string res = GetSmallestChange(Math.Round(amount - denominations[i], 2), maxCoins - 1, denominations);

if (res != null)
{
return res + denominations[i].ToString() + "; ";
}
}

return "The ATM does not have the provided change! Please enter a smaller amount...";
}
2 Replies
Vladimor •ᴗ•
private static double[] denominations = { 0.01, 0.02, 0.05, 0.10, 0.20, 0.50, 1.00, 2.00 };

static void GetAllChange()
{
Console.Clear();
ATM.GetAllChange(ATM.GetInput(), 10, denominations.ToList(), new List<double>());
Console.ReadKey();
}

static void GetSmallestChange()
{
Console.Clear();
Console.WriteLine(ATM.GetSmallestChange(ATM.GetInput(), 10, denominations.Reverse().ToArray()));
Console.ReadKey();
}
private static double[] denominations = { 0.01, 0.02, 0.05, 0.10, 0.20, 0.50, 1.00, 2.00 };

static void GetAllChange()
{
Console.Clear();
ATM.GetAllChange(ATM.GetInput(), 10, denominations.ToList(), new List<double>());
Console.ReadKey();
}

static void GetSmallestChange()
{
Console.Clear();
Console.WriteLine(ATM.GetSmallestChange(ATM.GetInput(), 10, denominations.Reverse().ToArray()));
Console.ReadKey();
}
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server