C
C#15mo ago
Jace

❔ i need help finding how many 10s, 5s, 2s and 1s are in the number 18.

Im really having trouble finding the solution... i tried the division and modulo.. heres the code ive done : //Goal: Write a program that asks the user for a cash amount and calculates the number of $10, $5, $2 and $1 bills that must be collected to obtain this amount. //varialbes int totalAmount; int tenDollars; int fiveDollars; int twoDollars; int oneDollars; //variable for when i have the final result of 10s,5s,2s and 1s. int resultTenDollars; int resultFiveDollars; int resultTwoDollars; int resultOneDollars; //contants const int amountTenDollars = 10; const int amountFiveDollars = 5; const int amountTwoDollars = 2; const int amountOneDollars = 1; //person enters a value for the varible in the console Console.WriteLine("Enter the total amount:"); totalAmount = int.Parse(Console.ReadLine()); //calcul ( this is where im not very sure what to do ) tenDollars = totalAmount/ amountTenDollars; resultTenDollars = tenDollars; //1,8 fiveDollars = totalAmount % 5; //1,8 resultFiveDollars = fiveDollars; twoDollars = totalAmount % 2; //1,8 resultTwoDollars = twoDollars; oneDollars = totalAmount % 1; //0,8 //showing the result to the console Console.WriteLine(tenDollars); //1 Console.WriteLine(fiveDollars); //3 Console.WriteLine(twoDollars); //0 Console.WriteLine(oneDollars); // 0 answer should be : 1 for 10s, 1 for 5s, 1 for2s and 1 for 5s ;/
84 Replies
Tvde1
Tvde115mo ago
How would you explain it to someone who needs to do it physically? To an actual worker at the cash register? Maybe if you put it into words, it will be easier to put into code
Moods
Moods15mo ago
^ that’s if you have a valid solution
Jace
JaceOP15mo ago
if i have to give to someone 18 dollars in those amount, ill start with doing 18 - 10 which is equal to 8 but how do i show the 1 on the console... then ill do 8 minus 5 = 3, 3 minus 2 ( 1 ) and 1- 1 ( 0 ) = 1 ten, 1 five, 1 two and 1 one. but im not sure how to show the ones on the console.. and if i use a bigger number ? i cant use the "-" sign..
Florian Voß
Florian Voß15mo ago
wouldn't you rather use division instead of subtraction? there might be more than one ten, more than one five etc. in the number. This should help: - divide number by 10 to know how many 10s there are - divide rest by 5 to know how many 5s there are - divide rest of that by 2 to know how many 2s there are - divide rest tof that by 1 to know how many 1s there are now the question of how to show the ones on the console should be self explanatory, thats the numbers how many 10s, 5s, 2s, 1s we have, which would be stored in a variable and you show it as usual with Console.WriteLine()
Jace
JaceOP15mo ago
yea ive tried the division but it doesnt give the right answer : 18 / 10 = 1,8. 1,8(rest) / 5 = 0,36 0,36 (rest) / 2 = 0,18 0,18(rest) /1 = 0,18 answer should be : 1 ten, 1 five, 1 two, 1 one the answer of the division is 1 ten, 0 five, 0 two, 0 one with the modulos, i get the answer : 8 tens, 3 fives, 1 two, 0 one 18mod10 =8 8mod5 = 3 3mod2 = 1 1mod1 = 0
Pobiega
Pobiega15mo ago
what do you mean by 1.8(rest)?
Jace
JaceOP15mo ago
the total amount is 18, so when i divide it by 10 ( 10$), it gives me 1,8
Pobiega
Pobiega15mo ago
thats not the rest thou thats the result the remainder of 18/10 is 8.
Jace
JaceOP15mo ago
oh shit isnt that modulos ?
Pobiega
Pobiega15mo ago
you need to use both. division is to see how many 10s etc you need but then after that, you want to divide the rest
Jace
JaceOP15mo ago
with the 8, how do i turn that in 1 in the console writeline ?
Pobiega
Pobiega15mo ago
?
Jace
JaceOP15mo ago
the answer of the modulo is 8, which means that there cant be anymore 10s going into the 18. that means that there only 1 10$ bill going in the 18. The problem is that it will show "8" on the console, not "1". it should show 1, because theres only 1 10$bill that fits in 18$
Pobiega
Pobiega15mo ago
is the code you pasted above still what you have? because that will not work, ofc
Jace
JaceOP15mo ago
heres the code im using rn : doesnt work btw
Pobiega
Pobiega15mo ago
$code
MODiX
MODiX15mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Pobiega
Pobiega15mo ago
dont just paste it. use the proper syntax so its readable
Jace
JaceOP15mo ago
wdym ?
Pobiega
Pobiega15mo ago
I mean read what the bot wrote.
Jace
JaceOP15mo ago
$codegif
Pobiega
Pobiega15mo ago
this will actually be harder to do with that many variables wouldn't you rather use a more appropriate datatype, like a dictionary?
Jace
JaceOP15mo ago
public static void Main()
(
//Goal: Write a program that asks the user for a cash amount and calculates the number of $10, $5, $2 and $1 bills that must be collected to obtain this amount.

//varialbes
int totalAmount;
int tenDollars;
int fiveDollars;
int twoDollars;
int oneDollars;
//variable for when i have the final result of 10s,5s,2s and 1s.
int resultTenDollars;
int resultFiveDollars;
int resultTwoDollars;
int resultOneDollars;
//contants
const int amountTenDollars = 10;
const int amountFiveDollars = 5;
const int amountTwoDollars = 2;
const int amountOneDollars = 1;

//person enters a value for the varible in the console
Console.WriteLine("Enter the total amount:");
totalAmount = int.Parse(Console.ReadLine());

//calcul ( this is where im not very sure what to do )

tenDollars = totalAmount % amountTenDollars;
fiveDollars = tenDollars % amountFiveDollars; ;
twoDollars = fiveDollars % amountTwoDollars;
oneDollars = twoDollars % amountOneDollars;

//showing the result to the console
Console.WriteLine(tenDollars);
Console.WriteLine(fiveDollars);
Console.WriteLine(twoDollars);
Console.WriteLine(oneDollars);
)
public static void Main()
(
//Goal: Write a program that asks the user for a cash amount and calculates the number of $10, $5, $2 and $1 bills that must be collected to obtain this amount.

//varialbes
int totalAmount;
int tenDollars;
int fiveDollars;
int twoDollars;
int oneDollars;
//variable for when i have the final result of 10s,5s,2s and 1s.
int resultTenDollars;
int resultFiveDollars;
int resultTwoDollars;
int resultOneDollars;
//contants
const int amountTenDollars = 10;
const int amountFiveDollars = 5;
const int amountTwoDollars = 2;
const int amountOneDollars = 1;

//person enters a value for the varible in the console
Console.WriteLine("Enter the total amount:");
totalAmount = int.Parse(Console.ReadLine());

//calcul ( this is where im not very sure what to do )

tenDollars = totalAmount % amountTenDollars;
fiveDollars = tenDollars % amountFiveDollars; ;
twoDollars = fiveDollars % amountTwoDollars;
oneDollars = twoDollars % amountOneDollars;

//showing the result to the console
Console.WriteLine(tenDollars);
Console.WriteLine(fiveDollars);
Console.WriteLine(twoDollars);
Console.WriteLine(oneDollars);
)
Pobiega
Pobiega15mo ago
much better.
Jace
JaceOP15mo ago
like math class ?
Pobiega
Pobiega15mo ago
? no?
Jace
JaceOP15mo ago
okok wdym by dictionnary
Pobiega
Pobiega15mo ago
I mean a Dictionary<int, int> where the key would be the dollar amount, and the value would be the number of that value ie 10:1 nevermind, we can do that later anyways, lets understand the maths involved here a bit better shall we? Given any starting sum, what is the first thing we do?
Jace
JaceOP15mo ago
yea alr im not sure
Pobiega
Pobiega15mo ago
really?
Jace
JaceOP15mo ago
wdym what we do with a starting sum
Pobiega
Pobiega15mo ago
your goal is to calculate how many 10s, 5s, 2s and 1s right?
Jace
JaceOP15mo ago
yea
Pobiega
Pobiega15mo ago
so.. describe the process
Jace
JaceOP15mo ago
i would say, we divide it
Pobiega
Pobiega15mo ago
say my starting sum is 28 good okay we divide it
Jace
JaceOP15mo ago
ok ok
Pobiega
Pobiega15mo ago
why do we divide it? whats the result of the division? and I dont mean the number, I mean what does it represent
Jace
JaceOP15mo ago
ok so we divide it to see how many lets say 10s are in that number
Pobiega
Pobiega15mo ago
okay, good then what?
Jace
JaceOP15mo ago
so if i divide 28 by 10, it give me 2,8. and this is where im stuck when i use the dividion. I would say, since there are 2 tens, i would take the 28, subtract it by 20 ( 2 tens ) = 8 and divide it by 5 what u think ?
Pobiega
Pobiega15mo ago
kinda, but can we generalize that? do we NEED to subtract the 20? isnt there another mathematical operator we already discussed that does that for us?
var startingNumber = 28;

