0% found this document useful (0 votes)
48 views4 pages

Master C++ 11

Uploaded by

Kirito Senpai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

Master C++ 11

Uploaded by

Kirito Senpai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
Learn Modern C++ Discover a language matched to today's computing needs Coding Assignments for each Chapter 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 , anda u8",.." literal to get the euro symbol. Hint: you may need to use << reinterpret_cast(u8"...") or similar to enable output ofa 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 youre using Windows and are your editor has UTF-16 support, experiment with UTE-16 source code and 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. © Adda 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-Jequality tests ( <, <=, >=, > )and outputs only if the test is true, suchas 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, anda default: case to catch out-of- range input. © Write a program which demonstrates the difference between pre- and post-increment ++ onan 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 .Usea 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. (Ihere isa 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 == @) ? b: gcd(b % a, a); } © Modify the first program to only test for primary Triples, where ged(a,b,c) = 1 holds true. Use the fact that gad(a,b,c) = ged(ged(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 builtin 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 © Writean 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 toan enum class making all the necessary modifications. Hint: you will need to overload global operator| , using static_cast(...) © Writea 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 <2 or false otherwise. Hint: pick suitable parameter and return types, and use toupper() from 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: 1. Ifa 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 2. Ifa word starts with two consonants move the two consonants to the end of the word and add “ay.” Example: Child = Ildch + ay = Ildchay 3. Ifa 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 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 itis 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 1s command, under Linux you could test with ./print-words | less , or under Windows just print the A’s. © Write a program whi [-1] command. Hi outputs the total number of words and lines in a text file, after the Unix we : think about continuous sequences of alphabetic characters making up words, and use isalpha(c) from (which returns true if char c isan 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. © Createa 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.55 weird string += ' "5 weird_string hello "; weird string += 42; std::cout << weird_string << '\n'; // outputs "@.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, 2x 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. Blog.at WordPress.com,

You might also like