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.

Printable View

- 10-19-2005filanfistanhow 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. - 10-19-2005ramya
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?

- 10-19-2005Rashakil Fol
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. - 10-19-2005Salem
You mean a program which tries all possible combinations until one (or more) combinations results in 100 ?

- 10-20-2005filanfistanQuote:

Originally Posted by**Salem**

- 10-20-2005filanfistanQuote:

Originally Posted by**ramya**

- 10-20-2005Salem
Look up permutations and combinations.

- 10-21-2005filanfistanQuote:

Originally Posted by**Salem**

- 10-21-2005Salem
> 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.