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
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).
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.