C/C++ Assigning numOnes with Modulo Operator

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Operator
AI Thread 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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top