Thread: C++ challenge: Sum of numbers

  1. #31
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Main-recursion, Mr D. Always fun and good practice and with commandline parameters use argc as your variable.

  2. #32
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    But then it doesn't compile as C++, Mr. T.

    Very well, if you insist, here's one version. I have a sneaking suspicion that it's very similar to a solution that someone's already posted.
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        while(--argc) {}
    
        if(scanf("&#37;i", &argc) == 1) {
            if(argc += main(0, 0)) {}
        }
    
        if(argv && printf("%i\n", argc)) {}
    
        return argc;
    }
    Last edited by dwks; 10-13-2007 at 03:29 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #33
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Since this is a contest, shouldn't there be a winner?

    I vote me

  4. #34
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by zacs7 View Post
    Since this is a contest, shouldn't there be a winner?

    I vote me
    Sorry guys, I was out of the house for the weekend.

    I'm glad my specific wording turned this into a somewhat more interesting affair. I initially considered asking that only a single statement be used, not a single semicolon, but that seemed to give too much freedom. It's easy to chain expressions using comma in a statement-like way and that isn't what I wanted to see.

    I thought, if I allow only a single semicolon, that will eliminate at the very least the use of for(; and do while(); but it didn't eliminate while-loops or if-statements. On the other hand if I explicitly disallowed all looping constructs I was afraid I would get a homogeneous set of solutions and that would turn the contest into a math exercise (what's 2 + 2? There's only one right answer).

    However I did not imagine that someone would "#define SEMICOLON ;" and use it that way. Definitely within the rules, though.

    So the award for Best Abuse Of The Rules goes to zacs7.

    The award for Doing What I Did goes to Laserlight for being the first one to land it in my Inbox -- but other people were right behind.

    Best Abuse Of Pointers goes to MacGyver.

    And everybody gets the Politically Correct Self-Esteem-Preserving Consolation And Fuzzy-Wuzzy Prize.

  5. #35
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    Best Abuse Of Pointers goes to MacGyver.
    I would just like to thank my friends, and the people that encouraged me to write crappy, non-conforming code. It's you people that have a part in this award, and I think you all should be proud of your part.

    I would name everyone, but time (space?) does not permit, so I shall just say, "Thanks again". Thanks again.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with programming assignment, please!
    By xMEGANx in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2008, 05:29 PM
  2. Finding the sum of even numbers using arrays
    By Fox101 in forum C Programming
    Replies: 7
    Last Post: 12-03-2007, 02:20 PM
  3. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  4. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM