As a C++ learner myself I have stumbled across this very compacted (yet effective) self-study book called Accelerated C++, written by Andrew koeing and Barbara Moo (Published in 2000 by Addison-Wesley). For learning sake I’ve decided to have a go at the end-of-chapter exercises and summarise my own solutions in this page. This is currently an on-going process and I expect newly written-up solutions to be posted here from time to time. As the book does not come with a solution section I can only verify my solutions the best I possibly can and may subject to errors. I perform my own verification including test running codes in Code::Block, referring back to the book, and “Google” for C++ references if required!
To C++ learners who are also studying this book: The best way to learn is to have a go at these exercises yourself – only look at these solutions once you have had a go yourself (to avoid missing out on the great learning experience facilitated by the authors of the book). Top tip: this site is optimised for Google Chrome browser. (I have noticed that Internet Explorer may not display codes snippets properly.)
Note: it was only later I realized the author (Andrew Koeing) actually had also published some accelerated C++ on this GitHub Repository. Use this as a secondary (or primary) resource is you need!
Chapter 0 – Getting Started
- Exercise 0-0 – understanding the “Hello World” program in depth
- Exercise 0-1 – understanding expressions, results, and side effects
- Exercise 0-2 – practice special string literal treatments
- Exercise 0-3 – practice special string literal treatments
- Exercise 0-4 – write a program that create “Hello World” program
- Exercise 0-5 – understanding scope
- Exercise 0-6 – understanding scope
- Exercise 0-7 – understanding comment usage
- Exercise 0-8 – understanding comment usage
- Exercise 0-9 – create the shortest valid program
- Exercise 0-10 – practice special string literal treatments
Chapter 1: Working with strings
- Exercise 1-0 – test run chapter programs
- Exercise 1-1 – Understanding string concatenation
- Exercise 1-2 – Understanding string concatenation
- Exercise 1-3 – Understanding scope
- Exercise 1-4 – Understanding scope
- Exercise 1-5 – Understanding scope
- Exercise 1-6 – Explore the “hidden” std::cin buffer operations
Chapter 2: Looping and counting
- Exercise 2-0 – test run chapter programs. Output a framed greeting using conditions and loops.
- Exercise 2-1 – create a framed greeting program with no padding.
- Exercise 2-2 – create a framed greeting program with separate vertical and horizontal padding assignments.
- Exercise 2-3 – create a framed greeting program with interactive user input against vertical and horizontal padding assignments.
- Exercise 2-4 – create a framed greeting program output empty spaces in groups (rather than one-by-one). Included a detail solution strategy and explanation.
- Exercise 2-5 – create a program that allows user to dynamically generate solid/hollow square/rectangle/triangle using algorithms, and display the shapes via the standard console output window. In-depth solution strategy included. This is a fun one!
- Exercise 2-6 – explain a while loop program.
- Exercise 2-7 – create an intelligent program to carry out number counting and reporting, with in-depth explanation of solution strategy.
- Exercise 2-8 – create an intelligent program to compute the product of numbers within the asymmetrical range [m,n), including very in-depth explanations and drill downs of solution strategy. This is essentially a program to compute factorial of a number.
- Exercise 2-9 – create a very simple program to compare two numbers using the if / else condition check.
- Exercise 2-10 – in-depth explanation of the use of std::, including a revisit of the fundamentals learnt in chapter 0 (e.g. namespace, names, standard library, iostream header).
Chapter 3: Working with batches of data
- Exercise 3-0 – test run the chapter 3 programs. How to read in a vector, sort vector, compute mean and median.
- Exercise 3-1 – perform a numeric test to show that we cannot afford discarding any elements should we wish to compute the median accurately.
- Exercise 3-2 – create a program to compute the quartiles given a dataset. Included comprehensive algorithm derivation, pattern recognition, numerical examples, solution strategy, the full program, and test results. This is a big one!
- Exercise 3-3 – create a program to read in a list of (string) words, and compute the number of distinct words.
- Exercise 3-4 – create a program to read in a list of (string) words, and compute the length of the shortest and longest words.
- Exercise 3-5 – create a program to keep two vectors in sync by using appropriate looping systems and logics.
- Exercise 3-6 – enhance the program to avoid the division by zero scenario.
Chapter 4: Organising programs and data
- Exercise 4-0 – an in-depth walk through and testing of the partitioned program (or so called Project) made up of multiple C++ source files and header files, with the objective of reading in flat-file like input stream, and produce a summarised output report via the output stream. i.e. somewhat related to Extract-Transform-Load (ETL) which is extremely important in the data analytic industry.
- Exercise 4-1 – explore what happens when submitting arguments of different types to the max function, from both conceptual and practical point of views. And correct code accordingly.
- Exercise 4-2 – use the setw to create fix-width column output.
- Exercise 4-3 – create an intelligent program to output two fixed width columns (numbers and square of numbers). Automatically obtain and implement suitable column widths, using a mixed of standard setw and in-house built functions.
- Exercise 4-4 – enhance the solution to Exercise 4-3 to take on double values instead of int. A mix of std::setw and std::setprecision are used, in conjunction with new algorithms to make the dynamic computation possible.
- Exercise 4-5 – reuse solutions to exercises 3-3 and 4-0 to write functions that read words from input stream, store them in a vector, count words and distinct-words from that vector. Solidify understanding in writing functions.
- Exercise 4-6 – rewrite the program in Exercise 4-0, to “upgrade” Student_info object structure changes as required. This exercise demonstrates some fundamentals of Object Orientated Programming (OOP), and provided some insight / experience in changing other people’s codes.
- Exercise 4-7 – create a function to compute and report the average of a set of numbers stored in a vector.
- Exercise 4-8 – revisit the definition of a function’s return type.
Chapter 5: Using sequential containers and analysing strings
- Exercise 5-0 – contain 3 sub exercises and solutions. Part 1/3: extend the student grading program in Chapter 4 to include a capability that split the set of students into passing and failing groups. Part 2/3: split an input line of text into words. Part 3/3: vertical-concatenate and horizontal-concatenate paragraph “pictures” together.
- Exercise 5-1 – what a challenge! Create a C++ program to automatically construct and display a Permuted Index Page (a.k.a. Key Word In Content / KWIC Page) when provided with an input “article”. I’ve deployed a (more or less) Object Oriented Programming (OOP) approach in the solving of this very interesting problem.
- Exercise 5-2 – compare the performance of using vector and list against the Chapter 5 student grading program.
- Exercise 5-3 – generalise / re-engineer the Student Grading program to enable ease of switching between vector-based and list-based input/output. The #include and typdef are used to enable this simple switch (Just 2 line code change for the switch!).
- Exercise 5-4 – generalise / re-engineer the already re-engineered Student Grading program in Exercise 5-3 further to enable even more flexible switching between vector-based and list-based input/output. The #include, typdef, and a new header file are used to enable this even simpler switch (Just 1 line code change for the switch this time!).
- Exercise 5-5 – enhance the frame function to enable left-aligning, center-aligning, and right-aligning.
- Exercise 5-6 – rewrite the (version 2) extract_fail function so that it uses the insert/resize technique instead of the erase technique for filtering for passing students. In-depth code logic walkthrough is included along with performance test comparisons between the “before” and “after” codes.
- Exercise 5-7 – a fundamental revisit of the frame and width functions (as described in Chapter 5)
- Exercise 5-8 – vary the hcat function. Demonstrate the program crashes with string length error should we not re-initialise correctly. Fix the code by incorporating re-initialisation.
- Exercise 5-9 – create a program to process a list of words and split into two portions. Portion 1 will contain words with all lower-case letters. Portion 2 will contain words with some upper-case letters.
- Exercise 5-10 – create a program to identify palindromes and the longest palindrome (if any), given a handful of dictionary words. A palindrome word read the same in both directions (left-to-right and right-to-left).
- Exercise 5-11 – create a program to determine whether the the given words contain either ascenders or descenders (likewise, whether the words contain neither ascenders nor descenders). The program is extended to resolve the longest word with neither ascenders nor descenders.
Chapter 6: Using library algorithms
- Exercise 6-0 – implement and test all chapter 6 programs. There are 7 core concepts / parts in total. (1) Append elements with 3 known ways, (2) alternative way to split a line into words, (3) a very compact function to detect palindromes, (4) the Find URLs program, (5) the Compare Grading Scheme program, (6) a two-pass extract_fails function, and finally (7) a single-pass extract_fails function.
- Exercise 6-1 – reimplement the frame and hcat operations to using iterator for accessing vector elements. Contains a summary diagram to show the similarity between accessing vector elements via index and iterator.
- Exercise 6-2 – write a program to test the find_urls function.
- Exercise 6-3 – demonstrate using the copy function without growing the base vector size dynamically can cause system to crash!
- Exercise 6-4 – demonstrate using copy with back_inserter or inserter enables the base vector to grow dynamically and avoid system crash (as discovered in previous exercise 6-3).
- Exercise 6-5 – write an analysis function to call optimistic_median.
- Exercise 6-6 – to merge three very similar analysis functions into one consolidated (and more generic) version. A good exercise to appreciate the difference between parsing a function as an argument to another function versus direct usage. Also good to visualise how to define a function within an implementation parameter list. Function Arguments vs Function Parameters.
- Exercise 6-7 – re-use the (stable_partition and erase) concepts learned in the one-pass extract_fail function and build up a similar extractDidnt function based on such concepts.
- Exercise 6-8 – create a single (more generic) extractOnCriteria function that is capable to parse predicate such as fgrade (to extract failed students) and did_all_hw (to extract students who did all their homeworks). The idea is to generalise and minimise number of source codes to maintain in long run.
- Exercise 6-9 – find a library algorithm to concatenate all the string elements within a vector
Chapter 7: Using associative containers
- Exercise 7-0 – implement and test all chapter 7 programs. There are 3 core concepts / parts in total – all uses associative arrays / maps: (1) Counting words, (2) Finding the line location of the words, (3) use random generator to create random sentences given a set of predefined Grammar Rules.
- Exercise 7-1 – apply associative arrays to extend the “word counting” program to display words grouped by frequency. A good exercise to gain confidence using the map associative array.
- Exercise 7-2 – extend the student grading program that (1) translate numeric grades to letter grades, and (2) create a letter grade summary that counts the number of students in each letter grade category. Associative array (map) is used in this exercise.
- Exercise 7-3 – adjust the cross-reference program to show only non-duplicated line numbers for the corresponding associative arrays.
- Exercise 7-4 – enhance the cross-reference program to control output line length for ease of human interpretation. The std::ostringstream (of the
directive) is used.
- Exercise 7-5 – re-implement the gen_sentence program (as per Exercise 7-0 Part 3 / 3) using list as a data structure in which we build the sentence (as to using vector).
- Exercise 7-6 – re-implement the gen_sentence program (as per Exercise 7-0 Part 3 / 3) so no recursive calls are required.
- Exercise 7-7 – Change the driver for the cross-reference program so that it writes line if there is only one line and lines otherwise. Use a simple if-else logic against the associative array (map) values.
- Exercise 7-8 – Change the cross-reference program to find all the URLs in a file, and write all the lines on which each distinct URL occurs.
- Exercise 7-9 – an in-depth walkthrough of writing (or re-writing) the nrand function – a.k.a. random number generator. This function generates random numbers within the asymmetric range [0, randomOutcomeLimit) with each randomOutcome having an equal probability of getting picked (instead of biased against certain outcomes).
Chapter 8: Writing generic functions
- Exercise 8-0
- Exercise 8-1
- Exercise 8-2
- Exercise 8-3
- Exercise 8-4
- Exercise 8-5
- Exercise 8-6
- Exercise 8-7
- Exercise 8-8
Chapter 9: Defining new types
- Exercise 9-0
- Exercise 9-1
- Exercise 9-2
- Exercise 9-3
- Exercise 9-4
- Exercise 9-5
- Exercise 9-6
- Exercise 9-7
Chapter 10: Managing memory and low-level data structures
- Exercise 10-0
- Exercise 10-1
- Exercise 10-2
- Exercise 10-3
- Exercise 10-4
- Exercise 10-5
- Exercise 10-6
Chapter 11: Defining abstract data types
- Exercise 11-0
- Exercise 11-1
- Exercise 11-2
- Exercise 11-3
- Exercise 11-4
- Exercise 11-5
- Exercise 11-6
- Exercise 11-7
- Exercise 11-8
- Exercise 11-9
Chapter 12: Making class objects act like values
- Exercise 12-0
- Exercise 12-1
- Exercise 12-2
- Exercise 12-3
- Exercise 12-4
- Exercise 12-5
- Exercise 12-6
- Exercise 12-7
- Exercise 12-8
- Exercise 12-9
- Exercise 12-10
- Exercise 12-11
- Exercise 12-12
- Exercise 12-13
- Exercise 12-14
- Exercise 12-15
Chapter 13: Using inheritance and dynamic binding
- Exercise 13-0
- Exercise 13-1
- Exercise 13-2
- Exercise 13-3
- Exercise 13-4
- Exercise 13-5
- Exercise 13-6
- Exercise 13-7
- Exercise 13-8
- Exercise 13-9
Chapter 14: Managing memory (almost) automatically
- Exercise 14-0
- Exercise 14-1
- Exercise 14-2
- Exercise 14-3
- Exercise 14-4
- Exercise 14-5
- Exercise 14-6
Chapter 15: Revisiting character pictures
- Exercise 15-0
- Exercise 15-1
- Exercise 15-2
- Exercise 15-3
- Exercise 15-4
- Exercise 15-5
- Exercise 15-6
Chapter 16: Where do we go from here?
- Exercise 16-0
- Exercise 16-1