Algorithm

Pages: 1234
Hello Dee5,

totalCoins = (count50 * 50) + (count100 * 100) + (count200 * 200);

Andy
Total coins of eache type Andy
Like the total count of 50
Total count of 100
Total count of 200
The total count of each type of coins
1
2
3
4
5
6
7
8
    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;

Put your initial counts OUTSIDE the for loop.
You're just starting from zero each time.

And just learn to put debug cout statements through your code if you're in any way confused as to what's going on.
Say
1
2
3
4
5
6
7
8
9
10
11
12
13
    if (studentmoney==50)
    {
      count50++;
    }
    if (studentmoney==100)
    {
      count100++;
    }
    if (studentmoney==200)
    {
      count200++;
    }
    cout << "DEBUG: Coin totals " << count50 << "," << count100 << "," << count200 << endl;

The 2nd time around, it ought to be obvious that it isn't what you expected.



For someone with a Python background, your indentation sucks.

So I managed to count the each type of coins in the studentmoney
But also I want to count the each type of coins in the machinemoney
Please help this has been so hard for me

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
61
62
63
64
65
66
include<iostream>
using namespace std;

int main()
{
    int no_students;
    cin >> no_students;
    int machinemoney = 0;
    bool okay = true;  
    
    int count50=0;
    int count100=0;
    int count200=0;
   
    for (int i = 0; i < no_students; i++) 
    {
  int studentmoney, no_bottles, Change;
  cin >> studentmoney >> no_bottles;
         
    
    
    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;
        } 
    } 
    cout << " Coin totals " << count50 << "," << count100 << "," << count200 << endl;
 
     if(!okay)
     {
       cout << "NO";
     }
     
    else 
    {
      cout <<"YES";
    }
  }   
  
Also comment in my code as it make me understand better
I learn more through other codes
if someone just please help to just finish the code for me
I managed to count the each type of coins in the studentmoney
No, you're counting the number of each coin in the machine. Lines 22 -33 basically take the student's coin (studentMoney) and place it into the appropriate bin the machine.

I suggest that you add comments to the variables stating exactly what they mean. For example:
1
2
3
4
5
6
7
8
    int no_students;    // The number of transactions
    cin >> no_students;
    int machinemoney = 0;  // Total amount of money in the machine
    bool okay = true;       // Can the machine provide change so far?
    
    int count50=0;         // Number of 50-cent coins in the machine
    int count100=0;      // Number of 100-cent coins in the machine
    int count200=0;      // Number of 200-cent coins in the machine 


Now you need to add the logic to see if you can actually make change. I'd put that in a function if you've learned about them.
Hey dhayden in line 4 yes the machine can provide Change and about function I didn't really learn about it
And yes i know that I'm counting each type of coins in the machine
But when change is given I need to update the count to substract the change given
Example
When user inputs
50 1
50 1
50 1
50 1
200 1
100 1
100 1
Here the machine will have four 50coins, one 200 coins and one 100coins

The change given to the fifth customer is 150 which is (three 50 coins)
Which makes the machine to have (one 50 coin, one 200coin) in it.
Then the sixth customer needs change of 50 which will make the count of 50 coins in machine be zero.

Now here is the part which I'm struggling to count the remain coins after change is given.

Then the last customer needs change of 50 of which the machine has only (one 100 coin and one 200coin)
Which it will be impossible to give change
Thus it should print No
Last edited on
Why
Off hand, I would try something like
1
2
3
4
5
6
7
while ( change > 0 ) {
  if ( change >= 200 && count200 > 0 ) {
    change -= 200;
    count200--;
  }
  // ditto other denominations
}
Where should I put the code of while because if I put it inside the loop
It gives wrong results
When I put outside it says Change is not declared

I want to put this if statements someone help me
1
2
3
4
5
6
7
8
9
10
11
12
If (Change==50 && count50<=0)
     { 
        Cout<<"No"
      }
If (Change==100&& count100<=0)
     { 
        Cout<<"No"
      }
If (Change==200 && count200<=0)
     { 
        Cout<<"No"
      }


Below is my code
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include<iostream>
using namespace std;

