Solution Review: Printing C++
Explore how to print the C++ pattern by using loops and modular functions. Understand calling functions for different segments such as first and last lines, body, and middle to build efficient, reusable code.
We'll cover the following...
Printing C++
Write a program that prints C++:
Look at the tables for each function below:
##The printS1_FirstAndLast() table
printS1_FirstAndLast() function will print the first and last horizontal lines of length x. We need to call this function two times to print the first and last lines of the C++ pattern.
printS1_FirstAndLast()
Dashes(-) | Spaces | Spaces | Dashes(-) | Spaces | Spaces | Spaces | Dashes(-) |
x | x/4 | x/2 | 1 | x/2 | x/4 | x/2 | 1 |
The printS2_Body() table
The printS2_Body()function will print the vertical lines (body segment) of the C++ pattern. We need to call this function x times.
printS2_Body()
Dashes(-) | Spaces | Spaces | Spaces | Dashes(-) | Spaces | Spaces | Spaces | Dashes(-) |
1 | x-1 | x/4 | x/2 | 1 | x/2 | x/4 | x/2 | 1 |
TheprintS3_Middle() table
The printS3_Middle() function will print the middle segment of the C++ pattern.
printS3_Middle()
Dashes(-) | Spaces | Spaces | Dashes(-) | Dashes(-) | Dashes(-) | Spaces | Dashes(-) | Dashes(-) | Dashes(-) |
1 | x-1 | x/4 | x/2 | 1 | x/2 | x/4 | x/2 | 1 | x/2 |
We have called all three functions in PrintCPP().
Solution
Let’s take a look at the solution.