The Modern C++ Challenge - Marius Bancila - E-Book

The Modern C++ Challenge E-Book

Marius Bancila

0,0
27,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Test your C++ programming skills by solving real-world programming problems covered in the book

Key Features

  • Solve a variety of real-world programming and logic problems by leveraging the power of C++17
  • Test your skills in using language features, algorithms, data structures, design patterns, and more
  • Explore areas such as cryptography, communication, and image handling in C++

Book Description

C++ is one of the most widely-used programming languages and has applications in a variety of fields, such as gaming, GUI programming, and operating systems, to name a few. Through the years, C++ has evolved into (and remains) one of the top choices for software developers worldwide. This book will show you some notable C++ features and how to implement them to meet your application needs. Each problem is unique and doesn't just test your knowledge of the language; it tests your ability to think out of the box and come up with the best solutions. With varying levels of difficulty, you'll be faced with a wide variety of challenges. And in case you're stumped, you don't have to worry: we've got the best solutions to the problems in the book. So are you up for the challenge?

What you will learn

  • Serialize and deserialize JSON and XML data
  • Perform encryption and signing to facilitate secure communication between parties
  • Embed and use SQLite databases in your applications
  • Use threads and asynchronous functions to implement generic purpose parallel algorithms
  • Compress and decompress files to/from a ZIP archive
  • Implement data structures such as circular buffer and priority queue
  • Implement general purpose algorithms as well as algorithms that solve specific problems
  • Create client-server applications that communicate over TCP/IP
  • Consume HTTP REST services
  • Use design patterns to solve real-world problems

Who this book is for

This book will appeal to C++ developers of all levels. There's a challenge inside for everyone.

Marius Bancila is a software engineer with 15 years of experience in developing solutions for the industrial and financial sectors. He is the author of Modern C++ Programming Cookbook. He focuses on Microsoft technologies and mainly develops desktop applications with C++ and C#. He is passionate about sharing his technical expertise with others, and for that reason, he was recognized as a Microsoft MVP for more than a decade. He can be contacted on Twitter at @mariusbancila.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 293

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



The Modern C++ Challenge

 

 

 

 

 

 

 

Become an expert programmer by solving real-world problems

 

 

 

 

 

 

 

 

 

Marius Bancila

 

 

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

The Modern C++ Challenge

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Aaron LazarAcquisition Editors: Nitin Dasan, Chaitanya NairContent Development Editor: Nikhil BorkarTechnical Editor: Jijo MaliyekalCopy Editor: Safis EditingProject Coordinator: Ulhas KambaliProofreader: Safis EditingIndexer: Mariammal ChettiyarGraphics: Tania DuttaProduction Coordinator: Shantanu Zagade

First published: May 2018

Production reference: 1210518

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78899-386-9

www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the author

Marius Bancila is a software engineer with 15 years of experience in developing solutions for the industrial and financial sectors. He is the author of Modern C++ Programming Cookbook. He focuses on Microsoft technologies and mainly develops desktop applications with C++ and C#.

He is passionate about sharing his technical expertise with others, and for that reason, he was recognized as a Microsoft MVP for more than a decade. He can be contacted on Twitter at @mariusbancila.

I would like to thank Nikhil Borkar, Jijo Maliyekal, Chaitanya Nair, Nitin Dasan, and all the other people at Packt who contributed to this book. I would also like to thank the reviewers who provided great feedback and steered the book in a better direction. Finally, a special thanks to my wife and family, who supported me to work on this project.

About the reviewers

Aivars Kalvāns is the lead software architect at Tieto Latvia. He has been working on a Card Suite payment card system for more than 16 years and maintains many of core C++ libraries and programs. He is also responsible for C++ programming guidelines, secure coding training, and code reviews. He organizes and speaks at internal C++ developer meetups.

I would like to thank my lovely wife, Anete, and sons, Kārlis, Gustavs, and Leo, for making life much more interesting.

 

 

Arun Muralidharan is a software developer with over 8 years of experience as a systems and full-stack developer. Distributed system design, architecture, event systems, scalability, performance, and programming languages are some of the aspects of a product that interest him the most.

