Chapter 1: String and Character Literals
- Write a program which outputs the cover text of your favorite novel, with title and author centered on your console as well as sale price in dollars and euros. Use multiple literal strings with
std::cout
, and a u8"..."
literal to get the euro symbol. Hint: you may need to use << reinterpret_cast<const char*>(u8"...")
or similar to enable output of a char8_t
literal string.
- Modify this program to use a (single) raw string literal.
- Write an email on your phone with special/accented/math characters and emoji. Open this email on your coding box and try to paste the contents into a C++ program, using a suitable raw string literal. If you’re using Windows and are your editor has UTF-16 support, experiment with UTF-16 source code and
u"..."
or L"..."
literals. Experiment with outputting to your console, and the type of codepage (or locale) necessary to display correctly.
Chapter 2: Variables, Scopes and Namespaces
- Write a program which outputs the minimum and maximum values of all unsigned types as decimal integers. Hint: try to use a shortcut.
- Write a program which outputs the minimum and maximum values of all signed types as decimal integers. Use a mixture of hexadecimal and octal literals, and test the output against the table in this Chapter.
- Write a program which modifies a reference to a global variable in a namespace of type
double
. Output the global variable before and after the modification.
- Add a
using
statement which means that the namespace qualifiers to the global variable are no longer necessary.
Chapter 3: Conditions and Operators
- Write a function that tests two integer input parameters agains the six (in-)equality tests (
==
, !=
, <
, <=
, >=
, >
) and outputs only if the test is true, such as 6 is less than or equal to 6
. Test this function by calling it from main()
with user input.
- Modify the same program to test floating-point inputs. Does it still behave as expected?
- Write a program which uses a switch statement to test a number (input by the user) between 1 and 10 as odd or even. Hint: use only two
std::cout
statements, and a default:
case to catch out-of-range input.
- Write a program which demonstrates the difference between pre- and post-increment
++
on an integer variable whose value is obtained from the user. Hint: output before increment:
, increment:
and after increment:
together with the variable’s name and current value.
Chapter 4: Functions
- Write a function which tests whether three number parameters form a Pythagorean Triple, such as
5
, 12
and 13
. Use a suitable return type, and write a main()
program to test this function.
- Write a function which uses Euclid’s algorithm to find the greatest common divisor (GCD) of two positive integer numbers. (There is a
std::gcd()
in the Standard Library, declared constexpr
, which you could use to test your version.) Hint: if you get stuck, use this C++ code: unsigned gcd(unsigned a, unsigned b) { return (a == 0) ? b : gcd(b % a, a); }
- Modify the first program to only test for primary Triples, where gcd(a,b,c) = 1 holds true. Use the fact that gcd(a,b,c) = gcd(gcd(a,b),c)).
Chapter 5: Arrays, Pointers and Loops
- Write a program which prints all of the multiples of seven (up to 98). Use a for-loop counting from 1 and incrementing by 1, with each iteration of the loop producing output.
- Write another program to accomplish the same task, except using a while-loop incrementing by 1 again, but with each seventh iteration of the loop producing output.
- Write a program which reverses and outputs a built-in
char
array in place. Don’t use user input, but cope with different (odd or even) length words. Hint: only use one array, and use pointer variables, not array indexing.
- Write a function which accepts a
char
pointer-to-array and length paramters, reversing the array in place. Use array indexing and output the reversed string back in the caller function, main()
. Hint: allow variable length user input, and use array indexing.
Chapter 6: Enums and Structs
- Write an
enum Color
which uses bitfields to represent RGB colorspace, where red is bit 0, green is bit 1 and blue is bit 2. Write a function that uses a switch statement to output all eight combinations, eg. cyan
from blue | green
.
- Change this to an
enum class
making all the necessary modifications. Hint: you will need to overload global operator|
, using static_cast<int>(...)
.
- Write a
struct Animal
using public:
data members set by uniform initialization and getters for attributes such as nWings()
, nLimbs()
, canFly()
, canSwim()
. Create instances of Tuna
, Dolphin
, Penguin
, Otter
and Hare
. Consider how well this monolithic design scales to later addition of attributes such as canRun()
, canJump()
for instances of Cheetah
or Kangaroo
, and try to add these.
Chapter 7: Strings, Containers and Views
- Create a function
string_case_less(s1, s2)
which performs case-insensitive comparison returning true
if s1 < s2
or false
otherwise. Hint: pick suitable parameter and return types, and use toupper()
from <cctype>
as demonstrated in this Chapter. Test with words with more than one letter of common prefix.
- Write a program which uses this function, accepting a list of mixed-case user-input strings and sorts them case-insensitively, using
std::sort
with the previous function as the third parameter.
- Write a program which converts an input sentence of English into “pig-Latin”. Most words in Pig Latin end in “ay.” Use the rules below:
- If a word starts with a consonant and a vowel, put the first letter of the word at the end of the word and add “ay.” Example: Happy = Appyh + ay = Appyhay
- If a word starts with two consonants move the two consonants to the end of the word and add “ay.” Example: Child = Ildch + ay = Ildchay
- If a word starts with a vowel add the word “way” at the end of the word. Example: Awesome = Awesome + way = Awesomeway
- Write a program which counts the number of occurrences of each word input, using a
std::map<std::string, size_t>
container. Output the list of words by top number of occurrencies, then alphabetically.
Chapter 8: Files and Formatting
- Write a program which scans for all the palindromes (words which are the same spelt backward, such as “madam”) in a text file. Read the file (such as an English dictionary) line-by-line, and only output the word if it is a palindrome.
- Modify the above program to read the whole file into a suitable container, before counting the number of palindromes and outputting this total.
- Using the same input file, write a program which outputs all of the words in as many columns as will fit on the console, in pages of 50 lines, with a blank line in between. Hint: this should imitate the Unix
ls
command, under Linux you could test with ./print-words | less
, or under Windows just print the A’s.
- Write a program which outputs the total number of words and lines in a text file, after the Unix
wc [-l]
command. Hint: think about continuous sequences of alphabetic characters making up words, and use isalpha(c)
from <cctype>
(which returns true
if char c
is an upper- or lower-case letter, false
otherwise).
Chapter 9: Classes, Friends and Polymorphism
- Rewrite the
Animal
class from before as a class hierarchy, inheriting from various abstract base classes (ABCs). Include derived classes for all the animal instances, plus more if you can.
- Create a
RoadVehicle
base class, with derived classes Bicycle
, Car
, Truck
each with virtual
functions as necessary to provide functionality such as startEngine()
and changeGear(n)
. Report road speed (using another member function) based on rpm and selected gear.
Chapter 10: Templates, Exceptions, Lambdas, Smart Pointers
- Write a generic class
StrAny
that allows initialization from, and concatenation of, any type which has a std::to_string
overload. Thus one could write:
StrAny weird_string = 0.5;
weird_string += ' ';
weird_string += "hello ";
weird_string += 42;
std::cout << weird_string << '\n'; // outputs "0.5 hello 42"
- Write a program which uses exceptions to count the total number of square numbers up to 1000, outputting them as found.
- Write a generic lambda function which can calculate mean, standard deviation and standard sample deviation on input data. Hint: keep a running total of n, Σx and Σx² outside the lambda. Test this lambda function against known data sets using a floating-point type.
- Implement a generic matrix class that uses a
std::unique_ptr
to a built-in array of the element type. Support initialization, element indexing, addition and multiplication at the very least. Try to enable the special functions (“rule of five” copy and assignment) one by one, making sure that the semantics are correct. Hint: you may find a deep-copying clone()
function is useful.
Like this:
Like Loading...