var numberOfTens = startingNumber / 10; // 2 (integer division truncates)
var remainder = ?;
var startingNumber = 28;

var numberOfTens = startingNumber / 10; // 2 (integer division truncates)
var remainder = ?;
Jace
JaceOP15mo ago
modulo 😮 - then, we would take the 28, do modulo 10 =8 and then divide by 5 ?
Pobiega
Pobiega15mo ago
right!
Jace
JaceOP15mo ago
imma read the code gimme sec
Pobiega
Pobiega15mo ago
var remainder = startingNumber % 10; now here is where I come in and do the programmer thing Can we generalize this?
Jace
JaceOP15mo ago
the whole thing or simply the mod ?
Pobiega
Pobiega15mo ago
the whole thing can you write out the code like I did above, but for both 10 and 5?
Jace
JaceOP15mo ago
imma try it rn, gimme one sec ok so that what i did and the answer is now : 1 ten, 1 five, 1 two, 0 1s
Pobiega
Pobiega15mo ago
show the relevant code.
Jace
JaceOP15mo ago
tenDollars = totalAmount /10;
resultTenDollars = totalAmount % 10;
fiveDollars = resultTenDollars / 5;
resultFiveDollars = totalAmount % 5;
twoDollars = resultFiveDollars /2;
resultTwoDollars = totalAmount % 2;