He is an ardent fan of C++ and its template metaprogramming; he likes how the language keeps his ego in check. So, one would find him working on C++ most of the time.

I would like to take this moment to thank the C++ community, from whom I have learned a lot over the years.

 

 

Nibedit Dey is a technopreneur with a multidisciplinary technology background. He has a bachelor's in biomedical engineering and a master’s in digital design and embedded systems. Before starting his entrepreneurial journey, he worked for L&T and Tektronix for several years in different R&D roles. He has been using C++ to build complex software-based systems for the last 8 years.

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

The Modern C++ Challenge

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the author

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Building the code

How to generate projects for Visual Studio 2017

How to generate projects for Xcode

Conventions used

Get in touch

Reviews

Math Problems

Problems

1. Sum of naturals divisible by 3 and 5

2. Greatest common divisor

3. Least common multiple

4. Largest prime smaller than given number

5. Sexy prime pairs

6. Abundant numbers

7. Amicable numbers

8. Armstrong numbers

9. Prime factors of a number

10. Gray code

11. Converting numerical values to Roman

12. Largest Collatz sequence

13. Computing the value of Pi

14. Validating ISBNs

Solutions

1. Sum of naturals divisible by 3 and 5

2. Greatest common divisor

3. Least common multiple

4. Largest prime smaller than given number

5. Sexy prime pairs

6. Abundant numbers

7. Amicable numbers

8. Armstrong numbers

9. Prime factors of a number

10. Gray code

11. Converting numerical values to Roman

12. Largest Collatz sequence

13. Computing the value of Pi

14. Validating ISBNs

Language Features

Problems

15. IPv4 data type

16. Enumerating IPv4 addresses in a range

17. Creating a 2D array with basic operations

18. Minimum function with any number of arguments

19. Adding a range of values to a container

20. Container any, all, none

21. System handle wrapper

22. Literals of various temperature scales

Solutions

15. IPv4 data type

16. Enumerating IPv4 addresses in a range

17. Creating a 2D array with basic operations

18. Minimum function with any number of arguments

19. Adding a range of values to a container

20. Container any, all, none

21. System handle wrapper

22. Literals of various temperature scales

Strings and Regular Expressions

Problems

23. Binary to string conversion

24. String to binary conversion

25. Capitalizing an article title

26. Joining strings together separated by a delimiter

27. Splitting a string into tokens with a list of possible delimiters

28. Longest palindromic substring

29. License plate validation

30. Extracting URL parts

31. Transforming dates in strings

Solutions

23. Binary to string conversion

24. String to binary conversion

25. Capitalizing an article title

26. Joining strings together separated by a delimiter

27. Splitting a string into tokens with a list of possible delimiters

28. Longest palindromic substring

29. License plate validation

30. Extracting URL parts

31. Transforming dates in strings

Streams and Filesystems

Problems

32. Pascal's triangle

33. Tabular printing of a list of processes

34. Removing empty lines from a text file

35. Computing the size of a directory

36. Deleting files older than a given date

37. Finding files in a directory that match a regular expression

38. Temporary log files

Solutions

32. Pascal's triangle

33. Tabular printing of a list of processes

34. Removing empty lines from a text file

35. Computing the size of a directory

36. Deleting files older than a given date

37. Finding files in a directory that match a regular expression

38. Temporary log files

Date and Time

Problems

39. Measuring function execution time

40. Number of days between two dates

41. Day of the week

42. Day and week of the year

43. Meeting time for multiple time zones

44. Monthly calendar

Solutions

39. Measuring function execution time

40. Number of days between two dates

41. Day of the week

42. Day and week of the year

43. Meeting time for multiple time zones

44. Monthly calendar

Algorithms and Data Structures

Problems

45. Priority queue

46. Circular buffer

47. Double buffer

48. The most frequent element in a range

49. Text histogram

50. Filtering a list of phone numbers

51. Transforming a list of phone numbers

52. Generating all the permutations of a string

