C/C++ Assigning numOnes with Modulo Operator

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Operator
Click For Summary
The discussion focuses on a coding problem where a cashier distributes change using the maximum number of five dollar bills followed by one dollar bills. The key point is to correctly assign the number of one dollar bills to the variable numOnes using the modulus operator. The correct code should use the variable amountToChange rather than a fixed value like 19. The solution is to write numOnes = amountToChange % 5, ensuring it works for any value of amountToChange. This highlights the importance of applying operations to the correct variable to achieve accurate results in programming.
ineedhelpnow
Messages
649
Reaction score
0
A cashier distributes change using the maximum number of five dollar bills, followed by one dollar bills. For example, 19 yields 3 fives and 4 ones. Write a single statement that assigns the number of distributed 1 dollar bills to variable numOnes, given amountToChange. Hint: Use %.

Sample program:

#include <iostream>
using namespace std;

int main() {
int amountToChange = 0;
int numFives = 0;
int numOnes = 0;

amountToChange = 19;
numFives = amountToChange / 5;
<STUDENT CODE>

return 0;
}


i only have to write code for the part that says <student code>. i tried numOnes = 19 % 5;

its wrong though. it passed two tests but failed one.
 
Last edited:
Technology news on Phys.org
oh i got it. it was supposed to be amountToChange % 5 not 19 % 5 because then it only works for one value
 
To which variable are you applying the modulus operator?
 
ineedhelpnow said:
oh i got it. it was supposed to be amountToChange % 5 not 19 % 5 because then it only works for one value

Good! (Yes)

That was my suspicion, that you were applying "% 5" to the wrong value. :D
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
1
Views
6K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K
Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 25 ·
Replies
25
Views
2K