### Calculating max combinations you can buy given a certain amount of money

I'm trying to write a program that will give me the maximum amount of items i can buy given a certain amount of money. So far my code only accounts for the max possible, so if I were to input \$100, it would only say I can afford the item that cost \$8.99, rather than telling me I can afford multiple items that would add up to \$100

 ``123456789101112131415161718192021222324252627282930313233343536`` `````` 1 #include 2 #include 3 using namespace std; 4 5 int main() 6 { 7 double number, cost, optimal, ten, twenty, forty; 8 cout << "How much money do you have?" << endl; 9 cin >> number; 10 cost = number - (number*.08875); 11 ten = 10; 12 twenty = 20; 13 forty = 40; 14 if ( cost < 4.49) 15 { 16 cout << "You poor as shit my dude u cant afford nothin" << endl; 17 } 18 if (cost >= 4.49 && cost < 5) 19 { 20 cout << "You can afford a "<< ten <<" piece" << endl; 21 cout << "This serves 2.5 people" << endl; 22 } 23 if (cost >= 5 && cost < 8.99) 24 { 25 cout << "You can afford a " << twenty <<" piece" << endl; 26 cout << "This serves 5 people" << endl; 27 } 28 if (cost >= 8.99) 29 { 30 cout << "You can afford a " << forty << " piece" << endl; 31 cout << "This serves 10 people" << endl; 32 } 33 optimal = cost 34 35 return 0; 36 }``````

I kinda stopped at line 33 because I hit this roadblock and can't think of a solution. Is there any way to make what I'm thinking of happen?
You are trying to solve a knapsack problem. It's a well-known piece of computer science.

As for your specific problem; you need a loop. You need to keep trying to buy things, until all the money is gone or you can't buy anything else.

 ``123456`` ``````while ( money_left > 0 ) { // try to buy another thing // if can't buy another thing, break out of the loop )``````
Last edited on
so far i have
 ``123456`` `````` 35 while (costi > 0) 36 { 37 costa = number - costi 38 goto keyword; 39 }``````

is there anyway to have the process from the initial post repeat but with a different variable?
try not to use goto.

yes, you can repeat code with other variables. Maybe put it into a function with a reference parameter? You can also put all the 'variables' into a vector and iterate the vector as the 'variables'. Both designs work, sometimes one is cleaner than the other depending on what it is you are doing.

The problem description is missing something — the knapsack problem is a cost-benefit problem. The OP only asks for “maximum [number] of items i can buy given a certain amount of money”.

That is easy. Purchase as many of the lowest-cost items you can.
Purchasing as much of the lowest cost item wouldn’t be the most cost effective method I think. If I input \$20, it would output that I could afford 4 10 pieces, although for 50¢ more I could afford a 20 piece, which would instead yield 40 more nuggets.
its the same idea, you just need to adjust pieces and costs to a common math term that can be compared, then buy all you can of the highest yield item, then see if you have enough left to buy anything more on top of it. that is the key to the algorithm here, if I understood the question (how to get the most # of things for some input).
how do you do the vector thing
Last edited on
Nevermind I just calculated the most cost effective nuggets to buy by seeing how may cents they were charging per nugget in each meal so now i only have one nugget cariable and this should be a whole lot easier
Exactly what I was trying to say about getting them to a common term.

vector<int> variable(size); //this works mostly the same as arrays, but you can do more things with them. If you haven't gotten to arrays yet, hold the thought until you do.
Topic archived. No new replies allowed.