Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I just started out in c++ and am trying to do a simple atm system where it will only allows user to withdraw with the least amount of notes but in the form of either $20 or $50..

I am trying to understand this question in which it ask me to create two functions - 1. bool canDispense (int amt) where it returns true if the amount can hand out in combination of 20s and 50s and return false otherwise, and 2. int dispense50s(int amt) returns the number of 50s that will result in least number of notes being hand out..

So assuming in Q1, if I got 5 bills of 50 and 2 bills of 20, then in Q2, it will means that the answer is 5?
Also was wondering in cases like this, is it alright to print out any output message that state how many bills of 20s and 50s you will be getting based on the above questions?

What I have tried:

I have tried incorporating the int dispense50s function into bool canDispense, but I am just not sure if I am misunderstanding my question..
Posted
Updated 23-Feb-16 19:30pm
v4
Comments
Mohibur Rashid 22-Feb-16 6:02am    
Say your amount is X
canDispense will verify whether X can represented by only $20 and/or $50
dispense50s will search the total number of notes(least count) which is required to dispense the money.

as example
i) 160 is valid
need 5 notes (2x50, 3x20)

ii) 165 is invalid
it cannot be represented by only $50 and $20

If you have tried anything, you should share it with us
xenas19 23-Feb-16 1:25am    
Hi there, I actually did write out my code, but I am not sure if I can even post my code for fear that people may misunderstand me in asking other people to do/review my homework. And so, I am trying my best to write in words on what I have tried etc. If posting of my code is possible, I will definitely do it
Mohibur Rashid 23-Feb-16 2:19am    
There are lots of companies who will review your every single line of code. So, no big deal. Besides, there is a chance some people will yell at you for your bad code(including me), but you will eventually learn something... you better post your code and tell us where is your problem.
xenas19 23-Feb-16 3:44am    
I have added in my code within my question. Please do take a look and feedback
Mohibur Rashid 23-Feb-16 4:02am    
Your algorithms are wrong. Try to workout the process with paper and pen.

If you want to know if the number is valid, what would be your method?
If you want to calculate the note count, what would be your method?

Follow john arndit answer

Q1 Can the value be handed out with 20 and 50 notes


As already noted this can be checked if the value can be divided without remainder by a combination of notes. This is obviously true if the value is a multiple of 20 or 50, or the value is a multiple of 10 with an additional condition. The additional condition is here a value of greater or equal than 70. To prove this write down a table:
10 - no
20 - 1 x 20
30 - no
40 - 2 x 20
50 - 1 x 50
60 - 3 x 20
70 - 1 x 50 + 1 x 20
80 - 4 x 20
90 - 1 x 50 + 2 x 20
100 - 2 x 50
110 - 1 x 50 + 3 x 20
120 - 2 x 50 + 1 x 20
130 - 1 x 50 + 4 x 20
140 - 2 x 50 + 2 x 20
...

Q2 The number of 50s for the least total numbers of notes


The function should only return the number of 50s. But to know this, you must also know the number of 20s to fullfill the requirement of least total numbers of notes. Looking at the above table, you will see that there will be max. four 20 notes. So you need to write an algorithm to get the number of 20 and 50 notes for the input value:

  • Check if value is a multiple of 50. If so number is value divided by 50
  • Check if value minus 20 is a multiple of 50. If so number is value minus 20 divided by 50
  • Check if value minus 40 is a multiple of 50. If so number is value minus 40 divided by 50
  • Check if value minus 60 is a multiple of 50. If so number is value minus 60 divided by 50
  • Check if value minus 80 is a multiple of 50. If so number is value minus 80 divided by 50
  • Otherwise return zero (no 50 note)
 
Share this answer
 
v2
Comments
CHill60 22-Feb-16 7:13am    
5'd - particularly for pointing out the 110 - which I completely missed in my simplistic solution
Jochen Arndt 22-Feb-16 7:29am    
Thank you.

I wrote my solution because I noticed that yours did not cover all combinations.

But you were right that this is a single problem with two (or three) results:
The number of 20 and 50 notes (and if cash can be dispensed).

So an implementation might use a calculation function and the functions from the assignment would just return the stored results.
nv3 22-Feb-16 9:32am    
Good analysis, Jochen!
Mohibur Rashid 23-Feb-16 2:27am    
To check whether the value is valid or not:
if value mod 20 is zero or (value mod 10 is zero and value is greater than 49)

I agree with the second part
xenas19 23-Feb-16 4:20am    
I am a bit lost for Q2, why would you minus 20 and 40?
If I was approaching this homework problem this is what I would do ...

Q1. CanDispense(amt)
- Can I divide amt by 50 and if so is there anything left?
- Can I divide the remainder by 20 and if so is there anything left?
- If I take the number of 50's plus the number of 20's does it equal amt?

So I'm going to need Integer Division (/) and possibly the remainder (modulus[^])

Q2. Dispense50s(amt)
- Can I divide amt by 50 and what is the result?

So I'm going to need Integer Division (/). And nothing else really - I'll either get 0 (zero) or the number of 50-bills I need.

