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

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    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. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    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. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Quote 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).
    Last edited by filanfistan; 10-20-2005 at 09:20 AM.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Quote 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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Quote 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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. Replies: 6
    Last Post: 04-04-2003, 10:09 PM
  3. How do I write more efficient code?
    By minesweeper in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 11:08 AM
  4. How to write code to change the printer resolution
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 07-26-2002, 06:04 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM