Algorithm

Pages: 1234
Here is a translation of your code. I made a few changes along the way:

- I used a single boolean variable to store whether the input was okay instead of a list.
- I simplified the computation of changeExpected.

There are other simplifications that can be made. For example, the if and elseif parts can be combined into one condition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
using namespace std;

int
main()
{
    int numberOfStudents;
    cin >> numberOfStudents;

    int currentAmountInMachine = 0;
    bool okay = true;		// replaces myList
    
    for (int i = 0; i < numberOfStudents; i++) {

	int money, numberOfBottles, changeExpected;
	cin >> money >> numberOfBottles;
	changeExpected = money - numberOfBottles*50; // simplified from python version
	
	if (changeExpected == 0) {
	    currentAmountInMachine += money;

	} else if (currentAmountInMachine - changeExpected >= 0) {
	    currentAmountInMachine += money;
	    currentAmountInMachine -= changeExpected;

	} else {
	    okay = false;
	}
    }

    if (!okay) {
	cout << "NO\n";
    } else {
	cout << "YES\n";
    }
}
Sorry all the test cases are alright except this one which brings Yes instead of No
15

50 1
50 1
50 1
50 1
200 1
100 1
100 1
200 1
100 1
50 1
50 1
50 1
50 1
50 1
200 1
Sorry I forgot to tell u that the machine only takes in coins of [50, 100, 200]
Thus the sixth customer can't get change of 50 because the machine will have coins of just [100 and 200] in it.
So the output should be No.

Anyone who can help me update the code please
Thanks in advance
Last edited on
You probably got reported because you neglected to mention this important fact until page 2 of the thread.

Can you post the entire problem please?
I'm very sorry for the confusion

Hello guys I'm working on a project about a vending machine which sells water at a price of 50.
At the beginning the machine has 0 coins.
The machine takes in only coins of 50,100 and 200.
If for example there is 100 and 200 in the machine but change needed is 50 it won't sell water.
The vending machine will not sell water if it doesn't have enough coins to give as a change.
The first input will be the number of buyers.
The buyer will input the amount of money and number of water bottles he wants.
The program prints No if it doesn't have enough coins as change and Yes if it has enough coins.
I've tried but I get wrong results

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;

int main()
{
    int no_students;
    cin >> no_students;
    int machinemoney = 0;
  
    bool okay = true;  
    
    for (int i = 0; i < no_students; i++) 
    {
  int studentmoney, no_bottles, Change;
  cin >> studentmoney >> no_bottles;  
  int Coinlist[4]={0,50,100,200 };
  Change = studentmoney - no_bottles*50;
     
  if (Change == 0 && Change == Coinlist[4])
  {
     machinemoney = machinemoney + studentmoney;
  }
      
  else if(machinemoney- Change>=0 && Change==Coinlist[4])
   {
     machinemoney = machinemoney + studentmoney;
      machinemoney = machinemoney - Change;
  }
 
   else {
      okay = false;
        }
    }
    
     if(!okay)
     {
       cout << "NO";
     }
     
    else 
    {
  cout << "YES";
    }
}
Help
Your machinemoney variable needs to be able to count the number of 50,100,200 coins actually in the machine.

If the machine has 200 worth of money as 100+50+50 and the student puts in a 200 coin and selects 2 bottles of water, what do you do?
You can either give the change as a single 100 coin, or as two 50 coins.

If the machine has 100 worth of money as just a 100 coin, and the student puts in a 100 coin and selects 1 bottle of water, what do you do?
Sure, you have the numerical funds to give the change, but not the actual coins to make the change.
So I should scan the student money for the coins and if there are coins to give change it should print yes while if no coins it should print no.
Thanks
Or I was thinking about putting the machine money in list then if Change required is in the machine money it should print yes or else no.
Am I wrong
The fact is I understand the logic but how to put it in a code is the problem
Some one please help please
Think of it like an ordinary cash register. The register has bins for each type of coin. When the clerk needs to make change, then take coins from the bins as needed.

Usually they take large coins first, then smaller and smaller coins until they change is made. For example, if you need to give 67 cents in change, you pull out two quarters (50 cents), then one dime (total=60 cents now), then one nickel (total = 65) and finally two pennies.

What if one of the bins is empty? In the above example, suppose you have no dimes. In that case, you move on to nickels and see if you have enough of those. In this case, if you have 3 nickels then everything works out.

So the hard part of this problem is in making change. Concentrate on that.
Hello Dee5,


Hello guys I'm working on a project about a vending machine which sells water at a price of 50.
The vending machine will not sell water if it doesn't have enough coins to give as a change.
The first input will be the number of buyers.
The buyer will input the amount of money and number of water bottles he wants.
The program prints No if it doesn't have enough coins as change and Yes if it has enough coins.

I've tried to write the code and it gives wrong results.


Maybe you should start with a full description of of the program and not what little bit you posted.

Next think more like a real vending machine. With what you have done it appears that your machine has an endless supply of water bottles to dispense. The question in is this the true?

You should not rely on any given transaction to determine if you have enough for change to sell a bottle of water. The vending should start with some change to work with and set aside some of the sales for change.

Not exactly sure what you are trying to do with the for loop, but as it has been mentioned there is no real use for it.

Not sure what you should be doing with the program, but here is an idea that might help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

 // <--- A better choice.
using std::cin;
using std::cout;
using std::endl;

