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.
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.
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?
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.
Last edited by Rashakil Fol; 10-19-2005 at 05:45 AM.
You mean a program which tries all possible combinations until one (or more) combinations results in 100 ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Yes, exactly. But then, one must be able to see the solution on the screen (output).Originally Posted by Salem
Last edited by filanfistan; 10-20-2005 at 09:20 AM.
Yes, exactly so.Originally Posted by ramya
Look up permutations and combinations.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.Originally Posted by Salem
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.