Im trying to use a 'for loop' in order to create a program where the user inputs 'N' amount of gold coins e.g: 50 and that also becomes the amount of items in total they are allowed to purchase. e.g: the 3 items 'Pot, Wan and Char' have to equal up to 'N' items and 'N' gold coins.
pots worth 7 gold coins each
wan worth 2
and char worth 0.5
int Coins;
int Pot;
int Wan;
double Char;
cin >> Coins;
int Items = Coins;
for (Pot = 0; Pot < Items; Pot++) {
for(Wan = 0; Wan < Items; Wan++) {
for(Char = 0; Char < Items; Char++) {
}
}
}
Pot = Pot / 7;
Wan = Wan / 2;
Char = Char / 0.5;
how can i create a brute force approach that checks all possible cases against the two constraints in this problem.
the output would show how many items of each sort the user can purcahse if they must purchase exactly N items with N gold coins.
The nested for loop is the brute force approach. Within the deepest loop in the nesting, you just need to check the two constraints: (1) does Pot, Wan, and Char add up to N? and (2) does this combination also cost N coins? (i.e. does 7 x Pot + 2 x Wan + 0.5 x Char = N?) If so, I guess you can print the values of Pot, Wan, and Char. Also, Char should be an int as well, and I would name it something different than Char because it can be easily confused with the built-in type char. Good luck with your homework!
Ah sorry forgot about the condition that there is a limit to how many items there can be. Is it not just a matter of putting in an AND is total must be less than items in the if statement.