Algorithm

Pages: 1234
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.
Example
3
50 1
100 1
50 1
Output:Yes

Example
3
100 1
50 1
50 1
Output:No

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include<iostream>
using namespace std;
 int main()
 {
   int money, no_bottles, remainder, cost, n;
   cost = 50;
   remainder = money -(cost * no_bottles);
   cout <<"Enter number of items:";
   cin >> n;
   
   cout <<"Enter money and number of bottles\n";
   {
     for (int i=0; i<n; i++)
   cin >> money >> no_bottles;
   }
   if (money<remainder)
   cout <<"No";
   else 
   cout << "Yes";
 }


Last edited on
How much change does the machine start with? Zero?
Yes at first it has zero change
I see that you calculate remainder on line 8, using the variables money, cost and no_bottles.

Tell me, what values do all those variables have?
The values are inputted by the buyer
Tell me, ON LINE 8, where you calculate remainder, what value does money have?

This is BEFORE the user has done anything, so what value does money have?
Last edited on
Example of input
3 (which is the number of items or buyers)
50 (money) 1(no_bottles)
100 (money) 1(no_bottles)
50(money) 1(no_bottles)
Output: Yes
Because the first buyer doesn't need change
Second buyer needs change of 50 which the machine has it already
Third buyer doesn't need change.
On line 8, where you calculate remainder, what is the value of money?

Is it zero? Is it one? Is it 5076784876859? What is it?
Last edited on
Ohh maybe i had the wrong arrangement
I should have put the remainder at the bottom sorry
Also your for loop makes no sense.

1
2
     for (int i=0; i<n; i++)
   cin >> money >> no_bottles;


You are collecting all the input before you do any calculations, so you are throwing away all except the very last input.
Is this ok tho it still gives wrong output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
 int main()
 {
   int money, no_bottles, remainder, cost, n;
   cost = 50;
   
   cout <<"Enter number of items:";
   cin >> n;
     for (int i=0; i<n; i++)
   {
   cout <<"Enter money and number of bottles\n";
   cin >> money >> no_bottles;
   }
   remainder = money -(cost * no_bottles);
   if (money<remainder)
   cout <<"No";
   else 
   cout << "Yes";
 }
Your for loop still makes no sense.

You need to read the first user's data, and then do something with it, and then read the second user's data, and do something with it, and then read the third user's data and do something with it.

Instead, you are reading the first user's data, and then reading the second user's data, and then read the third user's data and then doing something with the third user's data. You aren't doing anything with the first user's data or with the second user's data.
That's the point
I'm stuck on how to do that
Get first user's data.
Decide if it's possible to vend bottle, with correct change. If it's not, say "NO", all done.
If it is, update the amount of change the machine has (because the user gave it a coin, and maybe got given a coin back).
Get next user data, do that again.
Get next user data, do that again.
Get next user data, do that again.
Get next user data, do that again.
Get next user data, do that again...

Until there is no more user data. If there is no more, say "YES".

Do you understand why the answer to this set of data:
1
2
3
4
5
6
Example
3
100 1
50 1
50 1
Output:No

is No?
Last edited on
I do understand but to put it in code is hard for me
Indent your code to match the actual structure. Here is what you have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int
main()
{
    int money, no_bottles, remainder, cost, n;
    cost = 50;

    cout << "Enter number of items:";
    cin >> n;
    for (int i = 0; i < n; i++) {
        cout << "Enter money and number of bottles\n";
        cin >> money >> no_bottles;
    }
    remainder = money - (cost * no_bottles);
    if (money < remainder)
        cout << "No";
    else
        cout << "Yes";
}

Seen this way, it should be clear what Repeater is saying: your for loop reads all the input. Then, after all of the input has been read, it executes lines 15-19 once.

You may be confusing variables in C++ with cell values and formulas in a spreadsheet like Excel. In Excel, you give a cell a formula and then any time you update something that the formula depends upon, the cell with the formula updates automatically. C++ is very different. Once you've assigned a value to a variable, the value remains unchanged until you explicitly do something to change it:
1
2
3
int a=1, b=2;
int x = a + b;   // x = 3
a = 7;          // x is still 3 


This is all a long winded way of saying that line 15-19 need to be inside the loop. So move line 14 to line 20.

That will help, but the code still won't be wrong. Fundamentally, the problem is that you have to store (and use) the amount of money in the vending machine.
Thanks for the help I now understand what to do
Someone help me for a loop to go through all the inputs
Like to go through the first input then look if there is change required else if no to go through the other input and other and other untill there is no more
I'm learning c++ from a background of python I tried it in python can someone help me translate to c++ with comments if possible
Thanks
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

def main():
    try:
        numberOfStudents = int(input().strip())
    except EOFError:
        return

    currentAmountInMachine = 0
    myList = []
    
    for student in range(0,numberOfStudents):
        students = input().strip()
        studentPossesions = students.split(" ")

        money  =  int(studentPossesions[0])
        numberOfBottles = int(studentPossesions[1])
       
        changeExpected = ((money/50) - numberOfBottles)*50
        

        if(changeExpected == 0):
            myList.append("YES")
            currentAmountInMachine += money
            

        elif ((currentAmountInMachine - changeExpected) >= 0):
            myList.append("YES")
            currentAmountInMachine +=  money
            currentAmountInMachine -= changeExpected
            
        else:
            myList.append("NO")
          

    if ("NO" in myList):
        print("NO")
    else:
        print("YES")

    

main()
Help me please
Pages: 1234