oneDollars = resultTwoDollars / 1;
resultOneDollars = totalAmount % 1;
tenDollars = totalAmount /10;
resultTenDollars = totalAmount % 10;
fiveDollars = resultTenDollars / 5;
resultFiveDollars = totalAmount % 5;
twoDollars = resultFiveDollars /2;
resultTwoDollars = totalAmount % 2;

oneDollars = resultTwoDollars / 1;
resultOneDollars = totalAmount % 1;
Pobiega
Pobiega15mo ago
Not quite? lets read these out loud the number of ten dollar bills is the total amount divided by 10. (so far so good) resultTenDollars is the remainder (so far so good) the number of five dollar bills is the resultTenDollars divided by 5 (so far so good) resultkFiveDollars is the remainder of the total amount divided by 5 (???)
Jace
JaceOP15mo ago
Hmm i see like i do the reminder of 28( total). What i should do instead, IS do thé reminder of thé reminder of ten ( resultTenDollars) 28mod10= 8,meaning that for the fives, i should do 8/5 Right ?
Pobiega
Pobiega15mo ago
and to make this a lot nicer, why not keep the same variable for the remainder?
var numberOfTens = value / 10;
value = value % 10;

var numberOfFives = value / 5;
value = value % 5;
var numberOfTens = value / 10;
value = value % 10;

var numberOfFives = value / 5;
value = value % 5;
Jace
JaceOP15mo ago
Yea, will be lot nicer
Pobiega
Pobiega15mo ago
and now that we are at this step, do you perhaps see a pattern?
Jace
JaceOP15mo ago
Yea its the same pattern of division and rest/ modulos
Pobiega
Pobiega15mo ago
sure is
Jace
JaceOP15mo ago
Thx alors 🙂 Alot*
Pobiega
Pobiega15mo ago
we aint done here :p
Jace
JaceOP15mo ago
Fr ?
Pobiega
Pobiega15mo ago
well yes if you just copy the above section 4 times, you get ugly code
Jace
JaceOP15mo ago
Hmm i see
Pobiega
Pobiega15mo ago
thats why I suggested we use a different datatype instead of storing the results in 4 variables, how about we store it in 1? I think a Dictionary<int,int> would work really well here, as it makes it very clear what number is associated with what denomination
Jace
JaceOP15mo ago
Like storing them in one simple console.writeline ? Oh ok ok Im not sure what you mean by that tho
Pobiega
Pobiega15mo ago
do you know what a dictionary is/does? in C#, not in real life.
Jace
JaceOP15mo ago
Are the ints, the numbers 10,5,2,1 ?
Pobiega
Pobiega15mo ago
that would be the key here yes
Jace
JaceOP15mo ago
Hmm alr alr I havent seen it in my class yer Yet*
Pobiega
Pobiega15mo ago
its essentially a lookup table it lets you associate a key with a value
Jace
JaceOP15mo ago
Ok ill look it up
Pobiega
Pobiega15mo ago
btw, is this for class? in that case, you shold stop here.
Jace
JaceOP15mo ago
Yea its for m'y class
Pobiega
Pobiega15mo ago
if you show up to class with a bunch of code that, no offence, you clearly didn't write yourself.. it might not go so well go with the 4 copypasted lines with values changed, its fine
Jace
JaceOP15mo ago
Even if i dont consider it as * writing that myself* as well, i still think i learned alot from that.
Pobiega
Pobiega15mo ago
sure what I was going for was a method that takes in a list of denominations and a start value, and returns a dictionary of the correct change to give. but that will be a bit too advanced for where you are currently
Jace
JaceOP15mo ago
I dont think ill use the library tho, ill make sure to check it out tho
Pobiega
Pobiega15mo ago
library? you mean.. dictionary? 😄
Jace
JaceOP15mo ago
Yea 😂😅
MODiX
MODiX15mo ago
',' character is not allowed in the search, please try again. Query must match the following regex: ^[0-9A-Za-z.<>]$
Pobiega
Pobiega15mo ago
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-7.0 here its "full" name is Dictionary<TKey,TValue> and you can change TKey and TValue to be the types you need so in our case, that was int for both, but you could replace either or both
Jace
JaceOP15mo ago
alr ty so much 🙏
Accord
Accord15mo 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