C++ Puzzles

This category contains 75 posts

C++: Create an HTML Table

Create an HTML table. The table body should have at least three rows of three columns. Each of these three columns should be labelled “X”, “Y”, and “Z”. An extra column should be added at either the extreme left or the extreme right of the table that has no heading, but is filled with sequential … Continue reading »

C++: Guess the Number

The task is to write a program where the program chooses a number between 1 and 10. A player is then prompted to enter a guess. If the player guess wrong then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will give a “Well … Continue reading »

C++: Elementary Cellular Automaton

An elementary cellular automaton is a one-dimensional cellular automaton where there are two possible states (labeled 0 and 1) and the rule to determine the state of a cell in the next generation depends only on the current state of the cell and its two immediate neighbors. Those three values can be encoded with three … Continue reading »

C++: Largest Int from Concatenated Ints

Given a set of positive integers, the task is to write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer. Use the following two sets of integers as tests and show your program output here. {1, 34, 3, 98, … Continue reading »

C++: Day of the Week

A company decides that whenever Xmas falls on a Sunday they will give their workers all extra paid holidays so that, together with any public holidays, workers will not have to work the following week (between the 25th of December and the first of January). In what years between 2008 and 2121 will the 25th … Continue reading »

C++: Five Weekends

The month of October in 2010 has five Fridays, five Saturdays, and five Sundays. The task Write a program to show all months that have this same characteristic of five full weekends from the year 1900 through 2100 (Gregorian calendar). Show the number of months with this property (there should be 201). Show at least … Continue reading »

C++: Menu

Given a list containing a number of strings of which one is to be selected and a prompt string, create a function that: Print a textual menu formatted as an index value followed by its corresponding string for each item in the list. Prompt the user to enter a number. Return the string corresponding to … Continue reading »

C++: Long Multiplication

In this task, explicitly implement long multiplication. This is one possible approach to arbitrary-precision integer algebra. For output, display the result of 2^64 * 2^64. The decimal representation of 2^64 is: 18446744073709551616 The output of 2^64 * 2^64 is 2^128, and that is: 340282366920938463463374607431768211456 Version 1 #include <iostream> #include <sstream> //————————————————————————————————– typedef long long bigInt; … Continue reading »

C++: Leap Year

Determine whether a given year is a leap year in the Gregorian calendar. #include <iostream> bool is_leap_year(int year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } int main() { for (auto year : {1900, 1994, 1996, 1997, 2000}) { std::cout << year << … Continue reading »

C++: Anagrams

Two or more words can be composed of the same characters, but in a different order. Using the word list at http://www.puzzlers.org/pub/wordlists/unixdict.txt, find the sets of words that share the same characters that contain the most words in them. #include <iostream> #include <fstream> #include <string> #include <map> #include <vector> #include <algorithm> #include <iterator> int main() { … Continue reading »