Vending machine change function Write a function that takes an amount of money M, a price P and returns the change given in the smallest number of coins. The following coins are available: 1c, 5c, 10c, 25c, 50c, and $1. Time: 20min.

I'm looking for all sorts of reviews considering the time limit and environment.

Here's my code:

#include &lt;iostream&gt;
#include &lt;vector&gt;

std::vector&lt;int&gt; getChange(double M, double P)
{
  /*
    1c, 5c, 10c, 25c, 50c, and $1
    getChange(3.14, 1.99) should return [0,1,1,0,0,1]
    getChange(4, 3.14) should return [1,0,1,1,1,0]
    getChange(0.45, 0.34) should return [1,0,1,0,0,0]
  */

  int denominations[] = {1, 5, 10, 25, 50, 100};
  std::vector&lt;int&gt; r(sizeof denominations / sizeof denominations[0]);

  // I had a floating-point problem here by computing cents = (M-P)*100;
  // it took me a few minutes to figure it out
  int cents = (int)(M*100) - (int)(P*100);

  for(int i = sizeof denominations / sizeof denominations[0] - 1; i &gt;= 0; --i){
    r[i] = cents / denominations[i];
    cents = cents % denominations[i];
  }

  return r;
}

int main()
{
  auto change = getChange(5, 0.99);
  for(auto i : change){
      std::cout &lt;&lt; i &lt;&lt; ", ";
  }
  std::cout &lt;&lt; std::endl;
  return 0;
}
You still have a floating point problem
If you change your line to this input:
auto change = getChange(0.04, 0.03);

you will get the following output:
2, 0, 0, 0, 0, 0,

As you can see, it answers with 2 pennies instead of one.  The problem is with the line that you &quot;fixed&quot;:

// I had a floating-point problem here by computing cents = (M-P)*100;
// it took me a few minutes to figure it out
int cents = (int)(M*100) - (int)(P*100);


You did notice a problem with your floating point conversion but you came to the conclusion that it was the subtraction that was the problem.  The actual problem is that a floating point value of something like 0.03 can sometimes be slightly above or slightly below the actual value of 0.03.  In this case, it is slightly below.  When you convert this value to cents by using (int)(P*100), you end up converting something like 2.999999 to 2 instead of 3.
One way to avoid this problem is to round instead of truncate.  So you can change your line to this to fix the problem:
int cents = (int) (100*(M-P) + 0.5);