m3ph1st0s's programming puzzle 1 (C++)

26

1

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

Bogdan Alexandru

Posted 2012-09-28T14:26:18.767

Reputation: 653

Question was closed 2016-01-21T16:55:36.377

2I'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 – 2012-09-28T17:59:18.363

3Does 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 – 2012-09-28T18:28:51.730

@KonradRudolph - I don't know what IDE you use, I used DevC++ and it worked fine, so i wouldn't really say it's not valid C++ :) And the result has to be valid also, yes. – Bogdan Alexandru – 2012-09-28T19:38:10.203

4it is not valid. main must return int (read the c++ standard) – BЈовић – 2012-09-28T19:40:25.803

2we are missing the point here. put an int and a return 0 if you mind, I didn't. – Bogdan Alexandru – 2012-09-28T20:00:22.637

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 – 2012-09-28T20:24:02.023

Nice task, but the limit of only 18 characters forbids a lot of creative solutions. What's the point? It's obvious that those short #define ones work, but they aren't really the most interesting ones, are they? – ceased to turn counterclockwis – 2012-09-29T01:37:58.543

2While I find the idea very nice, there is no scoring criteria. – vsz – 2012-09-29T07:38:01.713

Hi everybody, thanks for all the feedback and the advice, since this was my first post I did make some mistakes (like ignoring the int and not mentioning 0 as the ONLY output) but I promise I'll learn out of your advice and next time it will be more precise! Cheers – Bogdan Alexandru – 2012-09-29T17:34:46.437

@leftaroundbout: I limited the size of the line because I wanted to deny the most obvious solution: std::cout<<0; exit(0); – Bogdan Alexandru – 2012-09-29T17:35:45.047

@BogdanAlexandru: Maybe it would be better to forbid it more explicitly, i.e. "the main function must run all of its body" or something? – FireFly – 2012-09-29T19:12:00.057

Answers

14

#define int float

should work as well and is the same length.

Dan

Posted 2012-09-28T14:26:18.767

Reputation: 256

this is what I had in mind when I first came with the idea – Bogdan Alexandru – 2012-09-28T19:41:47.243

5#define int float is actually undefined behavior. You are not allowed to give keywords new meaning. – fredoverflow – 2012-09-28T20:03:07.530

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 – 2012-09-28T20:07:33.840

@Dan: The C++ Standard forbids it. – DeadMG – 2012-09-28T20:16:07.650

@DeadMG Well, it doesn't forbid it, as the standard does not forbid UB. But you end up with undefined behaviour... – sehe – 2012-09-28T20:46:11.140

should work as well and is the same length error: '::main' must return 'int' (minGW 4.4) but #define float int works fine – bartolo-otrit – 2012-10-01T06:23:01.527

You'd have to make it the first line of your main, after main is defined... – Dan – 2012-10-01T13:44:30.010

@Dan In such case it works, sorry. – bartolo-otrit – 2012-10-02T05:10:52.890

I tried this. It works as long as it is defined after int main(). – bacchusbeale – 2014-06-20T23:15:56.670

3It 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. – dan04 – 2014-06-21T01:26:26.323

33

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

frozenkoi

Posted 2012-09-28T14:26:18.767

Reputation: 501

24

#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

mob

Posted 2012-09-28T14:26:18.767

Reputation: 2 506

My first idea when read the question))) – Qwertiy – 2015-06-25T15:37:31.550

That's what I wanted to submit... +1 for you. – H2CO3 – 2012-10-01T14:14:08.020

23

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?

sehe

Posted 2012-09-28T14:26:18.767

Reputation: 360

21

#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.

Mr Lister

Posted 2012-09-28T14:26:18.767

Reputation: 3 668

2int a=0;//\ will also do the trick – copy – 2012-09-28T18:00:50.090

2Absolutely. But is this a "shortest line wins" kind of contest? – Mr Lister – 2012-09-28T18:02:56.813

I thought so, but it's not. Nice trick by the way – copy – 2012-09-28T18:10:51.040

2

Yes, shortest solution wins on codegolf SE. see faq

– BЈовић – 2012-09-28T19:38:40.110

