Assigning numOnes with Modulo Operator

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Operator
Click For Summary

Discussion Overview

The discussion revolves around a programming problem involving the use of the modulo operator to calculate the number of one dollar bills a cashier distributes as change, given a specific amount. The context is primarily focused on coding and problem-solving within a programming assignment.

Discussion Character

  • Homework-related

Main Points Raised

  • One participant initially suggests using "numOnes = 19 % 5" to calculate the number of one dollar bills, which is identified as incorrect.
  • Another participant points out that the correct expression should be "amountToChange % 5" instead of "19 % 5," emphasizing the need for a variable that can change rather than a fixed value.
  • A later reply confirms the suspicion that the modulus operator was applied to the wrong value, reinforcing the correction made by the second participant.

Areas of Agreement / Disagreement

Participants generally agree on the correct application of the modulus operator, with a clear consensus on using "amountToChange" instead of a fixed number.

Contextual Notes

The discussion does not address potential edge cases or limitations in the problem, such as what happens with negative amounts or non-integer values.

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
 

Similar threads

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