53. Average rating of movies

54. Pairwise algorithm

55. Zip algorithm

56. Select algorithm

57. Sort algorithm

58. The shortest path between nodes

59. The Weasel program

60. The Game of Life

Solutions

45. Priority queue

46. Circular buffer

47. Double buffer

48. The most frequent element in a range

49. Text histogram

50. Filtering a list of phone numbers

51. Transforming a list of phone numbers

52. Generating all the permutations of a string

53. Average rating of movies

54. Pairwise algorithm

55. Zip algorithm

56. Select algorithm

57. Sort algorithm

58. The shortest path between nodes

59. The Weasel program

60. The Game of Life

Concurrency

Problems

61. Parallel transform algorithm

62. Parallel min and max element algorithms using threads

63. Parallel min and max element algorithms using asynchronous functions

64. Parallel sort algorithm

65. Thread-safe logging to the console

66. Customer service system

Solutions

61. Parallel transform algorithm

62. Parallel min and max element algorithms using threads

63. Parallel min and max element algorithms using asynchronous functions

64. Parallel sort algorithm

65. Thread-safe logging to the console

66. Customer service system

Design Patterns

Problems

67. Validating passwords

68. Generating random passwords

69. Generating social security numbers

70. Approval system

71. Observable vector container

72. Computing order price with discounts

Solutions

67. Validating passwords

68. Generating random passwords

69. Generating social security numbers

70. Approval system

71. Observable vector container

72. Computing order price with discounts

Data Serialization

Problems

73. Serializing and deserializing data to/from XML

74. Selecting data from XML using XPath

75. Serializing data to JSON

76. Deserializing data from JSON

77. Printing a list of movies to a PDF

78. Creating a PDF from a collection of images

Solutions

73. Serializing and deserializing data to/from XML

74. Selecting data from XML using XPath

75. Serializing data to JSON

76. Deserializing data from JSON

77. Printing a list of movies to a PDF

78. Creating a PDF from a collection of images

Archives, Images, and Databases

Problems

79. Finding files in a ZIP archive

80. Compressing and decompressing files to/from a ZIP archive

81. Compressing and decompressing files to/from a ZIP archive with a password

82. Creating a PNG that represents a national flag

83. Creating verification text PNG images

84. EAN-13 barcode generator

85. Reading movies from an SQLite database

86. Inserting movies into an SQLite database transactionally

87. Handling movie images in an SQLite database

Solutions

79. Finding files in a ZIP archive

80. Compressing and decompressing files to/from a ZIP archive

81. Compressing and decompressing files to/from a ZIP archive with password

82. Creating a PNG that represents a national flag

83. Creating verification text PNG images

84. EAN-13 barcode generator

85. Reading movies from an SQLite database

86. Inserting movies into an SQLite database

87. Handling movie images in an SQLite database

Cryptography

Problems

88. Caesar cipher

89. Vigenère cipher

90. Base64 encoding and decoding

91. Validating user credentials

92. Computing file hashes

93. Encrypting and decrypting files

94. File signing

Solutions

88. Caesar cipher

89. Vigenère cipher

90. Base64 encoding and decoding

91. Validating user credentials

92. Computing file hashes

93. Encrypting and decrypting files

94. File signing

Networking and Services

Problems

95. Finding the IP address of a host

96. Client-server Fizz-Buzz

97. Bitcoin exchange rates

98. Fetching emails using IMAP

99. Translating text to any language

100. Detecting faces in a picture

Solutions

95. Finding the IP address of a host

96. Client-server Fizz-Buzz

97. Bitcoin exchange rates

98. Fetching emails using IMAP

99. Translating text to any language

100. Detecting faces in a picture

Bibliography

Articles

Library documentation

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

C++ is a general-purpose programming language that combines different paradigms such as object-oriented, imperative, generic, and functional programming. C++ is designed for efficiency and is the primary choice in applications where performance is key. Over the last few decades, C++ has been one of the most widely used programming languages in industry, academia, and elsewhere. The language is standardized by the International Organization for Standardization (ISO), which is currently working on the next version of the standard, called C++20, due to be completed in 2020.

