26
\$\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
    Sep 28 '12 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\$ Sep 28 '12 at 18:28
  • 4
    \$\begingroup\$ it is not valid. main must return int (read the c++ standard) \$\endgroup\$ Sep 28 '12 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\$ Sep 28 '12 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
    Sep 28 '12 at 20:24

17 Answers 17

33
\$\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
    Oct 1 '12 at 14:14
  • \$\begingroup\$ My first idea when read the question))) \$\endgroup\$
    – Qwertiy
    Jun 25 '15 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\$
21
\$\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
    Sep 28 '12 at 18:00
  • 2
    \$\begingroup\$ Absolutely. But is this a "shortest line wins" kind of contest? \$\endgroup\$
    – Mr Lister
    Sep 28 '12 at 18:02
  • \$\begingroup\$ I thought so, but it's not. Nice trick by the way \$\endgroup\$
    – copy
    Sep 28 '12 at 18:10
  • 2
    \$\begingroup\$ Yes, shortest solution wins on codegolf SE. see faq \$\endgroup\$ Sep 28 '12 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
    Sep 29 '12 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\$ Sep 28 '12 at 19:41
  • 5
    \$\begingroup\$ #define int float is actually undefined behavior. You are not allowed to give keywords new meaning. \$\endgroup\$ Sep 28 '12 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
    Sep 28 '12 at 20:07
  • \$\begingroup\$ @Dan: The C++ Standard forbids it. \$\endgroup\$
    – DeadMG
    Sep 28 '12 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
    Jun 21 '14 at 1:26
11
\$\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\$ Sep 28 '12 at 19:40
  • 5
    \$\begingroup\$ Which will truncate to zero. \$\endgroup\$
    – DeadMG
    Sep 28 '12 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\$ Sep 28 '12 at 21:31
  • 2
    \$\begingroup\$ @BogdanAlexandru take a gander at the C++ standard, it specifically details the implicit conversion at play here. \$\endgroup\$
    – obataku
    Sep 28 '12 at 21:39
  • 2
    \$\begingroup\$ Note that although all compilers allow this, the standard prohibits redefining keywords (and float is a keyword). \$\endgroup\$
    – avakar
    Sep 29 '12 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\$ Oct 1 '12 at 12:20
  • 2
    \$\begingroup\$ why the ternary operator wouldn't work? \$\endgroup\$ Oct 11 '12 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\$
7
\$\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
    Sep 29 '12 at 12:10
  • 3
    \$\begingroup\$ `puts´ was not declared in this scope \$\endgroup\$
    – shiona
    Sep 30 '12 at 7:44
  • \$\begingroup\$ @shiona what compiler are you using? \$\endgroup\$
    – Ashrr
    Oct 2 '12 at 7:16
  • \$\begingroup\$ @Ashrr gcc (g++) 4.5.4 \$\endgroup\$
    – shiona
    Oct 2 '12 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
    Jun 25 '15 at 15:33
7
\$\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\$ Sep 28 '12 at 19:42
  • 7
    \$\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
    Sep 28 '12 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
    Jun 25 '15 at 17:08
  • \$\begingroup\$ Oops, I should have given it to a compiler! :-( \$\endgroup\$ Jun 25 '15 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\$ Oct 1 '12 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
    Oct 1 '12 at 6:35
  • 1
    \$\begingroup\$ This is undefined, you’re using an uninitialised value for a. \$\endgroup\$ Oct 1 '12 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.