C
C#•3y ago
populus

Return amount of denominations

static void RunExerciseNineteen()
{
int price = 150;
Console.WriteLine($"It'll cost you: {price}\nHow much are you providing?");
int input = Convert.ToInt32(Console.ReadLine());
int[] denominations = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int temp = 0;

int retur = input - price; // Remainder. (Negative if insufficient).
Console.WriteLine(retur); // Print how much is owed.
if (retur < 0)
{
Console.WriteLine("You didn't provide enough funds.");
} else if (retur > 0)
{
for (int i = denominations.Length - 1; i >= 0; i--)
{
if (denominations[i] / retur == 1 && retur > denominations[i])
{
retur += denominations[i];
}
}
}
Console.WriteLine("Retur: " + retur);
}
static void RunExerciseNineteen()
{
int price = 150;
Console.WriteLine($"It'll cost you: {price}\nHow much are you providing?");
int input = Convert.ToInt32(Console.ReadLine());
int[] denominations = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int temp = 0;

int retur = input - price; // Remainder. (Negative if insufficient).
Console.WriteLine(retur); // Print how much is owed.
if (retur < 0)
{
Console.WriteLine("You didn't provide enough funds.");
} else if (retur > 0)
{
for (int i = denominations.Length - 1; i >= 0; i--)
{
if (denominations[i] / retur == 1 && retur > denominations[i])
{
retur += denominations[i];
}
}
}
Console.WriteLine("Retur: " + retur);
}
I'm having trouble constructing the logic behind returning bills. Am I on the right path? What am I missing?
16 Replies
Pobiega
Pobiega•3y ago
Isnt the expected output something like... "you get 1x 500, 2x 20, 1x 2 in return" retur += denominations[i]; seems very curious to me retur, before being modified, holds how much change you need to give back, as an int
populus
populusOP•3y ago
You're right, that's what the output is supposed to look like. I admit I am very confused. Should I create another array to hold the bills? Am I silly for thinking it can be done with 1 single 'retur' variable? I find myself removing and starting over and writing the same thing as before.
Pobiega
Pobiega•3y ago
it can't be done with a single int, no imagine it like so: the expected return for 752 should total 752 but its not 752 itself its 1x 500, 1x 200, 1x 50, 1x 2 My first instinct is to have your calculate method return a Dictionary<int,int> where the key is your denomination, and your value the count
populus
populusOP•3y ago
I will look into it. My beginner instinct is to create variables for each of the denominations but that feels so.. unsatisfactory.
Pobiega
Pobiega•3y ago
yeah don't do that the alternative to a dict would be an int[], where each position of the array matches the position of the denomination
populus
populusOP•3y ago
That I can wrap my head around.
static void RunExerciseNineteen()
{
int price = 150;
Console.WriteLine($"It'll cost you: {price}\nHow much are you providing?");
int input = Convert.ToInt32(Console.ReadLine());
int[] denominations = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int[] bills = new int[denominations.Length];

int retur = input - price; // Remainder. (Negative if insufficient).
Console.WriteLine("retur " + retur); // Print how much is owed.
if (retur < 0)
{
Console.WriteLine("You didn't provide enough funds.");
} else if (retur > 0)
{
for (int i = denominations.Length - 1; i >= 0; i--)
{
while (retur % denominations[i] > 0)
{
bills[i]++;
}
}
}
Console.WriteLine("Retur: " + retur);
}
static void RunExerciseNineteen()
{
int price = 150;
Console.WriteLine($"It'll cost you: {price}\nHow much are you providing?");
int input = Convert.ToInt32(Console.ReadLine());
int[] denominations = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int[] bills = new int[denominations.Length];

int retur = input - price; // Remainder. (Negative if insufficient).
Console.WriteLine("retur " + retur); // Print how much is owed.
if (retur < 0)
{
Console.WriteLine("You didn't provide enough funds.");
} else if (retur > 0)
{
for (int i = denominations.Length - 1; i >= 0; i--)
{
while (retur % denominations[i] > 0)
{
bills[i]++;
}
}
}
Console.WriteLine("Retur: " + retur);
}
I fear it's too simple.
Pobiega
Pobiega•3y ago
do you know about unit tests? if you wrote this as a pure function instead of as a void, you could slap together a test suite that would let you verify that things work as expected
populus
populusOP•3y ago
I do not know of unit tests.
Pobiega
Pobiega•3y ago
Would you like to learn?
populus
populusOP•3y ago
Sure
Pobiega
Pobiega•3y ago
I can jump on a voice call for a while if you have the time
populus
populusOP•3y ago
Yeah, join a channel and I'll pop in
Pobiega
Pobiega•3y ago
#dev-vc-0
public static Dictionary<int, int> AsChange(this int amount)
{
if (amount < 0)
throw new ArgumentException("Can't calculate negative change.", nameof(amount));

var dict = new Dictionary<int, int>();

foreach (var denomination in _denominations)
{
if (amount < denomination)
continue;

var count = amount / denomination;
dict.Add(denomination, count);
amount -= count * denomination;
}

return dict;
}
public static Dictionary<int, int> AsChange(this int amount)
{
if (amount < 0)
throw new ArgumentException("Can't calculate negative change.", nameof(amount));

var dict = new Dictionary<int, int>();

foreach (var denomination in _denominations)
{
if (amount < denomination)
continue;

var count = amount / denomination;
dict.Add(denomination, count);
amount -= count * denomination;
}

return dict;
}
populus
populusOP•3y ago
public static Dictionary<int, int> AsChange(int amount)
{ // Thanks to "Pobiega" from the C# Discord.
int[] _denominations = new int[] { 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 };

if (amount < 0)
throw new ArgumentException("Can't calculate negative change.", nameof(amount));

var dict = new Dictionary<int, int>();

foreach (var denomination in _denominations)
{
if (amount < denomination)
continue;

var count = amount / denomination;
dict.Add(denomination, count);
amount -= count * denomination;
}

return dict;
}

