C
C#

help

Return amount of denominations

Ppopulus9/10/2022
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?
PPobiega9/10/2022
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
Ppopulus9/10/2022
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.
PPobiega9/10/2022
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
Ppopulus9/10/2022
I will look into it. My beginner instinct is to create variables for each of the denominations but that feels so.. unsatisfactory.
PPobiega9/10/2022
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
Ppopulus9/10/2022
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.
PPobiega9/10/2022
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
Ppopulus9/10/2022
I do not know of unit tests.
PPobiega9/10/2022
Would you like to learn?
Ppopulus9/10/2022
Sure
PPobiega9/10/2022
I can jump on a voice call for a while if you have the time
Ppopulus9/10/2022
Yeah, join a channel and I'll pop in
PPobiega9/10/2022
#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;
}
Ppopulus9/10/2022
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. (=
PPobiega9/10/2022
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}");
}
šŸ™‚
Ppopulus9/10/2022
Wonderful. You're an asset to this community.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
ASP.NET Video Recommendations?I'm trying to find some video's that go over how/why/where all of the code itself in ASP.NET gets paHotkey systemtrying to make a hotkey system, i'm not sure how to get the user's key input after a click of a buttConsole.ReadKey() binds to ConsolekeyinfoWell im just trying to create some keybindings and save them down in a .json file.. everything worksAsp.Net Core Localization Resource File Naming SchemeHi! Im following this tutorial (https://www.codemag.com/Article/2009081/A-Deep-Dive-into-ASP.NET-CorWpf won't compile because SomeClass do not exist in SomeNamespace (but it actually exist!) (solved)I have Wpf app that don't want to compile after adding Window.DataContext tag pointing to viewmodel double array yields only zeroes``` static void RunExerciseSixteen() { int[] ints = new int[10]; RanWhat does 'Intellisense' refer to?Is intellisense solely responsible for all feedback that you receive in the IDE? Or do other parts oCloud Blazor web app - Mitigate malicious crawlers from wasting container resourcesI have a cloud-related question. If I'm hosting a Blazor server web app and I want to prevent maliciSystem.AccessViolationExceptionI'm trying to create a simple multiplayer game using SignalR and Raylib, I wrote a really simple demHow can i achieve something like this```cs public class Test { public string Name{get;set;} } //And i want to do Test.Add(new string SShould I use && and || operators at the end of a line or beginning? [Answered]if (a || b || c) { } vs. if (a || b || c) { } Which is the more accepted standard? I jusConfused about interface default implementations [Answered]Trying out default implementations for methods in the interfaces for the first time. In the interfacAnyone have experience or know of an open source barcode reading library for images?There's of course a bunch of paid options, but I'm looking for an open source alternative if one exiTested it a lot of times but still giving unexpected errorusing System; class Program { public static void Main (string[] args) { Random numGen = nDockerfile cannot find dependent project in the parent directory``` . +---src | +---MarketData.Api | +---MarketData.Messages | +---MarketData.Subscriber Solution to mixing SQL queries with entity framework entitiesHi guys, So I have a code base that uses manually written SqlConnection queries AND entity frameworDLL Unable to load file or assembly [Answered]Hi, i've made my own DLL and when i try to create a new variable with a class from this DLL, it launASP.NET Core 6 - Strange Monitor.TryLock behaviorHi, I'm making some experiment to better understand the Monitor. Looks like something is off: I've wSystem.Diagnostics.EventLog [Answered]For some reason it's not appearing either with name or full name, I'm using .NET6 console appLINQ Lambda-Expression DiffIt's probably very simple, I'm just on the fence today. The Lambda version works as expected the Ex