Hang on a minute, that Dispense50s function could be called from the CanDispense function. Hm ... I could do a Dispense20s function as well and use that in the CanDispense function too.

So now my CanDispense function is going to look like
1. How many 50's do I need
2. How Many 20's do I need
3. If I add up all the bills I have does it equal the original amount?

Member 12343138102 said:
So assuming in Q1, if I got 5 bills of 50 and 2 bills of 20, then in Q2, it will means that the answer is 5?
No. See the quote from Kornfield Eliyahu Peter. Your homework as it stands does not ask you to get the number of 50-bills in Q1. However if you look at my train-of-thought you can use that information the other way around ... so you were actually on the right track.

Member 12343138102 said:
Also was wondering in cases like this, is it alright to print out any output message that state how many bills of 20s and 50s you will be getting based on the above questions?
That is entirely up to you, and possibly your tutor. Certainly it's a good idea when debugging but you might not want it in your final program. It's likely to be the next stage in the assignment anyway :)

Give it a go, and come back if you get stuck on the actual results
 
Share this answer
 
Comments
xenas19 23-Feb-16 1:35am    
Hi CHill60, thanks for the input. That was very informative and very helpful, especially so to a beginner like me. Like I have mentioned above - in case you missed it, I did write up my code already, it is just that I am not so sure if I can even post my code here in the first place. If it is alright, please do let me know, and I would really appreciate if anyone can feedback me on it (its structure, contents etc)

Within my code, I have placed Dispense50s function into CanDispense Function. So far it seems to be working for me, at least in my POV, I don't think I have incorporated any brute-forcing into it
Maciej Los 23-Feb-16 10:37am    
+5! Interesting explanation.
Ok, you have changed the original question by adding the piece of code you wrote and you still want extra guidance:
xenas19[^] wrote:
The reason I have asked this (question) is mainly because I am not even sure if I am on the right track. Just want to have some assurance


So...
CanDispense function is wrong. I don't want to explain why, i prefer to explain how to write proper function.

Because you're a beginner...

Imagine, you have got only 2 types of coins/notes: 20 and 50, similar to cash machine. Someone wants to get his money, let's say 170$. Inside CanDispense function you should check all possible combinations of 20's and 50's which will give you a 170$. How to achieve that? In pseudo code:
C#
//all banknotes: 
int[] coins = {20,50};
//amount to exchange/dispense
int amt = 170;
//get the max. count of ... 
//20's in amount - in this case 8 plus some value after decimal separator
int a = (int)(amt / coins[0])+1;
//50's in amount - in this case 3 plus some value after decimal separator
int b = (int)(amt / coins[1])+1;
//+1 is extra added - why? you'll see in next step
//now you have to find all possible combinations of 20's and 50's to dispense requested amount of money
//the simplest way to "visualize" that is to create 2-dimensional array which will hold the sum of 20*n + 50*n, where "n" is a multiple of banknote
//you don't need to create it, because your function should return true if it finds the first match (the others are unimportant).
int[,] ways = new int[a,b];


A table should looks like:

iteration0123
0050100150
12070120170
24090140190
360110160210
480130180230
5100150200250
6120170220270
7140190240290
8160210260310


As you can see, the requested amount is possible to achive only with 2 combinations, when you get
1x20 + 3x50 = 170<br />
6x20 + 1x50 = 170


How to fill a table and check if a sum of 20's and 50's is equal to requested amount? Using 2 loops:
C#
for (int i = 0; i<a; i++)
{
	for (int j = 0; j<b; j++)
        {
                //k & l are used to show a way how to calculate multiple
                //do not create additional variables if they are unnecessary
		int k = a-(a-i);  //calculate multiple of 20's 
		int l = b-(b-j);  //calculate multiple of 50's
                //as i mentioned, array is used to visualize data only! 
		ways[i,j] = coins[0]*k + coins[1]*l;  //calculate a sum 
		if (ways[i,j]==amt)
			return true;//Console.WriteLine("{0}x{1}+{2}x{3}={4}", i, coins[0], j, coins[1], ways[i,j]);
	}
}
return false;  //a payment is impossible


Got it?
 
Share this answer
 
v4
Comments
xenas19 23-Feb-16 10:58am    
Thanks for posting your solution. Sadly I have yet to learn arrays yet, not to mention implementing it. As you can see from my code, it is written in very basics or beginner form.. Not that I can't use yours but I would like to try and see if I can make the code work with the current knowledge that I had (I concur it is not very much and it may be very hard to write this system...)

But I really appreciate it as it broaden my horizon, giving me another insight on tackling the issue :)
Maciej Los 23-Feb-16 13:00pm    
You're very welcome.
Philippe Mori 23-Feb-16 18:48pm    
Array are essentially useless in that case. One can easily see that you check the result immediatly after storing it in the array...

Also computation of k and l are nothing than obfuscation. a - (a - i) is equal to i except if you activate option to check range and the value is out of range.
Maciej Los 24-Feb-16 6:41am    
Good points, Philippe. My intention to use additional variables (k and l) was based on information that OP is absolutely beginner. An array is used to "visualize" the result only. I forgot to mention that. Thank you for all your valuable remarks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900