1. ## 24 game problem

Okay, this isn't really a game.

I had a math problem, and i wanted to make a program where you enter 4 numbers, and the program finds what combinations can be used without exponents or grouping signs and would equal 24.

I have the adding/subtractions part working, just a few small problems with grouping, but i dont want to use if else if the whole time. I addded the mulitplication and division, just to have it break out of the loop.

Is there an algorithim i could use for this, or a certain command? Thanks.

2. I'm not sure if I quite follow what you want it to do, but couldn't you use recursion?

Do you mean the user enters in 4 numbers like "12 1 9 5" or like "5613"? And if you mean the second one, then would your program count "5+6+13"?

3. if you enter 1 2 3 4, i want the program to make a random equation, test it, and displayt the equation if it equals 24, else, repeat. After it fails a bunch of times, say "cannot get ot 24"

4. so the user can only input four 1 digit numbers?

5. Then I would say just use recursion, that way it is easily changed to any amount of numbers you want to test. Something like this:

Code:
```void recursion(vector<int> nums, int spot, int max, int currentTotal, int target)
{
currentTotal+=nums[spot];
if spot==max
else recursion one farther
currentTotal-=nums[spot];

currentTotal-=nums[spot];
etc etc
}```
// edit although now that I think about it, for multiplication and division you'll only do that operation with the previous number, not the whole current total. I'd suggest doing similar to above, but instead adding the operator and the number to a string instead of add/subtract/etc with an int. Then when you hit the max recursion depth, you have a function parse the string to figure out what the total is.

6. Originally posted by neandrake
so the user can only input four 1 digit numbers?
No, they can be big multi-digit numbers. but im doing a for loop on an int array of four digits, so it runs like this:

-----------------
Enter 4 numbers: 12 34 15 18
You entered:
1: 12
2: 34
3: 15
4: 18
------------------

As for recursion, vectors dont mean anything to me, i use them to find out how many units are in the magnitude of a right triangle.

All i was really wanting was to put 12 34 15 18 into a random equation, solve it, if it equals twenty four, then output that equation.

I can do it, but i'd have to use a lot of if else if statements.

7. Vectors are nothing more than smarter arrays really. Just substitute in an array and it would work the same. The benefit of using recursion this way is that it will test all possibilities, not just some.

8. Recursion... tests all? okay. but how does that work on making an equation, solving it, and outputting the equation?

On short, throw me some code that does that

9. Okay, I'll show you a simple example which you'd have to update to accomodate the bigger numbers for your program. Say you have a function called
Code:
`solve(string s)`
which takes a string like "1+3*4-9" and is able to parse and figure out what that is equal to, and if you want maybe print out the equation if it equals a certain number. Then your recursion would look something like this (i haven't tested it though!):
Code:
```int main()
{
....
char nums[4]={'1','3','4','9'};
string s="";

recursion(nums, 0, 3, s);
...
}

void recursion(char nums[], int spot, int max, string s)
{
s+=nums[spot];
if (spot==max)
{
solve(s);
return;
}

s+='+';
recursion(nums, spot+1, max, s);
s[s.length()-1]='-';
recursion(nums, spot+1, max, s);
s[s.length()-1]='*';
recursion(nums, spot+1, max, s);
s[s.length()-1]='/';
recursion(nums, spot+1, max, s);
}```
There are ways to optimize this better, just a quick idea. Its basically equivalent to four nested for loops running through all of the operator possibilities in between each pair of numbers and solving it in the innermost for loop. The reason I chose recursion is that it can easily be done to say ten numbers instead of only four without having to change anything.

10. Here's my ideas on how to solve it

Code:
```In pseudo-code, it looks something like this

// prototype
bool solve ( int target, int values[], int numvals );

// The initial call would be
int nums[] = { 1, 2, 3, 4 };
bool result = solve( 24, nums, 4 );

The basic idea is that if you have
1 + (solve("2,3,4")) == 24
You can rearrange that into
solve(24-1,"2,3,4")

That is to say, knowing that adding 1 to the result will
make 24, you can solve the recursive step for 24-1

2 * (solve("1,3,4") == 24
You would invoke
solve(24/2,"1,3,4")

At each recursive level you try
- 4 operators
- each element of the array

Eventually, you end up with a simple case where the function is called
with a single number and a single target value.  If they match, you
have an answer to the problem.```

11. but what is the code behind the declaration? I know how i would use it, but how would i code it to actually solve?

Right now i am using a class, called class Array, and its working quite well, it works for the following:

Subtraction,
Multiplication,
Division,
Multiplication & Divison
Multiplication & Subtraction
and
Division & Subtraction

Works wonders for these. But im using if else if statements, and i grow tired of entering
Code:
```cout << n[1] << " + " << n[2] << " + " << n[3] << " + " << n[4] << " = 24\n";
if (n[1] + n[2] + n[3] + n[4] == 24)
{
cout << "Correct\n";
getche();
}
else
cout << "Incorrect\n";```
see what i mean?

Shouldn't there be something in math.h or something for this problem?

And one more thing you should know: I suck at programming, haha.

Just check out my best work at http://blizzarddog.tk and get TDOD and TDOD Music Pack from the projects->released page:
Thats my best... And yes, based on if else if statements, a bunch of switch case statements and voids.

Ill upload the much better version later though.

Any how... I've included the source file for the 24 game thingy.