Change Breakdown computation. Can't make the formula.

Okay, our programming teacher gave an exercise for us, the output should be like this https://www.dropbox.com/s/t70zui2xvnyj7sb/CS126%20Major%20Exercise%203%20%28October%2031%29.pdf

I have already found out how to find the number of thousands and five hundreds, but I can't formulate the number of two hundreds, and cannot continue to the other parts.

here is my work so far

//Change Breakdown
int wholePart;
double centsPart;
int thousands, fiveHundreds, twoHundreds, oneHundreds, fifties, twenties, tens, fives, ones;
int dotTwentyFives, dotTens, dotZeroFives;

//Computations
wholePart = (int) change;
centsPart = change - wholePart;
thousands = wholePart / 1000;
tHundreds = wholePart /
fiveHundreds = (wholePart - (thousands * 1000)) / 500;

cout<<"\n\n========================Change Breakdown=======================\n\n";

cout<<"1000 "<<thousands<<" "<<thousands*1000.00<<"\n";
cout<<"500 "<<fiveHundreds<<" "<<fiveHundreds*500.00<<"\n";
Last edited on
Hint 1: you can store the result of (wholePart - (thousands * 1000)) and then use that instead of wholePart.

Hint 2: compound assignment operators.
Hint 3: modulo operator (%).
Hint 1 ive already tried that and it didnt work. What's compound assignment operarors and modulo operator?
Last edited on
They are syntactic sugar.

Show your hint 1 attempt. (And code tags, please.)
twoHundreds = (fiveHundreds - (fiveHundreds * 500))/200

this is what I did
store the result of (wholePart - (thousands * 1000)) and then use that instead of wholePart.
tried that and it didnt work:
twoHundreds = (fiveHundreds - (fiveHundreds * 500))/200

Are you sure you did the right thing?

I said "store result", so instead of
fiveHundreds = (wholePart - (thousands * 1000)) / 500;
think about
1
2
3
int remainder = wholePart - (thousands * 1000);
fiveHundreds = remainder / 500; // Determine the count of size 500 currency
remainder = remainder - (fiveHundreds * 500); // take off the size 500 currency's value 


Have you had functions yet? Something like:
int extract( int & remainder, const int Size );

Edit Functions are boring and so are loops, but with syntactic sugar they go down much easier than copy-paste programming. The code below has not been tested, but most definitely would cause a reaction in regular professor.
1
2
3
4
5
6
7
std::transform( sizes.begin(), sizes.end(), counts.begin(),
  [&wholePart](const int size)
    {
      const int count{ wholePart / size };
      wholePart %= size;
      return count;
    });
Last edited on
Topic archived. No new replies allowed.