Like Tree1Likes

C++ challenge: Sum of numbers

This is a discussion on C++ challenge: Sum of numbers within the Contests Board forums, part of the Community Boards category; This one is fairly simple but it should help get you thinking about slightly different ways of doing things in ...

  1. #1
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239

    C++ challenge: Sum of numbers

    This one is fairly simple but it should help get you thinking about slightly different ways of doing things in C++.

    This challenge is C++ only. If you can solve it in C, I will be extremely impressed, though.

    Challenge: Write a program which reads integer values from standard input until EOF, then displays their sum on standard output. Do not worry about overflow.

    The Goofy Part: Your program must contain EXACTLY ONE SEMICOLON. If you choose to "return 0;" at the end of main(), that semicolon does not count.

    You may, and most likely will, use any and all features of standard C++ including STL.
    Last edited by brewbuck; 10-12-2007 at 11:29 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    One of these is obviously for the "return 0;" at the end of main().
    However, in C++ (and C99), that explicit return 0 is not required. Perhaps just change the requirement to have exactly one semicolon?
    bbroadstone likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    However, in C++ (and C99), that explicit return 0 is not required. Perhaps just change the requirement to have exactly one semicolon?
    Yeah, I think I'll clarify somewhat.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Just read in number after number and print their sum, and we can only use one semicolon (outside of the return from main())?

    Mmkay.

    I managed to do it in C as far as I can understand the rules. I can do it without any semicolons, but I think I have undefined behavior in my program. Moving it to only one semicolon, and I believe it to be valid, if not incredibly stupid, C.

    Should I send it to you via PM or post it here? Be forewarned, it's pretty ugly.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Ah well, decided to give it a try, and it was simpler than I thought. So yeah, by PM, or post here?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Cool idea.

    I've got a solution that works in VC++ 7.1.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by MacGyver View Post
    Just read in number after number and print their sum, and we can only use one semicolon (outside of the return from main())?

    Mmkay.
    It's actually not that weird-looking once you see how it's done.

    I managed to do it in C as far as I can understand the rules. I can do it without any semicolons, but I think I have undefined behavior in my program. Moving it to only one semicolon, and I believe it to be valid, if not incredibly stupid, C.
    I'd love to see it. How about PM first, and let's give other people a few hours to work on solutions?

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    Ah well, decided to give it a try, and it was simpler than I thought. So yeah, by PM, or post here?
    I figure it's a simple task for people who are already primed for the right kind of problem solving. For others it is going to be a real migraine-causer.

    Now, imagine an entire program written in this style. That's how I write C++ :-P

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    How about PM first, and let's give other people a few hours to work on solutions?
    Fair enough, so I have sent you a PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    PM sent.

    Quote Originally Posted by brewbuck View Post
    Now, imagine an entire program written in this style. That's how I write C++ :-P
    I can almost guarentee my solution is not what you're thinking of if you admit to writing in this style regularly.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Laserlight, MacGyver, and Daved -- good stuff so far. I'm glad this isn't as obscure as I feared.

    And no MacGyver, I don't refer to THAT when I say I code like that all the time

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Laserlight, MacGyver, and Daved -- good stuff so far.
    So, can we accuse each other of plagiarism now? I am still amused by the accusation of psychic plagiarism that my prof made against me last semester
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    So, can we accuse each other of plagiarism now? I am still amused by the accusation of psychic plagiarism that my prof made against me last semester
    Heh. You've got me curious about that now.

    But yeah, you and Daved clearly are colluding to win the prize

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Psychic plagarism ... please explain!

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Okay, okay. Basically, the fellow (I think that's literally his academic designation, Senior Fellow) used some plagiarism detection software that detected that several people submitted similiar programs for an assignment (which was, ironically, about detecting software similiarity).

    I was told that my program matched too closely that of a student who I have never met in any way. That student supposedly collaborated with five other students, but strangely my program was not deemed to have been similiar enough to their programs to be matched. My reasoning is that I must have used telepathy or something to communicate, since I clearly never met the student in person, on a forum, by email, phone, etc. As such, we were psychics who cheated with our special abilities.

    Anyway, it turned out that the plagiarism detection software had been fooled by the fact that we had adopted a similiar approach to solving the problem, one that involved breaking it down to several small functions in an already small program. So, despite our different naming schemes and styles, the software still decided that we were cheating and then trying to hide it by making minor name alterations.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
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, 04:29 PM
  2. Finding the sum of even numbers using arrays
    By Fox101 in forum C Programming
    Replies: 7
    Last Post: 12-03-2007, 01: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, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21