C Board  

Go Back   C Board > Community Boards > Contests Board

Reply
 
LinkBack Thread Tools Display Modes
Old 10-12-2007, 11:06 AM   #1
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
brewbuck is offline   Reply With Quote
Old 10-12-2007, 11:23 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
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?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-12-2007, 11:28 AM   #3
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
brewbuck is offline   Reply With Quote
Old 10-12-2007, 11:41 AM   #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.
__________________
MacGyver is offline   Reply With Quote
Old 10-12-2007, 11:47 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-12-2007, 11:47 AM   #6
Registered User
 
Join Date: Jan 2005
Posts: 7,137
Cool idea.

I've got a solution that works in VC++ 7.1.
Daved is offline   Reply With Quote
Old 10-12-2007, 11:51 AM   #7
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.

Quote:
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?
brewbuck is offline   Reply With Quote
Old 10-12-2007, 11:53 AM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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
brewbuck is offline   Reply With Quote
Old 10-12-2007, 11:54 AM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-12-2007, 12:02 PM   #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.
__________________
MacGyver is offline   Reply With Quote
Old 10-12-2007, 12:14 PM   #11
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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
brewbuck is offline   Reply With Quote
Old 10-12-2007, 12:18 PM   #12
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-12-2007, 12:19 PM   #13
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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
brewbuck is offline   Reply With Quote
Old 10-12-2007, 12:24 PM   #14
The superheterodyne.
 
twomers's Avatar
 
Join Date: Dec 2005
Location: Ireland
Posts: 2,205
Psychic plagarism ... please explain!
__________________
I blag!
twomers is offline   Reply With Quote
Old 10-12-2007, 12:32 PM   #15
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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