With the standard covering almost 1500 pages, C++ is not the simplest language to learn and master. Skills are not acquired only by reading about them or watching others exercising them, but by practicing them again and again. Programming is no different; we developers do not learn new languages or technologies just by reading books, articles, or watching video tutorials. Instead, we need practice to sediment and develop the new things we learn so that we can eventually master them. Many a times, however, finding good exercises to put our knowledge to test is a difficult task. Although there are many websites that feature problems for different programming languages, most of these are mathematical problems, algorithms, or problems for student competitions. These kinds of problems do not help you exercise a large variety of a programming language functionalities. That is where this book steps in.

This book is a collection of 100 real-world problems designed for you to practice a large variety of the C++ language and standard library features as well as many third-party, cross-platform libraries. Yet, a few of these problems are C++ specific and, in general, can be solved in many programming languages. Of course, the intention is to help you master C++ and therefore you are expected to solve them in C++. All the solutions provided in the book are in C++. However, you can use the book as a reference for its collection of proposed problems when you learn other programming languages, although in this case, you will not benefit from the solutions.

The problems in this book are grouped into 12 chapters. Each chapter contains problems on similar or related topics. The problems have different levels of difficulty; some of them are easy, some are moderate, and some are difficult. The book has a relatively equal number of problems for each difficulty level. Each chapter starts with the description of the proposed problems. The solutions to these problems ensue with recommendations, explanations, and source code. Although you can find the solutions in the book, it is recommended that you try to implement them by yourself first, and only afterward—or if you have difficulties completing them—look at the proposed solutions. There is only one thing that is missing in the source code presented in the book—the headers you have to include. This was left out on purpose so that you figure those out by yourself. On the other hand, the source code provided with the book is complete, and you can find all the required headers there.

At the time of writing this book, the C++20 version of the standard is in progress and will continue for the next couple of years. However, some features have already been voted in, and one of these features is the extension to the chrono library with calendars and time zones. There are several problems in the fifth chapter on this topic, and although no compiler supports these yet, you can solve them using the date library, based on which the new standard additions have been designed. Many other libraries are used for solving problems in the book. The list includes Asio, Crypto++, Curl, NLohmann/json, PDF-Writer, PNGWriter, pugixml, SQLite, and ZipLib. Also, as an alternative to the std::optional and the filesystem libraries used throughout the book, you can use Boost with compilers where these are not available. All these libraries are open source and cross-platform. They were chosen for reasons that include performance, good documentation, and wide use within the community. However, you are free to use any other libraries you would like to solve the problems.

Who this book is for

Are you trying to learn C++ and are looking for challenges to practice what you're learning? If so, this book is for you. The book is intended for people learning C++, regardless of their experience with other programming languages, as a valuable resource of practical exercises and real-world problems. This book does not teach you the features of the language or the standard library. You are expected to learn that from other resources, such as books, articles, or video tutorials. This book is a learning companion and challenges you to solve tasks of various difficulties, utilizing the skills you have previously learned from other resources. Nevertheless, many of the problems proposed in this book are language agnostic, and you can use them when learning other programming languages; however, in this case, you won't be benefiting from the solutions provided here.

What this book covers

Chapter 1, Math Problems, contains a series of math exercises to warm you up for the more challenging problems in the next chapters.

Chapter 2, Language Features, proposes problems for you to practice operator overloading, move semantics, user-defined literals, and template metaprogramming aspects such as variadic functions, fold expressions, and type traits.

Chapter 3, Strings and Regular Expressions, has several problems for string manipulation, such as converting between strings and other data types, splitting and joining strings, and also for working with regular expressions.

Chapter 4, Streams and Filesystems, covers output stream manipulation and working with files and directories using the C++17 filesystem library.

Chapter 5, Date and Time, prepares you for the upcoming C++20 extensions to the chrono library, with several calendar and time zone problems that you can solve with the date library, on which the new standard additions are based.