int main()
{
	constexpr int COST{ 50 };
	constexpr int MAX50S{ 1000 };  // <--- Max change.
	constexpr int MAX100S{ 2000 }; // <--- Max change.

	int money{}, no_bottles{}, cost{};
	int machineInventory{ 50 };        // <--- Number of bottles in machine.
	int fiftys{ 500 }, hundreds{ 1000 }; // <--- Starting change in machine.
	//int remainder{ 500 }; // <--- Could use later.

	//remainder = money - (cost * no_bottles); // <--- Have not input a value for money yet.
	//cout << "Enter number of items:";        // <--- A vending machine should do one transaction at a time.
	//cin >> n;

	cout << "Enter number of bottles: ";
	cin >> no_bottles;

	if (no_bottles > machineInventory)
	{
		std::cout << "\n    Machine does not have enough product\n";
	}

	cost = COST * no_bottles;

	std::cout << "\n  Your cost is: " << cost << '\n';

	std::cout << "\n Please enter your money: ";
	std::cin >> money;

	// check if money entered should be kept for change.

	// Check money entered meets or exceeds cost.
	// If less ask for more money. Maybe allow quantity to be reduced to fit money.

	// <--- Rework for not enough change.
	if (money > fiftys || money > hundreds) // <--- "remainder" is the wrong variable to check. Better idea, but not tested.
		cout << "\n    Exact Change Only!\n";
	else
	{
		cout << "\n    Yes\n"; // <--- Maybe "Your drink is on the way"?
	}

	return 0;  // <--- Not required, but makes a good break point.
}


Enlighten us on what the program need to do and not ask how to fix something that, most likely, should not be there in the first place.

Andy
> The fact is I understand the logic but how to put it in a code is the problem
ok, explain the logic, write pseudocode
really, because I have no idea what you're trying to code
if (Change == 0 && Change == Coinlist[4])¿?

also, your indentation is awful, ¿python didn't teach you that?


> Usually they take large coins first
that works because the value of large coins are multiples of every smaller coin
if that's not the case, the greedy approach may fail.
Here's what I'm trying to code
The vending machine is guaranteed that it has enough water bottles to sell at the beginning but has zero coins in it.
Also at the end of the day all coins in the machine are removed so the other day the machine has zero coins but enough water bottles
Also the machine only takes in 50 100 200
So I the change required is 50 and machine has only 100 and 200
It should print No.
Also when the machine doesn't have enough change to give to student
It should print No
Otherwise if it has enough change and coins to give as change it should
Print yes
I understand the problem, you posted several times already
I want to see your proposed solution

if you have no idea what to do, create some scenarios and write the steps you take to solve them
I did write the solution in python but someone here helped me to translate into C++
The problem is that in my solution it doesn't consider the coins in the machine
For example if the machine has 200 which is 100 coin and 100 coin
And the change required is 50
My code will print YES because change is less than machinmoney
While it should print NO because the machine does not have a 50 coin in it
...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
include<iostream>
using namespace std;

int
main()
{
    int numberOfStudents;
    cin >> numberOfStudents;

    int currentAmountInMachine = 0;
    bool okay = true;		// replaces myList
    
    for (int i = 0; i < numberOfStudents; i++) {

	int money, numberOfBottles, changeExpected;
	cin >> money >> numberOfBottles;
	changeExpected = money - numberOfBottles*50; // simplified from python version
	
	if (changeExpected == 0) {
	    currentAmountInMachine += money;

	} else if (currentAmountInMachine - changeExpected >= 0) {
	    currentAmountInMachine += money;
	    currentAmountInMachine -= changeExpected;

	} else {
	    okay = false;
	}
    }

    if (!okay) {
	cout << "NO\n";
    } else {
	cout << "YES\n";
    }
}
Last edited on
> currentAmountInMachine += money;
You're still NOT counting how many 50, 100 and 200 coins you've received.

You need to count how many of each coin type is put into the machine if you want to be able to answer the how much change can be given question.

Get yourself a piece of paper, draw 3 circles on it labelled 50, 100 and 200 and put a box around all of them labelled 'vending machine'.

Then grab some real dimes, quarters, nickels (or any 3 denominations in a currency of your choice) and pretend you're putting money in the machine, getting change and taking out product.

The things inside your paper diagram labelled 'vending machine' WILL be things you need to have as variables in your code.

Ok I'm just trying to follow Salem advice to count coins
I've managed to do it but how can I get like the total coins instead of just multiple counts
I'm sorry if it's hard to understand

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
using namespace std;

int main()
{
    int no_students;
    cin >> no_students;
    int machinemoney = 0;
    bool okay = true;  
   
    for (int i = 0; i < no_students; i++) 
    {
  int studentmoney, no_bottles, Change;
  cin >> studentmoney >> no_bottles;
         
    int count50=0;
    int count100=0;
    int count200=0;
    
    if (studentmoney==50)
    {
      count50++;
    }
    if (studentmoney==100)
    {
      count100++;
    }
    if (studentmoney==200)
    {
      count200++;
    }
  
  Change = studentmoney - no_bottles*50;
  if (Change == 0)
  {
     machinemoney = machinemoney + studentmoney;
  }
      
  else if(machinemoney- Change>=0)
   {
     machinemoney = machinemoney + studentmoney;
      machinemoney = machinemoney - Change;
  }
 
   else {
      okay = false;
        }
    } 
    
     if(!okay)
     {
       cout << "NO";
     }
     
    else 
    {
      cout <<"YES";
    }
  }   
  
Pages: 1234