C
C#14mo 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
Murten14mo 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
MODiX14mo 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
jaddersOP14mo 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
jaddersOP14mo ago
BlazeBin - elwkqappayom
A tool for sharing your source code with the world!
Murten
Murten14mo ago
You're probably just running into floating point precision errors.
jadders
jaddersOP14mo ago
ahh, okay, so I should use Floor, or Ceil? It happened when I converted it all to doubles too
Murten
Murten14mo 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.
Omnissiah
Omnissiah14mo ago
i would advise against using float and instead using decimal type for money it's the type made specifically for this case
Accord
Accord13mo 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