This challenge is related to the C++ language. Note that challenges that require the answers to be in a specific language are generally discouraged.

learn more… | top users | synonyms

12
votes
14answers
3k views

Compare two integers in C or C++ without comparison operators

Produce the shortest program which takes two signed integers as input (through stdin or as arguments) and displays 3 different outputs depending upon whether the first number is (1) greater than, (2) ...
277
votes
56answers
70k views

Tweetable Mathematical Art

Integer math can generate amazing patterns when laid out over a grid. Even the most basic functions can yield stunningly elaborate designs! Your challenge Write 3 Tweetable (meaning 140 characters ...
7
votes
0answers
526 views

X greater than 3 with at least 2 difference between X and Y

I am trying to golf down some C++. Is it possible to make this condition shorter? X > 3 & X - Y > 1 (Apart from removing whitespace, of course.) So, X is at least 4 but X >= Y + 2. X ...
8
votes
8answers
1k views

Shortest C \ C++ function to generate -1, 0 ,1

Let a function receive two integers i and j between 0 and 3, it should return the value in position (i,j) in the following 2D array: 1, 0, -1, 0 1, 0, -1, 0 0, 1, 0, -1 0, -1, 0, 1 Shortest ...
4
votes
2answers
355 views

Generate a self-extractor application

Create a program in any language, that takes an arbitrary binary input, and generates a valid C++11 program, which when compiled and run will print this binary input. So you could do this (on linux ...
4
votes
6answers
829 views

Make FizzBuzz using only preprocessor directives

I have a source file here: #include <FizzBuzz.h> fizzbuzz And I want to make "fizzbuzz" turn into a FizzBuzz program solely through the preprocessor. Your task is to write a FizzBuzz.h that ...
3
votes
1answer
528 views

Flip the commented-out code [closed]

This puzzle works for any programming language that has the same // single-line and /* ... */ multi-line comments that C++, C# and Java do. You are debugging an application. You find a fishy block of ...
1
vote
1answer
273 views

Generate numerical permutations with conditions and template metaprogramming

This, but via template metaprogramming: template <int n> struct insanity { int[][n] permutations; } Generate all numbers with n digits (0-9) such that each digit is either: Zero The first ...
12
votes
1answer
536 views

Implement an IEEE 754 64-bit binary floating point number through integer manipulation

(I've tagged the question "C" for the time being, but if you're aware of another language that supports unions, you can also use that.) Your task is to build the four standard mathematical operators ...
-1
votes
1answer
168 views

Why does one stack variable have a weird address? [closed]

I was called a liar when I showed my output (while debugging a problem). So I thought I'll make it into a puzzle. The output of this code ... T a,b,c, d; printf("%p %p %p %p %d %d %d %d %d", &a, ...
5
votes
4answers
1k views

RPN calculator without pointers

Me and some other students were at a bar discussing programming languages, in particular the C programming language. At some point a first year student said he didn't need no damn pointers. Another ...
5
votes
3answers
2k views

The shortest code to tell if a number is even or odd

One of my colleagues proposed us a challenge: to write the shortest C/C++ program to determine if a number is even or odd. These are the requirements: Get a number from keyboard, at run-time (not ...
3
votes
3answers
535 views

m3ph1st0s's programming puzzle 4 (C/C++): “3-way swap”

My last puzzle has generated some confusion and controversy so I decided to give up, for now, on those "replace one character"-type puzzles. Hope this 4th puzzle will redeem myself after all the ...
11
votes
5answers
1k views

m3ph1st0s's programming puzzle 3 (C): “Easy bug”

This is the 3rd of my series of C/C++ puzzles; in case you missed the first 2 they are here: (1) m3ph1st0s's programming puzzle 1 (C++) (2) m3ph1st0s's programming puzzle 2 (C++): "Call ...
3
votes
4answers
491 views

Shortest undefined behavior sample in C++

What's the shortest, well-formed C++ code that exhibits undefined behavior?
1
vote
11answers
652 views

Code-golf: square of the number of ones

I'm solving problem: in file "a.in" given the number N - length of the number consists of ones. Need to gets square of it, and put this in file "a.out". This is my shortest solution(150 bytes): char ...
-2
votes
2answers
990 views

Conversion to palindrome with minimal points used

Input: a word (2-100 characters) Convert this word to a palindrome: delete character - 13 points add character - 12 points increase character - 5 points ('d' > 'e') decrease character - 4 points ...
6
votes
7answers
823 views

Shortest code that return SIGSEGV among the given languages

I come across a question in a coding competion "write a code that returns SIGSEGV(Segmentation fault ) " . Points were given on the basis of length of code. The prgramming languages available ...
1
vote
2answers
546 views

A small puzzle. Why does this code work the way it does? [closed]

This was given to me by a friend of mine, it prints out 0 through 9. We have been quite puzzled over this. Does anyone know why this works? #include <iostream> using namespace std; void x(int ...
3
votes
2answers
541 views

Fastest C/C++ comparison function for an opaque field containing doubles

Description: Write the fastest comparison function for an opaque field containing a double. Here is my example: // Three-way comparison function: // if a < b: negative result // if a > b: ...
-4
votes
0answers
1k views

How can I loop through all the possibilites of building a panel with two diffenent block sizes? [closed]

Im trying to land an interview for a company in Austin, Tx. before I can be considered they asked me to solve a programming puzzle. I really need this interview. Your niece was given a set of ...
12
votes
8answers
5k views

Compute the CRC32 table at compile-time [closed]

The reference implementation of CRC32 computes a lookup table at runtime: /* Table of CRCs of all 8-bit messages. */ unsigned long crc_table[256]; /* Flag: has the table been computed? Initially ...
27
votes
4answers
4k views

Solve the eight queens problem at compile-time

Can you solve the eight queens puzzle at compile-time? Pick any suitable output format. I'm particularly interested in a C++ template metaprogramming solution, but you can use languages that have ...
37
votes
11answers
12k views

Generate the longest error message in C++

Write a short program, that would generate the longest possible error message, in a standard C++ compiler (gcc, cl.exe, icc, or clang). The score of each entry is the number of characters in the ...
23
votes
23answers
5k views

Print 1 to 1000 in C++ without semi-colons

Following this popular question, present your solution which prints the numbers 1 to 1000 (all of them, not the string "1 to 1000" verbatim or something funny) in C++ without using any semi-colons. ...
21
votes
9answers
2k views

Tips for golfing in C++

What general tips do you have for golfing in C++? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C++ (e.g. "remove comments" is not ...
9
votes
4answers
729 views

fizz buzz in TMP

The Fizz Buzz problem is a very basic problem to solve that is used by some to weed out interviewees that don't know how to program. The problem is: Set N = [0,100] Set F = x in N where x % 3 == 0 ...