Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Hello C++ programmers! This is the first of a series of C++ puzzles for you. Hope you will enjoy.

So, puzzle no.1:

Given the following program:

#include <iostream>
int main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;
   std::cout << a/b-a*c;
}

Insert some code on a single new line anywhere inside the program so that the output will be 0. The new line will contain AT MOST 18 characters (including spaces), and the rest of the lines will remain unmodified. To be clear, here is an example of a valid new code:

#include <iostream>
int main() 
{
   const int a=1;
   const int b=2;
   int* p = NULL;
   const float c=0.5;
   std::cout << a/b-a*c;
}

A new line with 15 characters was inserted so it's ok. However it does not solve the problem.

If this is too simple for you, don't worry, more is coming!!

share|improve this question
2  
I'm very happy someone posts a C++ question occasionally! I mean, with all the puzzles where a C++ solution would be 20 or 30 lines, then people posting solutions in J or K or Golfscript becomes frustrating after a while. –  Mr Lister Sep 28 '12 at 17:59
3  
Does the result have to be valid, well-defined C++ or can it use UB? (But like Mr Lister noted, the original code isn’t even valid C++.) –  Konrad Rudolph Sep 28 '12 at 18:28
4  
it is not valid. main must return int (read the c++ standard) –  BЈовић Sep 28 '12 at 19:40
2  
we are missing the point here. put an int and a return 0 if you mind, I didn't. –  Bogdan Alexandru Sep 28 '12 at 20:00
11  
@Bogdan: Dafuq? For one, DevC++ is so unspeakably ancient, it's output is irrelevant. And secondly, whether or not any given compiler in any given configuration at any time targetting any OS accepts it does not make it valid C++. –  DeadMG Sep 28 '12 at 20:24
show 6 more comments

13 Answers

up vote 9 down vote accepted
#define int float

should work as well and is the same length.

share|improve this answer
    
this is what I had in mind when I first came with the idea –  Bogdan Alexandru Sep 28 '12 at 19:41
2  
#define int float is actually undefined behavior. You are not allowed to give keywords new meaning. –  FredOverflow Sep 28 '12 at 20:03
    
Fred, can you cite your sources? The GCC cpp docs say "You may define any valid identifier as a macro, even if it is a C keyword." –  Dan Sep 28 '12 at 20:07
    
@Dan: The C++ Standard forbids it. –  DeadMG Sep 28 '12 at 20:16
    
@DeadMG Well, it doesn't forbid it, as the standard does not forbid UB. But you end up with undefined behaviour... –  sehe Sep 28 '12 at 20:46
show 3 more comments

We can get rid of a=1 by moving it into another scope:

#include <iostream>
main() 
{
int a=0;if(0)
    const int a=1;
    const int b=2;
    const float c=0.5;
    std::cout << a/b-a*c;
}

This is I think 13 characters. Or better yet get a new a that also results in 0:

#include <iostream>
int main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;
if(int a=2)
   std::cout << a/b-a*c;
}

That's 11 characters

share|improve this answer
    
+1, very nice solution! –  avakar Sep 29 '12 at 6:56
add comment
#include <iostream>
main() 
{
   const int a=1;
#define a 0
   const int b=2;
   const float c=0.5;
   std::cout << a/b-a*c;
}

1 new line, 12 new chars

share|improve this answer
    
That's what I wanted to submit... +1 for you. –  H2CO3 Oct 1 '12 at 14:14
add comment
#include <iostream>
main() 
{
const int a=0;//\
   const int a=1;
   const int b=2;
   const float c=0.5;
   std::cout << a/b-a*c;
}

17 chars.

By the way, the original program doesn't compile under MSVC, which complains that main doesn't have a return type.

share|improve this answer
2  
int a=0;//\ will also do the trick –  copy Sep 28 '12 at 18:00
2  
Absolutely. But is this a "shortest line wins" kind of contest? –  Mr Lister Sep 28 '12 at 18:02
    
I thought so, but it's not. Nice trick by the way –  copy Sep 28 '12 at 18:10
2  
Yes, shortest solution wins on codegolf SE. see faq –  BЈовић Sep 28 '12 at 19:38
    
