Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying The Modern C++ Challenge
  • Table Of Contents Toc
  • Feedback & Rating feedback
The Modern C++ Challenge

The Modern C++ Challenge

4 (7)
close
close
The Modern C++ Challenge

The Modern C++ Challenge

4 (7)

Overview of this book

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?
Table of Contents (20 chapters)
close
close
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Free Chapter
1
Math Problems
2
Language Features
3
Strings and Regular Expressions
4
Streams and Filesystems
chevron up
5
Date and Time
6
Algorithms and Data Structures
8
Design Patterns
9
Data Serialization
10
Archives, Images, and Databases
11
Cryptography
12
Networking and Services
Index

Solutions


32. Pascal's triangle

Pascal's triangle is a construction representing binomial coefficients. The triangle starts with a row that has a single value of 1. Elements of each row are constructed by summing the numbers above, to the left and right, and treating blank entries as 0. Here is an example of the triangle with five rows:

        1
      1   1
    1   2   1
  1   3   3   1
1   4   6   4   1

To print the triangle, we must:

  • Shift the output position to the right with an appropriate number of spaces, so that the top is projected on the middle of the triangle's base.
  • Compute each value by summing the above left and right values. A simpler formula is that for a row i and column j, each new value x is equal to the previous value of x multiplied by (i - j) / (j + 1), where x starts at 1.

The following is a possible implementation of a function that prints the triangle:

unsigned int number_of_digits(unsigned int const i)
{
   return i > 0 ? (int)log10((double)i) + 1 : 1;
}

void print_pascal_triangle...

Limited Time Offer

$10p/m for 3 months

Get online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech and supported with AI assistants

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY