views:

185

answers:

4

Hey there, this is part of a question i got in class, im at the final stretch but this has become a major problem. In it im given a certain value which is called the "gold value" and it is 40.5, this value changes in input.

and i have these constants

const int RUBIES_PER_DIAMOND = 5;   // relative values. *
const int EMERALDS_PER_RUBY = 2;
const int GOLDS_PER_EMERALDS = 5;
const int SILVERS_PER_GOLD = 4;
const int COPPERS_PER_SILVER = 5;
const int DIAMOND_VALUE = 50;       // gold values. *
const int RUBY_VALUE = 10;
const int EMERALD_VALUE = 5;
const float SILVER_VALUE = 0.25;    
const float COPPER_VALUE = 0.05;

which means that basically for every diamond there are 5 rubies, and for every ruby there are 2 emeralds. So on and so forth.

and the "gold value" for every diamond for example is 50 (diamond value = 50) this is how much one diamond is worth in golds.

my problem is converting 40.5 into these diamonds and ruby values. I know the answer is 4rubies and 2silvers but how do i write the algorithm for this so that it gives the best estimate for every goldvalue that comes along??

please help!, im at my wits end

+4  A: 

This sounds like a Greedy algorithm to me. It's basically just going to be a set of while loops, and you decrease the amount you have left every time you're able to "buy" an item.

If you post your code we can help you further, but considering you mentioned this is homework, I'd like to see your attempts first.

Good luck!

overslacked
+1  A: 

Here's a solution, just fill in the gaps for all of your currencies and stuff.

const unsigned int VALUE_SCALE = 100;

struct convertedValue  
{  
   unsigned int diamond;  
   unsigned int ruby;  
   unsigned int emerald;  
   unsigned int silver;  
   unsigned int copper;  
};

int main()  
{  
   unsigned int remainingValue;  
   convertedValue currency;  

   // Get input to fill remainingValue, or sub in your own for the question...  
   // ...  

   // Multiply everything by VALUE_SCALE to get rid of floating-point error  
   remainingValue *= VALUE_SCALE;
   while (remainingValue != 0)
   {  
      if (remainingValue >= DIAMOND_VALUE * VALUE_SCALE)  
      {  
         currency.diamond++;  
      }
      else if (remainingValue >= RUBY_VALUE * VALUE_SCALE)  
      {  
         currency.ruby++;  
      }  
      // etc for each case  
      // ...  
   }  

   // then loop through each type and print them out or whatever  
   // ...  

   return 0;  
} 
SalamiArmi
You could multiply the input value with 100 (and the 'gem'-values), to get rid of the float problem. Effectively working in gold cents. (that would be copper coins I guess ;-) )
NomeN
There we go, thats a bit easier to read now.
SalamiArmi
A: 

My advice would be to start by setting the item with the smallest value as having a value of 1. Then convert all the other items to integer multiples of that one. Once you've done that, it'll be pretty easy to arrange your items in order by decreasing value.

When something comes it that you want to convert, start by converting it to an integer multiple of that base (no pun intended) value as well. Then you do your conversion about the way others have already advised -- subtract off as many of the highest value item as possible, then the second highest value item, and so on down the line.

Jerry Coffin
I'm not a native english speaker, what's the pun that wasn't intended?Or is it a case of http://xkcd.com/559/?
Ledhund
In this case there is a pun: at one time, they separated metals into two general classes: noble and base (and many alchemists spent their lives trying to convert some base metal into a noble metal, e.g., lead into gold). In this case all the monetary measurements would be *based on* a "base metal"...
Jerry Coffin
A: 

Take the item. For exemple, n Check if n is lower than your biggest currency and check how many times you can fit the currency in if it is. Then get your second biggest currency and check this again with what's left above. Do this until n=0. You then just have to display each currency and the amount of it you've found.

Plebraly