Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Coins & C++ (http://www.programmingforums.org/showthread.php?t=1011)

gamehack Nov 1st, 2004 3:29 PM

Hello all,

I need to do a program for school but I'm completely lost :( I cannot think of a algorithm... the question is how many combinations there are if I have unlimited supply of 50 pence, 1 J and 2 J coins, how many different ways is it possible to make a sum of X amount?

Thanks all

Mjordan2nd Nov 1st, 2004 3:55 PM

Hava a variable, we'll call counter, and initialize at 0. have a few nested loops which calculate every possible combination of coins and every time X amount is reached, increment counter.

Benoit Nov 1st, 2004 4:22 PM

I don't understand the problem :huh:

kurifu Nov 1st, 2004 8:09 PM

Actually I do not really understand the currency you are using, could you explain what !j and 2J is in relation to 50pence? Or use good old canadian/american currency systems :P

Benoit Nov 1st, 2004 11:34 PM

The good ol' dollar :)

gamehack Nov 2nd, 2004 7:04 AM

Actually J = Pound Sterling :P

PS

During my Physics lesson today, I tought of the algorithm for 5 minutes and wrote this source code:
:

#include <iostream>
using namespace std;

int main() {

float fiftyp, onepound, twopounds, amount;
int counter;
// initialization
counter = 0;
amount = 100.0;
fiftyp = 0;
onepound = 0;
twopounds = 0;

for(fiftyp = 0; fiftyp <= 200; fiftyp++) {
  for(onepound = 0; onepound <= 100; onepound++) {
    for(twopounds = 0; twopounds <= 50; twopounds++) {
  if(((0.5*fiftyp)+(1*onepound)+(2*twopounds)) == amount) { counter++; }
        }
 }
        }


return 0;
}


Can someone test it and review to see if it's alright, because I don't have a compiler at school...

Thanks very much

Ooble Nov 2nd, 2004 11:43 AM

Looks perfect. Nice to see you do more work in Physics than I do... even if it's irrelevant :P


All times are GMT -5. The time now is 8:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC