Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The 24 Game is an arithmetical game for java in which the objective is to find a

ID: 3567642 • Letter: T

Question

The 24 Game is an arithmetical game for java in which the objective is to find a way to manipulate four integers so that the end result is 24. Addition, subtraction, multiplication, or division in any order of the numbers may be used to make the four digits operations from one to nine equal 24.

The rules are simple: you have to use each number only once and only the 4 numbers that were read from the user to find one equation to obtain 24.

For example, for the numbers 4,7,8,8, a possible solution is: (7-(8/8))*4=24.

Most sets of 4 digits can be used in multiple equations that result in 24: for example the input: 2, 2, 4 and 7 can be used in multiple ways to obtain

24: 2+2*(4+7) = 24

2+2*(7+4) = 24

(2+2)*7-4 = 24

(2*2)*7-4 = 24 2*(2*7)-4 = 24

There are also combinations of 4 numbers that cannot result into any equation equal with 24. For example 1,1,1,1. In this case, your program should return that there is no possible equation equal with 24.

Note: Although we will enter 4 integers between 1 and 9, we will use doubles to compute all the operations. For example, the numbers 3,3,8,8 can be combined into the formula: 8/(3-8/3) = 24.

Workflow: Your program should read the 4 numbers from the user and output a formula that results in 24. The algorithm should enumerate all the possible orders of 4 numbers, all the possible combinations and all the possible formulas. There is no required GUI for this project.

Explanation / Answer

#include #include #include using namespace std; bool MakeSum(int num1, int num2, int num3, int num4) { vector vi; vi.push_back(num1); vi.push_back(num2); vi.push_back(num3); vi.push_back(num4); sort(vi.begin(),vi.end()); char a1 = '+'; char a2 = '-'; char a3 = '*'; char a4 = '/'; vector va; va.push_back(a1); va.push_back(a2); va.push_back(a3); va.push_back(a4); sort(va.begin(),va.end()); while(next_permutation(vi.begin(),vi.end())) { while(next_permutation(va.begin(),va.end())) { cout