C
C#8mo ago
jadders

❔ Coin sorting issue

Hey guys, I'm building a little test app that gives you the smallest amount of coins that you can use to make a specific value. Here is the program running, and here is my code I'm having an issue, where when it gets to a certain number, it's breaking. Do I need to use Ceiling or Floor in the code? I just find it weird as I swear I built this exact same program with roughly the same code in Uni and it worked? Every if/else if statement is the same as these, and then it prints out the coins, but due to the weird issue, it never gets to 0. I guess I could add an if statement to check if it is less than 0.01 Thank you
No description
No description
10 Replies
Murten
Murten8mo ago
I suggest looking into the modulus operator. And you are not showing all the code so I can't tell why your current implementation is failing. $code
MODiX
MODiX8mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
jadders
jadders8mo ago
namespace Coin_Sorter
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input an amount of change");
float amountOfChange = float.Parse(Console.ReadLine());
int coinCount = 0;

while (amountOfChange > 0)
{
if (amountOfChange > 5)
{
amountOfChange -= 5;
coinCount++;
Console.WriteLine("taken 5");
Console.WriteLine(amountOfChange);
}
else if(amountOfChange > 2)
{
amountOfChange -= 2;
coinCount++;
Console.WriteLine("taken 2");
Console.WriteLine(amountOfChange);
}
else if(amountOfChange > 1)
{
amountOfChange -= 1;
coinCount++;
Console.WriteLine("taken 1");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.50f)
{
amountOfChange -= 0.50f;
coinCount++;
Console.WriteLine("taken 0.50");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.20f)
{
amountOfChange -= 0.20f;
coinCount++;
Console.WriteLine("taken 0.20");
Console.WriteLine(amountOfChange);
}


namespace Coin_Sorter
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input an amount of change");
float amountOfChange = float.Parse(Console.ReadLine());
int coinCount = 0;

while (amountOfChange > 0)
{
if (amountOfChange > 5)
{
amountOfChange -= 5;
coinCount++;
Console.WriteLine("taken 5");
Console.WriteLine(amountOfChange);
}
else if(amountOfChange > 2)
{
amountOfChange -= 2;
coinCount++;
Console.WriteLine("taken 2");
Console.WriteLine(amountOfChange);
}
else if(amountOfChange > 1)
{
amountOfChange -= 1;
coinCount++;
Console.WriteLine("taken 1");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.50f)
{
amountOfChange -= 0.50f;
coinCount++;
Console.WriteLine("taken 0.50");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.20f)
{
amountOfChange -= 0.20f;
coinCount++;
Console.WriteLine("taken 0.20");
Console.WriteLine(amountOfChange);
}


else if (amountOfChange > 0.10f)
{
amountOfChange -= 0.10f;
coinCount++;
Console.WriteLine("taken 0.10");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.05f)
{
amountOfChange -= 0.05f;
coinCount++;
Console.WriteLine("taken 0.05");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.02f)
{
amountOfChange -= 0.02f;
coinCount++;
Console.WriteLine("taken 0.02");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.01f)
{
amountOfChange -= 0.01f;
coinCount++;
Console.WriteLine("taken 0.01");
Console.WriteLine(amountOfChange);
}
}

Console.WriteLine("The minimum amount of coins is: ", coinCount);

}
}
}
else if (amountOfChange > 0.10f)
{
amountOfChange -= 0.10f;
coinCount++;
Console.WriteLine("taken 0.10");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.05f)
{
amountOfChange -= 0.05f;
coinCount++;
Console.WriteLine("taken 0.05");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.02f)
{
amountOfChange -= 0.02f;
coinCount++;
Console.WriteLine("taken 0.02");
Console.WriteLine(amountOfChange);
}
else if (amountOfChange > 0.01f)
{
amountOfChange -= 0.01f;
coinCount++;
Console.WriteLine("taken 0.01");
Console.WriteLine(amountOfChange);
}
}

Console.WriteLine("The minimum amount of coins is: ", coinCount);

}
}
}
jadders
jadders8mo ago
BlazeBin - elwkqappayom
A tool for sharing your source code with the world!
Murten
Murten8mo ago
You're probably just running into floating point precision errors.
jadders
jadders8mo ago
ahh, okay, so I should use Floor, or Ceil? It happened when I converted it all to doubles too
Murten
Murten8mo ago
You should use the modulus operator.
Erroneous Fatality
GeeksforGeeks
Find minimum number of coins to make a given value (Coin Change) - ...
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
not guilty
not guilty8mo ago
i would advise against using float and instead using decimal type for money it's the type made specifically for this case
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise 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
More Posts
✅ Higher or lower game, variable wont show inside the loop using console.write ;-; help please!✅need help!! (very very new to c#)hello! for my assignment, my teacher has asked that we keep track of values assigned to a double exc❔ error: Unable to create a 'DbContext' of type '' ". When adding migration to MySQL databasehi there! im just starting a webapi project and i wanted to use a mysql docker container and go cod❔ Visual Studio not detecting .NET 7I have installed the .NET 7 SDK, but its not showing up (I have run `dotnet sdk check`)KeyNotFoundException: The given key was not present in the dictionary.Hey guys. I am attempting to send paramaters over to another method in a different file and im getti❔ Garbage collection questionI see in many libraries examples where in DI ".AddScoped<>", ".AddTransient<>" is used instead of ".❔ CS0123No overload for 'CanvasMain_PreviewMouseMove' matches delegate for 'EventMouseHandler'Basically i'm trying to make a system where there is a grid with moveable objects - if you drag the ❔ EntityFramework - How do I figure out what ORM expects?```#region Assembly AgeOfSailOnline_DB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null // Age❔ what learn first as beginerr blazor or asp.neti want to go web dev route with c# and i am confused what is better learn first asp.net or blazor✅ Two calls to same microservice inside one transactionGreetings, I have a .NET Core Web client that is accessing a .NET Core API microservice (that uses