27
\$\begingroup\$

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!!

\$\endgroup\$
11
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Mr Lister
    Commented Sep 28, 2012 at 17:59
  • 3
    \$\begingroup\$ 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++.) \$\endgroup\$ Commented Sep 28, 2012 at 18:28
  • 4
    \$\begingroup\$ it is not valid. main must return int (read the c++ standard) \$\endgroup\$ Commented Sep 28, 2012 at 19:40
  • 2
    \$\begingroup\$ we are missing the point here. put an int and a return 0 if you mind, I didn't. \$\endgroup\$ Commented Sep 28, 2012 at 20:00
  • 11
    \$\begingroup\$ @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++. \$\endgroup\$
    – DeadMG
    Commented Sep 28, 2012 at 20:24

17 Answers 17

35
\$\begingroup\$

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

\$\endgroup\$
0
24
\$\begingroup\$
#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

\$\endgroup\$
2
  • \$\begingroup\$ That's what I wanted to submit... +1 for you. \$\endgroup\$
    – H2CO3
    Commented Oct 1, 2012 at 14:14
  • \$\begingroup\$ My first idea when read the question))) \$\endgroup\$
    – Qwertiy
    Commented Jun 25, 2015 at 15:37
23
\$\begingroup\$

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?

\$\endgroup\$
22
\$\begingroup\$
#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.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ int a=0;//\ will also do the trick \$\endgroup\$
    – copy
    Commented Sep 28, 2012 at 18:00
  • 2
    \$\begingroup\$ Absolutely. But is this a "shortest line wins" kind of contest? \$\endgroup\$
    – Mr Lister
    Commented Sep 28, 2012 at 18:02
  • \$\begingroup\$ I thought so, but it's not. Nice trick by the way \$\endgroup\$
    – copy
    Commented Sep 28, 2012 at 18:10
  • 2
    \$\begingroup\$ Yes, shortest solution wins on codegolf SE. see faq \$\endgroup\$ Commented Sep 28, 2012 at 19:38
  • 2
    \$\begingroup\$ @BЈовић the FAQ says the shortest solution wins for actual code-golf questions. This question is not clearly a code-golf question. \$\endgroup\$
    – kojiro
    Commented Sep 29, 2012 at 14:04
14
\$\begingroup\$
#define int float

should work as well and is the same length.

\$\endgroup\$
10
  • \$\begingroup\$ this is what I had in mind when I first came with the idea \$\endgroup\$ Commented Sep 28, 2012 at 19:41
  • 6
    \$\begingroup\$ #define int float is actually undefined behavior. You are not allowed to give keywords new meaning. \$\endgroup\$ Commented Sep 28, 2012 at 20:03
  • \$\begingroup\$ 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." \$\endgroup\$
    – Dan
    Commented Sep 28, 2012 at 20:07
  • \$\begingroup\$ @Dan: The C++ Standard forbids it. \$\endgroup\$
    – DeadMG
    Commented Sep 28, 2012 at 20:16
  • 3
    \$\begingroup\$ It may be UB, but several popular compilers support it anyway. I've done #define int ERROR to force myself to use the equivalent of int32_t instead of built-in types. By the time I got around to int main(), I had forgotten about the macro and wondered why the heck my code wouldn't compile. \$\endgroup\$
    – dan04
    Commented Jun 21, 2014 at 1:26
12
\$\begingroup\$

18, including newline

#define float int
\$\endgroup\$
5
  • \$\begingroup\$ I'm not really sure this works...ur declaring c as a float and initializing it with 0.5 \$\endgroup\$ Commented Sep 28, 2012 at 19:40
  • 5
    \$\begingroup\$ Which will truncate to zero. \$\endgroup\$
    – DeadMG
    Commented Sep 28, 2012 at 20:15
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Sep 28, 2012 at 21:31
  • 2
    \$\begingroup\$ @BogdanAlexandru take a gander at the C++ standard, it specifically details the implicit conversion at play here. \$\endgroup\$
    – obataku
    Commented Sep 28, 2012 at 21:39
  • 3
    \$\begingroup\$ Note that although all compilers allow this, the standard prohibits redefining keywords (and float is a keyword). \$\endgroup\$
    – avakar
    Commented Sep 29, 2012 at 6:51
11
\$\begingroup\$
#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.

\$\endgroup\$
2
  • \$\begingroup\$ will output more than a "0" \$\endgroup\$ Commented Oct 1, 2012 at 12:20
  • 2
    \$\begingroup\$ why the ternary operator wouldn't work? \$\endgroup\$ Commented Oct 11, 2012 at 5:34
9
\$\begingroup\$
#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.

\$\endgroup\$
8
\$\begingroup\$
#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.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Best solution yet, because it flies in the face of anything related to C++. \$\endgroup\$
    – fabspro
    Commented Sep 29, 2012 at 12:10
  • 3
    \$\begingroup\$ `puts´ was not declared in this scope \$\endgroup\$
    – shiona
    Commented Sep 30, 2012 at 7:44
  • \$\begingroup\$ @shiona what compiler are you using? \$\endgroup\$
    – Ashrr
    Commented Oct 2, 2012 at 7:16
  • \$\begingroup\$ @Ashrr gcc (g++) 4.5.4 \$\endgroup\$
    – shiona
    Commented Oct 2, 2012 at 16:32
  • 1
    \$\begingroup\$ Don't think, this code returns 0. But you can replace space via exclamation mark. Anyway, I'm not sure when puts returns zero. \$\endgroup\$
    – Qwertiy
    Commented Jun 25, 2015 at 15:33
8
\$\begingroup\$
#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.

\$\endgroup\$
7
\$\begingroup\$

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)

\$\endgroup\$
2
  • \$\begingroup\$ Oh well, the output should be just 0, I thought it was obvious, otherwise there would be no puzzle, would it? \$\endgroup\$ Commented Sep 28, 2012 at 19:42
  • 8
    \$\begingroup\$ @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. \$\endgroup\$
    – ardnew
    Commented Sep 28, 2012 at 22:34
3
\$\begingroup\$

12 chars, similar to mob's solution

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

other combinations also work, like #define a c or #define c 0

\$\endgroup\$
2
\$\begingroup\$

I know it's not , but I seem to be wearing that hat today!

#include <iostream>
int main() 
{
   const int a=1;
   const int b=2;
   const float c=0.5;

--a;

   std::cout << a/b-a*c;
}

five chars, including the newline;

\$\endgroup\$
2
  • 2
    \$\begingroup\$ This does not compile, because a is const. \$\endgroup\$
    – Csq
    Commented Jun 25, 2015 at 17:08
  • \$\begingroup\$ Oops, I should have given it to a compiler! :-( \$\endgroup\$ Commented Jun 25, 2015 at 17:20
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Nice try but violates the requirement: valid C++. \$\endgroup\$ Commented Oct 1, 2012 at 7:00
0
\$\begingroup\$

A variant on Mr Lister's answer but slightly less obvious.

#include <iostream>
int main() 
{
   const float a=1; //??/
   const int a=1;
   const int b=2;
   const float c=0.5;
   std::cout << a/b-a*c;
}
\$\endgroup\$
-1
\$\begingroup\$
#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...

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The problem here is that the int a before the if is not being initialized, so a could have any value. \$\endgroup\$
    – frozenkoi
    Commented Oct 1, 2012 at 6:35
  • 1
    \$\begingroup\$ This is undefined, you’re using an uninitialised value for a. \$\endgroup\$ Commented Oct 1, 2012 at 6:59
-1
\$\begingroup\$
#define a 0

http://codepad.org/N06weGJc

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

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