int main()
{
    int no_students;
    cin >> no_students;
    int machinemoney = 0;
    bool okay = true;  
    int count50=0;
    int count100=0;
    int count200=0;    
    for (int i = 0; i < no_students; i++) 
    {
  int studentmoney, no_bottles, Change;
  cin >> studentmoney >> no_bottles;
             
    if (studentmoney==50)
    {
      count50++;
    }
    if (studentmoney==100)
    {
      count100++;
    }
    if (studentmoney==200)
    {
      count200++;
    }

Change = studentmoney - no_bottles*50;

    cout <<Change;
  if (Change == 0)
  {
     machinemoney = machinemoney + studentmoney;
  }
      
  else if(machinemoney- Change>=0)
   {
     machinemoney = machinemoney + studentmoney;
      machinemoney = machinemoney - Change;
  }
  
  
   else {
      okay = false;
        }  
         
   while ( Change > 0 )
{
  if ( Change >= 50 && count50 > 0 ) 
  {
    Change -= 50;
    count50--;
  }
  
  if ( Change >= 100 && count100 > 0 ) 
  {
    Change -= 100;
    count100--;
  }
  
  if ( Change >= 200 && count200 > 0 ) 
  {
    Change -= 200;
    count200--;
  }
 }
}
      
if((!okay)
     {
       cout << "NO";
     }
     
    else 
    {
      cout <<"YES";
    }   
}
 




Last edited on
> if((!okay)
This won't even compile.

Your indentation still looks like something the cat dragged in.
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
61
#include<iostream>
using namespace std;

int main()
{
  int no_students;
  cin >> no_students;
  int machinemoney = 0;
  bool okay = true;
  int count50 = 0;
  int count100 = 0;
  int count200 = 0;
  for (int i = 0; i < no_students; i++) {
    int studentmoney, no_bottles, Change;
    cin >> studentmoney >> no_bottles;

    if (studentmoney == 50) {
      count50++;
    }
    if (studentmoney == 100) {
      count100++;
    }
    if (studentmoney == 200) {
      count200++;
    }

    Change = studentmoney - no_bottles * 50;
    cout << Change;
    if (Change == 0) {
      machinemoney = machinemoney + studentmoney;
    }
    else if (machinemoney - Change >= 0) {
      machinemoney = machinemoney + studentmoney;
      machinemoney = machinemoney - Change;
    }
    else {
      okay = false;
    }

    while (Change > 0) {
      if (Change >= 50 && count50 > 0) {
        Change -= 50;
        count50--;
      }
      else if (Change >= 100 && count100 > 0) {
        Change -= 100;
        count100--;
      }
      else if (Change >= 200 && count200 > 0) {
        Change -= 200;
        count200--;
      }
    }
  }

  if (!okay) {
      cout << "NO";
  } else {
      cout << "YES";
  }
}


3. You should compare the counts largest to smallest, not smallest to largest.
When giving change, you always give the largest suitable coin available.

4. You need an if / else if / else chain so that you only give out ONE coin per iteration of the loop.

5. The while (Change > 0) should be inside the else if (machinemoney - Change >= 0) part of the code.
BUT!!!!
You need to make sure you can give ALL the change before you decide to give any of the change.
Simply having the numerical amount of money in the machine doesn't immediately mean you're able to give the required change.




The first part when u say this if(!okay) won't even compile
What should I do to make it compile
I didn't get the first and fourth instructions
Bro Salem just please if you have time just finish up the code for me
'Bro' Salem ain't going to do diddly squat for you until you show some nouse and show that you're actually learning something.

Reposting other peoples code with broken edits (and nagging) won't get you very far here, and it won't get you far anywhere else either.

My sense is you've been coasting through the course for far too long on the back of other peoples work and "googled" answers. So much so, you're now completely hopeless at actually achieving anything new for yourself.

In short, you're wasting everyone's time - including your own.
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
61
62

#include<iostream>
using namespace std;

int main()
{
  int no_students;
  cin >> no_students;
  int machinemoney = 0;
  bool okay = true;
  int count50 = 0;
  int count100 = 0;
  int count200 = 0;
  for (int i = 0; i < no_students; i++) {
    int studentmoney, no_bottles, Change;
    cin >> studentmoney >> no_bottles;

    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;
      
      while (Change > 0) {
      if (Change >= 200 && count200 > 0) {
        Change -= 200;
        count200--;
      }
      else if (Change >= 100 && count100 > 0) {
        Change -= 100;
        count100--;
      }
      else if (Change >= 50 && count50 > 0) {
        Change -= 50;
        count50--;
      }
    }
 }
    else {
      okay = false;
    }
  }

  if (!okay) {
      cout << "NO";
  } else {
      cout << "YES";
  }
}






Okay I did what you told me to do
But the place when u said that I need an if else chain so that I can give out one coin per literation I didn't understand
I spent days solving this and I've finally able to count the each type of coins inside the machine after change is given.

But I want to remove this from the code
1
2
3
4
5
 if(!okay){
cout<<"NO";
}else{
cout<<"YES";
}


And replace with this

1
2
3
4
5
 If (Change<currentAmountInmachine &&count50Change<=count50machine)
       count<<"YES";
 }else{
       cout<<"NO";
}


Any ways of removing someone help me please
Here is my code
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74


#include<iostream>
using namespace std;

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

    int currentAmountInMachine = 0;
    bool okay = true;
    
    int count50Change=0;
    int count100Change=0;
    int count200Change=0;
    int count50=0;
    int count100=0;
    int count200=0;
    
    for (int i = 0; i < numberOfStudents; i++) {

  int money, numberOfBottles, Change;
  cin >> money >> numberOfBottles;
    if (money == 50) {
      count50++;
    }
    if (money == 100) {
      count100++;
    }
    if (money == 200) {
      count200++;
    }
  
  Change= money - numberOfBottles*50;
  
          while (Change > 0){
      if (Change >= 200 && count200 >= 0) {
        Change -= 200;
        count200Change++;
      }
      else if (Change >= 100 && count100 >= 0 ) {
        Change -= 100;
        count100Change++;
      }
      else if (Change >= 50 && count50>= 0) {
        Change -= 50;
        count50Change++;
      }
    }
    
     int count50machine=(count50-count50Change);
     int count100machine=(count100-count100Change);
     int count200machine=(count200-count200Change);
     
  if (Change== 0) {
      currentAmountInMachine += money;

  }else if (currentAmountInMachine - Change >= 0) {
      currentAmountInMachine += money;
      currentAmountInMachine -= Change;
   
  } else{
      okay = false;
  }
    }

    if (!okay) {
  cout << "NO\n";
    } else {
  cout << "YES\n";
    }
}



Last edited on
Pages: 1234