Thread: C++ challenge: Sum of numbers

  1. #1
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396

    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,210
    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
    28,413
    Ah well, decided to give it a try, and it was simpler than I thought. So yeah, by PM, or post here?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

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

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,210
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,273
    Psychic plagarism ... please explain!

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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