Questions tagged [c++]
This challenge is related to the C++ language. Note that challenges that require the answers to be in a specific language are generally discouraged.
32 questions
2
votes
0
answers
261
views
Shortest way to cause a seg fault at 0x9c?
In honour of CrowdStrike's exception at 0x9c, what's the shortest way (character-wise) to cause a segfault by dereferencing address 0x9c?
The best I can come up with is:
...
1
vote
0
answers
176
views
Printing Hello World using fewest characters [duplicate]
I saw a problem which asks you to print "Hello World" using the fewest characters, including spaces. How can I further shorten this code? Thank you in advance!
...
13
votes
7
answers
2k
views
Generate ladder of integers using the least number of unique characters (in C++)
I am new to the sport of code golf. I am trying to generate a ladder of integers using the least number of unique characters in C++.
Let's say we are given an integer 4.
We will generate the ...
-3
votes
2
answers
210
views
find the value at kth position when numbers are sorted lexicographically till n [closed]
Ex :-
Input:
n = 12, k = 5
Output:
ans = 2
Sorted list S: ["1", "10", "11", "12", "2", "3", "4", "5", ...., "9"]
ans = 2
1
vote
1
answer
1k
views
A+B using C++ language
I'm golfing a program to sum two numbers in C++.
Example:
10 3
13
I found out that this problem can be solved in 54 symbols (without spaces and so on) using C++....
7
votes
4
answers
730
views
Let's play POKE!
Poke is a really simple incomplete-information game. It's like "Poker", but simpler. At the start of every round, each player is dealt a hand. To simplify things, a hand is a uniformly distributed ...
74
votes
18
answers
8k
views
The Futuristic Gun Duel
The Background Future
In the year 2017, you and your opponent will face off in a futuristic gun battle where only one may survive. Are you experienced enough to defeat your opponent? Now is the time ...
12
votes
5
answers
1k
views
Add comments to a Python script and make it a bilingual Python/C++ “program”
Given the following Python 3 script:
def greet():
print("Hello, world!")
greet()
Prepend some lines to this text file so that it can be both executed as a ...
1
vote
0
answers
477
views
Useful C++ template metaprograms [closed]
C++ implements a Turing-complete functional programming language which is evaluated at compile time.
Hence, we can use a C++ compiler as an interpreter to run our metaprogram without ever generating ...
12
votes
5
answers
2k
views
Risk, the Warlight Way
Introduction
In this game, players use their armies to fight other players' armies, capture territories, and become the last man standing. Each turn, players receive a base number of armies to use at ...
31
votes
7
answers
3k
views
Write a C / C++ Polyglot [closed]
This challenge's concept is pretty simple. All you have to do is write a program that will compile as both valid C and valid C++! Well, there are some catches. The program must behave differently when ...
14
votes
15
answers
14k
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) ...
11
votes
2
answers
956
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, <...
8
votes
9
answers
3k
views
Shortest C \ C++ function to generate -1, 0 ,1 [closed]
Let a function receive two integers i and j between 0 and ...
5
votes
2
answers
613
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 ...
5
votes
6
answers
2k
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 ...
3
votes
1
answer
861
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# ...
2
votes
1
answer
561
views
Generate numerical permutations with conditions and template metaprogramming [closed]
This, but via template metaprogramming:
template <int n> struct insanity { int[][n] permutations; }
Generate all numbers with n digits (0-9) such that each ...
12
votes
1
answer
809
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 <...
5
votes
4
answers
3k
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 ...
2
votes
3
answers
853
views
m3ph1st0s's programming puzzle 4 (C/C++): “3-way swap” [closed]
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
5
answers
3k
views
m3ph1st0s's programming puzzle 3 (C): “Easy bug” [closed]
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 ...
8
votes
6
answers
2k
views
Shortest undefined behavior sample in C++ [closed]
What's the shortest, well-formed C++ code that exhibits undefined behavior?
-2
votes
2
answers
3k
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 ('n'...
7
votes
7
answers
2k
views
Shortest code that return SIGSEGV among the given languages [duplicate]
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 ...
3
votes
2
answers
736
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?
...
2
votes
2
answers
904
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:
...
17
votes
8
answers
11k
views
Compute the CRC32 table at compile-time [closed]
The reference implementation of CRC32 computes a lookup table at runtime:
...
139
votes
23
answers
137k
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, ...
27
votes
19
answers
9k
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. ...
63
votes
31
answers
20k
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 ...
13
votes
4
answers
1k
views
fizz buzz in TMP [closed]
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:
...