cout << "In total, there are " << counter << " possible combinations.\n";
system("pause");
}
--------------------------------------------------------------------------------
So, I made this, but when I run the program, it really doesn't display the number of ways. Any idea of what I'am missing?
The line "amount *= 100;" implies that amount is the number of cents.
Dividing the number of cents by, say, 1000, yields a value specified in 100s of dollars, which
you are then comparing against D10 * 1000 (assuming you fix the typo) which is cents. The
two units don't match, so the comparison must be wrong.
So after fixing that, it still seems that your program takes an eternity to run. This is because of
the sheer number of multiplications and additions you are doing. Particularly, you have 8 multiplies
and 8 additions in the terminating condition of your inner-most for loop (assuming the multiply by 1
is optimized out). This loop is going to run a gazillion times, so I'm sure your program will take a long
time to generate the answer.
You need to think of a different approach to solving the problem.
I wrote your program using recursion and it completes in 4.3 seconds with no optimization (using gcc)
and 1.1 seconds with -O3.
(The number of ways of making change for $7.37 is 680,592 by the way.)
EDIT: I got it down to just under 0.5 seconds. (btw, this is using $7.37 as the input value).
EDIT 2: LOL. I got it down to less than .01 seconds now. (average = 0.007s)
I started working with C++ recently, so this is what I came up with. Our teacher had showed us his program and his worked fast, which is why I loss at how to approach this. It may take long but I' am just glad it does what its suppose to do (or come close to), even though its super slow.
Having said that, I appreciate you help! I'll fix it as soon as I can.
I heard of recursion, but I'am barely covering pointers atm (<---sad panda). I don't think we will cover it this semester, but I'll keep working with coding to improve.