need help with for loop problem

Hi

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!
Last edited on
I understand what you mean, thanks for the reply. although, what happens if it doesn't add up to N? what would I do then?
would you not be better to initialise them with the number of coins they're each worth
1
2
3
int Pot = 7;
int Wan = 2;
double Char = 0.5;

Then in the for loops use a variable like numberOfPots etc. and on the inner most for loop just have an if statement something like this:

1
2
3
if( (Pot*numberOfPots)+(Wan*numberOfWan)+(Char*numberOfChar)==Coins){
        cout<<"You can buy "<<numberOfPots<<" Pots, "<<numberOfWan<<" Wan, "<<numberOfChar<<" Char. \n";
}


I only sort of understand what you are trying to do so sorry if this is a bit off.
Also I agree with shacktar about not using Char.
Last edited on
yeah im getting close, ^ with that method it prints a long list of numbers etc. some, or most not being the solution.

if N = 100

There should be 3 solutions, with one being

1 pot, 29 wan and 70 char

( 1 + 29 + 70 = 100)

(1x7 + 29x2 + 70x0.5 = 100 gold coins )
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.

This is what I think you mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
	#include <iostream>
	
	using namespace std;

int main(int argc, char* argv[])	
{
	int Coins;

	int Pot = 7;
	int Wan = 2;
	double Char = 0.5;
	int numberOfPots = 0;
	int numberOfWan = 0;
	int numberOfChar = 0;
	
	cin >> Coins;

	int Items = Coins;

	for (numberOfPots = 0; numberOfPots < Items; numberOfPots++) {
		for(numberOfWan = 0; numberOfWan < Items; numberOfWan++) {
			for(numberOfChar = 0; numberOfChar < Items; numberOfChar++) {
				if( (Pot*numberOfPots)+(Wan*numberOfWan)+(Char*numberOfChar)==Coins && 
					(numberOfPots+numberOfWan+numberOfChar)<=Items){
					cout<<"You can buy "<<numberOfPots<<" Pots, "<<numberOfWan<<" Wan, "<<numberOfChar<<" Char. \n";
				}
			}
		}
	}
return 0;
}
	
Topic archived. No new replies allowed.