Chapter 6, Algorithms and Data Structures, is one of the largest chapters and contains a variety of problems where you need to utilize the existing standard algorithms; others are where you need to implement your own general-purpose algorithms or data structures, such as circular buffer and priority queue. The chapter ends with two rather fun problems, Dawkins' Weasel program and Conway's Game of Life program, where you can learn about evolutionary algorithms and cellular automata.

Chapter 7, Concurrency, is where we use threads and asynchronous functions to implement general-purpose parallel algorithms, but also solve some real-word problems involving concurrency.

Chapter 8, Design Patterns, proposes a series of problems suited to be solved with design patterns such as decorator, composite, chain of responsibility, template method, and others.

Chapter 9, Data Serialization, covers most common formats of serialized data, JSON, and XML, with several problems; but it also challenges you to create PDF files, all with the use of third-party, open-source, and cross-platform libraries.

Chapter 10, Archives, Images, and Databases, teaches you to solve problems for working with zip archives, creating PNG files for real-world problems, such as Captcha-like systems and barcodes, and embedding and utilizing SQLite databases in your applications.

Chapter 11, Cryptography, mostly covers the user of the Crypto++ library for data encryption and signing. It also challenges you to implement your own Base64 encoding and decoding utilities.

Chapter 12, Networking and Services, is where you have to implement your own client-server application communicating on TCP/IP, and also consume various REST services such as bitcoin exchange rates or text translation APIs.

To get the most out of this book

As previously mentioned, you need a basic familiarity with the C++ language and the standard library in order to be able to utilize this book, or you can learn that along the way. In any case, this book will teach you how to solve problems, but it will not teach you about the language and features utilized in the solutions. You will need a compiler with C++17 support; a complete list of required libraries as well as possible compilers you can use can be found in the Software Hardware List available in the code bundle. In the following sections, you will find detailed instructions for downloading and building the code from this book.

Download the example code files

You can download the code files with the solutions to the problems in this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/The-Modern-Cpp-Challenge. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Building the code

Although a large number of 3rd party libraries are used throughout the book, all these libraries, as well as all the solutions provided in the book are cross-platform and run on all platforms. However, the code has been developed and tested with Visual Studio 2017 v15.6/7 on Windows 10 and Xcode 9.3 on Mac OS 10.13.x.

If you are using Xcode on a Mac, there are two features used in the book that are not available with the LLVM toolset included in Xcode; these are the filesystem library and std::optional. However, these have been designed based on the Boost.Filesystem and Boost.Optional libraries and the use of the mentioned standard libraries in the proposed solutions is easily interchangeable with the Boost libraries. In fact, the accompanying code is written so that it works with either of the two; controlling which one to use is done with several macros. Instructions for building either with one or another are provided below, although the same information is also available in the source archive.

In order to support most of the development environments and build systems you could use on various platforms, the code is provided with CMake scripts. These are used to generate projects or build scripts for your preferred toolset. If you do not have CMake installed on your machine, you can get it from https://cmake.org/. Below, you can find instructions for using CMake to generate Visual Studio and Xcode scripts. For other tools, please refer to the CMake documentation, if necessary.

How to generate projects for Visual Studio 2017

Do the following in order to generate Visual Studio 2017 projects to target the x86 platform:

Open a command prompt and go to the

build

directory in the source code root folder.

Execute the following CMake command:

cmake -G "Visual Studio 15 2017" .. -DCMAKE_USE_WINSSL=ON -DCURL_WINDOWS_SSPI=ON -DCURL_LIBRARY=libcurl -DCURL_INCLUDE_DIR=..\libs\curl\include -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF

After completion, the Visual Studio solution can be found at 

build/cppchallenger.sln

.

If you want to target the x64 platform instead, use the generator called "Visual Studio 15 2017 Win64". Visual Studio 2017 15.4 supports both filesystem (as an experimental library) and std::optional. If you use a previous version, or just want to use the Boost libraries instead, you can generate the projects using the following command, after you properly install Boost:

