Thread: how to write a C code that evaluates 7, 7, 7, 1 = 100

1. how to write a C code that evaluates 7, 7, 7, 1 = 100

Hello. Can anybody help me with writing a C code that evaluates 100, using 7, 7, 7, 1 and usingthe four basic arithmetic operations: +, -, *, /.

I found the answer long before; it is (7 + 7) * (7 + 1 / 7) = 100

But I need a C code to do this job.

2. Actually the question means this that how you will rearrange the four numbers 7,7,7,1 along with operators to get 100 using a C code? is that the question?

3. Surely in this case you mean 7, 7, 7, 7, 1.

First, you need some way of representing rational numbers. A struct with two integers, a numerator and denominator, will do.

One way to solve this problem is to represent expressions in postfix notation. With N numbers, you'll have N-1 operators. A valid postfix expression is any sequence x0, x1, x2, ..., x(2n) for which for any given M, 0 <= M <= 2N, the number of operands in the first M elements is at least one greater than the number of operators. Then generate all possible valid postfix expressions. If any of the expressions evaluate to 100 (or whatever number you're looking for), you've succeeded. I assume you can google for yourself what postfix notation is, or what reverse Polish notation is. ('Reverse Polish notation' is a common name for postfix notation.)

This algorithm is slow and its running time grows rapidly for longer lists of numbers. This is a brute force way of solving the problem, so I'd wager there are faster ones around. Generating all valid expressions is a slightly tricky part, too, and postfix notation might not be the best datastructure for your problem. But hopefully this helps you think of better solutions.

4. You mean a program which tries all possible combinations until one (or more) combinations results in 100 ?

5. Originally Posted by Salem
You mean a program which tries all possible combinations until one (or more) combinations results in 100 ?
Yes, exactly. But then, one must be able to see the solution on the screen (output).

6. Originally Posted by ramya
Actually the question means this that how you will rearrange the four numbers 7,7,7,1 along with operators to get 100 using a C code? is that the question?
Yes, exactly so.

7. Look up permutations and combinations.

8. Originally Posted by Salem
Look up permutations and combinations.
Yeah, of course the solution is based on combinations and permutations; but the difficulty is already here: How can you permutate or combine both numbers and mathematical operators (+,-,*,/) ? At least, we must determine an algorithm for this job, don't we? Such an algorithm probably helps us with the solution.

9. > At least, we must determine an algorithm for this job, don't we?
Yes, start doing some reading on permutation algorithms and figure out a way of permuting something simple like
num op num
num being all the permutations of the numbers, and op being all of +-*/

Then think about expanding it to cover 3 and 4 number cases as well.

> Such an algorithm probably helps us with the solution.
Yes, it will.

But this is what separates the real programmers from the wannabe's. Doing the whole job is what it's all about - the actual code is just a small part of developing any meaningful program.