static void RunExerciseNineteen()
{ // Thanks to "Pobiega" from the C# Discord.
int price = 150;
Console.WriteLine($"It'll cost you: {price}\nHow much are you providing?");
int input = Convert.ToInt32(Console.ReadLine());
int[] denominations = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };

int retur = input - price; // Remainder. (Negative if insufficient).
Console.WriteLine("retur " + retur); // Print how much is owed.

var temp = AsChange(retur);
foreach (var i in temp)
{
Console.WriteLine(i);
}
}
public static Dictionary<int, int> AsChange(int amount)
{ // Thanks to "Pobiega" from the C# Discord.
int[] _denominations = new int[] { 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1 };

if (amount < 0)
throw new ArgumentException("Can't calculate negative change.", nameof(amount));

var dict = new Dictionary<int, int>();

foreach (var denomination in _denominations)
{
if (amount < denomination)
continue;

var count = amount / denomination;
dict.Add(denomination, count);
amount -= count * denomination;
}

return dict;
}

static void RunExerciseNineteen()
{ // Thanks to "Pobiega" from the C# Discord.
int price = 150;
Console.WriteLine($"It'll cost you: {price}\nHow much are you providing?");
int input = Convert.ToInt32(Console.ReadLine());
int[] denominations = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };

int retur = input - price; // Remainder. (Negative if insufficient).
Console.WriteLine("retur " + retur); // Print how much is owed.

var temp = AsChange(retur);
foreach (var i in temp)
{
Console.WriteLine(i);
}
}
Thanks for your help. I did it like this. Now I just need to learn how to deal with Dictionaries to create pretty print statements for the bills. (=
Pobiega
Pobiega•3y ago
when looping over a dictionary with foreach, you get access to both key and value
foreach (var kvp in change)
{
Console.WriteLine($"{kvp.Value}x {kvp.Key}");
}
foreach (var kvp in change)
{
Console.WriteLine($"{kvp.Value}x {kvp.Key}");
}
šŸ™‚
populus
populusOP•3y ago
Wonderful. You're an asset to this community.

Did you find this page helpful?