Assigning numOnes with Modulo Operator

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

The discussion focuses on the correct implementation of the modulo operator in C++ to calculate the number of one dollar bills when distributing change. The correct statement to assign the number of one dollar bills to the variable numOnes is numOnes = amountToChange % 5;. This ensures that the calculation is dynamic and works for any given value of amountToChange, rather than being hardcoded to a single value like 19.

PREREQUISITES
  • Understanding of C++ syntax and variable assignment
  • Knowledge of arithmetic operations in programming
  • Familiarity with the concept of the modulo operator
  • Basic understanding of conditional logic in programming
NEXT STEPS
  • Explore C++ variable types and their usage
  • Learn about arithmetic operators in C++, focusing on the modulo operator
  • Study control structures in C++ to enhance logic implementation
  • Practice writing functions in C++ to encapsulate change distribution logic
USEFUL FOR

Beginner C++ programmers, students learning about arithmetic operations, and anyone interested in optimizing code for change distribution algorithms.

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