Hi, like I said I used DevC++ so I didn't mind about main's return type :) – Bogdan Alexandru – 2012-09-28T19:41:05.873

2@BЈовић the FAQ says the shortest solution wins for actual code-golf questions. This question is not clearly a code-golf question. – kojiro – 2012-09-29T14:04:34.370

multi-line comment - great trick! – bartolo-otrit – 2012-10-01T06:19:16.390

11

18, including newline

#define float int

shiona

Posted 2012-09-28T14:26:18.767

Reputation: 2 889

I'm not really sure this works...ur declaring c as a float and initializing it with 0.5 – Bogdan Alexandru – 2012-09-28T19:40:10.350

5Which will truncate to zero. – DeadMG – 2012-09-28T20:15:41.113

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 – 2012-09-28T21:31:45.980

2@BogdanAlexandru take a gander at the C++ standard, it specifically details the implicit conversion at play here. – oldrinb – 2012-09-28T21:39:48.447

2Note that although all compilers allow this, the standard prohibits redefining keywords (and float is a keyword). – avakar – 2012-09-29T06:51:16.967

11

#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.

Andrey Regentov

Posted 2012-09-28T14:26:18.767

Reputation: 211

will output more than a "0" – Bogdan Alexandru – 2012-10-01T12:20:31.023

2why the ternary operator wouldn't work? – Andrey Regentov – 2012-10-11T05:34:59.383

9

#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.

FireFly

Posted 2012-09-28T14:26:18.767

Reputation: 7 107

7

#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.

marinus

Posted 2012-09-28T14:26:18.767

Reputation: 30 224

7

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)

NRGdallas

Posted 2012-09-28T14:26:18.767

Reputation: 707

Oh well, the output should be just 0, I thought it was obvious, otherwise there would be no puzzle, would it? – Bogdan Alexandru – 2012-09-28T19:42:50.393

7@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 – 2012-09-28T22:34:11.667

7

#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.

Ashrr

Posted 2012-09-28T14:26:18.767

Reputation: 71

1Don't think, this code returns 0. But you can replace space via exclamation mark. Anyway, I'm not sure when puts returns zero. – Qwertiy – 2015-06-25T15:33:30.693

1Best solution yet, because it flies in the face of anything related to C++. – fabspro – 2012-09-29T12:10:54.340

3`puts´ was not declared in this scope – shiona – 2012-09-30T07:44:20.673

@shiona what compiler are you using? – Ashrr – 2012-10-02T07:16:40.367

@Ashrr gcc (g++) 4.5.4 – shiona – 2012-10-02T16:32:25.743

If you use return write(1,"0",1); it compiles under g++ ok – gnibbler – 2012-10-04T12:33:22.077

@gnibbler it's too long for solution, though. – Ashrr – 2012-10-09T16:11:56.610

3

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

Csq

Posted 2012-09-28T14:26:18.767

Reputation: 131

2

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;

Toby Speight

Posted 2012-09-28T14:26:18.767

Reputation: 5 058

2This does not compile, because a is const. – Csq – 2015-06-25T17:08:48.243

Oops, I should have given it to a compiler! :-( – Toby Speight – 2015-06-25T17:20:20.367

0

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;
}

Toby Speight

Posted 2012-09-28T14:26:18.767

Reputation: 5 058

0

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

baby-rabbit

Posted 2012-09-28T14:26:18.767

Reputation: 1 623

Nice try but violates the requirement: valid C++. – Konrad Rudolph – 2012-10-01T07:00:26.520

-1

#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;
}

Qwertiy

Posted 2012-09-28T14:26:18.767

Reputation: 2 697

Ups.. Posted the answer before looking the others. There is already the same answer: http://codegolf.stackexchange.com/a/8507/32091

– Qwertiy – 2015-06-25T15:37:01.473

-1

#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...

tow

Posted 2012-09-28T14:26:18.767

Reputation: 101

1The problem here is that the int a before the if is not being initialized, so a could have any value. – frozenkoi – 2012-10-01T06:35:51.163

1This is undefined, you’re using an uninitialised value for a. – Konrad Rudolph – 2012-10-01T06:59:48.283