cmake -G "Visual Studio 15 2017" .. -DCMAKE_USE_WINSSL=ON -DCURL_WINDOWS_SSPI=ON -DCURL_LIBRARY=libcurl -DCURL_INCLUDE_DIR=..\libs\curl\include -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF -DBOOST_FILESYSTEM=ON -DBOOST_OPTIONAL=ON -DBOOST_INCLUDE_DIR=<path_to_headers> -DBOOST_LIB_DIR=<path_to_libs>

Make sure that the paths to the headers and static library files do not include trailing backslashes (i.e. \).

How to generate projects for Xcode

Several solutions in the last chapter utilize the libcurl library. For SSL support, this library needs to be linked with the OpenSSL library. Do the following to install OpenSSL:

Download the library from

https://www.openssl.org/

.

Unzip the archive and, in a terminal, go to its root directory.

Build and install the library with the following commands (executed in this order):

./Configure darwin64-x86_64-cc shared enable-ec_nistp_64_gcc_128 no-ssl2 no-ssl3 no-comp --openssldir=/usr/local/ssl/macos-x86_64

make depend

sudo make install

Until std::optional and the filesystem library will be available with Xcode's Clang, you need to use Boost. Do the following to install and build the Boost libraries:

Install Homebrew from

https://brew.sh/

.

Run the following command to download and install Boost automatically.

brew install boost

After installation, the Boost library will be available at

/usr/local/Cellar/boost/1.65.0

.

In order to generate projects for Xcode from the sources you have to:

Open a terminal and go to the

 

build

 

directory in the source code root directory.

Execute the following CMake command:

cmake -G Xcode .. -DOPENSSL_ROOT_DIR=/usr/local/bin -DOPENSSL_INCLUDE_DIR=/usr/local/include/ -DBUILD_TESTING=OFF -DBUILD_CURL_EXE=OFF -DUSE_MANUAL=OFF -DBOOST_FILESYSTEM=ON -DBOOST_OPTIONAL=ON -DBOOST_INCLUDE_DIR=/usr/local/Cellar/boost/1.65.0 -DBOOST_LIB_DIR=/usr/local/Cellar/boost/1.65.0/lib

After completion, the Xcode project can be found at 

build/cppchallenger.xcodeproj

.

 

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in the text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

int main(){ std::cout << "Hello, World!\n";}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

template<typename C, typename... Args>void push_back(C& c, Args&&... args){

(c.push_back(args), ...);

}

Any command-line input or output is written as follows:

$ mkdir build

$ cd build

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Math Problems

Problems

1. Sum of naturals divisible by 3 and 5

Write a program that calculates and prints the sum of all the natural numbers divisible by either 3 or 5, up to a given limit entered by the user.

2. Greatest common divisor

Write a program that, given two positive integers, will calculate and print the greatest common divisor of the two.

3. Least common multiple

Write a program that will, given two or more positive integers, calculate and print the least common multiple of them all.

4. Largest prime smaller than given number

Write a program that computes and prints the largest prime number that is smaller than a number provided by the user, which must be a positive integer.

5. Sexy prime pairs

Write a program that prints all the sexy prime pairs up to a limit entered by the user.

6. Abundant numbers

Write a program that prints all abundant numbers and their abundance, up to a number entered by the user.

7. Amicable numbers

Write a program that prints the list of all pairs of amicable numbers smaller than 1,000,000. 

8. Armstrong numbers

Write a program that prints all Armstrong numbers with three digits.

9. Prime factors of a number

Write a program that prints the prime factors of a number entered by the user.

10. Gray code

Write a program that displays the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit numbers.

11. Converting numerical values to Roman

Write a program that, given a number entered by the user, prints its Roman numeral equivalent. 

12. Largest Collatz sequence

Write a program that determines and prints which number up to 1 million produces the longest Collatz sequence and what its length is.

13. Computing the value of Pi

Write a program that computes the value of Pi with a precision of two decimal digits.

14. Validating ISBNs

Write a program that validates that 10-digit values entered by the user, as a string, represent valid ISBN-10 numbers.

Solutions