Hi, like I said I used DevC++ so I didn't mind about main's return type :) –  Bogdan Alexandru Sep 28 '12 at 19:41
show 2 more comments

So, #define a 0, Done. I saw that was posted - unsurprisingly.

Surprisingly, this wasn't posted:

#include <iostream>
main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;
   std::cout<<0||
   std::cout << a/b-a*c;
}

14 chars

That should do, right?

share|improve this answer
add comment

18, including newline

#define float int
share|improve this answer
    
I'm not really sure this works...ur declaring c as a float and initializing it with 0.5 –  Bogdan Alexandru Sep 28 '12 at 19:40
4  
Which will truncate to zero. –  DeadMG Sep 28 '12 at 20:15
    
you're right, funny thing I've never come across initializing an int with decimal value, I'd thought it would be compiler error, but it only issues a warning –  Bogdan Alexandru Sep 28 '12 at 21:31
2  
@BogdanAlexandru take a gander at the C++ standard, it specifically details the implicit conversion at play here. –  oldrinb Sep 28 '12 at 21:39
2  
Note that although all compilers allow this, the standard prohibits redefining keywords (and float is a keyword). –  avakar Sep 29 '12 at 6:51
add comment
#include <iostream>
main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;
#define a 0;1
   std::cout << a/b-a*c;
}

14 characters.

share|improve this answer
add comment
#include <iostream>
main()
{
   const int a=1;
   const int b=2;
   const float c=0.5;
std::cout<<0;//\
   std::cout << a/b-a*c;
}

It's 17 characters so it just fits.

share|improve this answer
add comment

I do not know C++, however based on the question, couldn't you just input a line to simply output 0? the question specifies the output should be 0, it does not specify you must CHANGE the output to 0.

std::cout << 0

(I have 0 clue on C++, perhaps somebody can use this concept though)

share|improve this answer
    
Oh well, the output should be just 0, I thought it was obvious, otherwise there would be no puzzle, would it? –  Bogdan Alexandru Sep 28 '12 at 19:42
4  
@BogdanAlexandru You will find that exploiting a poorly written question is a common technique to solving these puzzles. If you want to prevent users from taking these shortcuts, spend a few extra minutes analyzing your own question and try to remove any potential ambiguity. –  ardnew Sep 28 '12 at 22:34
add comment
#include <iostream>
main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;
   return puts("0");
   std::cout << a/b-a*c;
}

17 chars.

share|improve this answer
1  
Best solution yet, because it flies in the face of anything related to C++. –  fabspro Sep 29 '12 at 12:10
2  
`puts´ was not declared in this scope –  shiona Sep 30 '12 at 7:44
    
@shiona what compiler are you using? –  Ashrr Oct 2 '12 at 7:16
    
@Ashrr gcc (g++) 4.5.4 –  shiona Oct 2 '12 at 16:32
    
If you use return write(1,"0",1); it compiles under g++ ok –  gnibbler Oct 4 '12 at 12:33
show 1 more comment
#include <iostream>
int main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;
   1?std::cout<<0:
   std::cout << a/b-a*c;
}

15 chars.

share|improve this answer
    
will output more than a "0" –  Bogdan Alexandru Oct 1 '12 at 12:20
    
why the ternary operator wouldn't work? –  Andrey Regentov Oct 11 '12 at 5:34
add comment

c++ whatever...

echo "0"; exit
#include <iostream>
int main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;
   std::cout << a/b-a*c;
}

run via:

sh mp.cpp
share|improve this answer
    
Nice try but violates the requirement: valid C++. –  Konrad Rudolph Oct 1 '12 at 7:00
add comment
#include <iostream>
int main() 
{
int a;if(a)
   const int a=1;
   const int b=2;
   const float c=0.5;
   std::cout << a/b-a*c;
}

How about these 11 chars...

share|improve this answer
1  
The problem here is that the int a before the if is not being initialized, so a could have any value. –  frozenkoi Oct 1 '12 at 6:35
1  
This is undefined, you’re using an uninitialised value for a. –  Konrad Rudolph Oct 1 '12 at 6:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.