Task 3: [Hidden tests] Trapezoid printing
Task
In this exercise, you are going to print trapezoids. A trapezoid is defined by the width of its base and its height. A trapezoid can also either be rightsideup or upsidedown.
Note: It is mandatory to use recursive implementation for this task.
Examples
Rightsideup trapezoid with height and width \(3\) (aka a triangle)
0
0 0
0 0 0
Upsidedown trapezoid with height and width \(3\) (aka an upsidedown triangle)
0 0 0
0 0
0
Rightsideup trapezoid with base width 5 and height 2
0 0 0 0
0 0 0 0 0
Subtask 1: print_trapezoid
Implement the function print_trapezoid. The function takes three arguments:
int base_width
: The width of the trapezoid's baseint height
: The height of the trapezoidint offset_left
: The number of spaces between the left edge of the trapezoid's base and the left edge of the console.bool upsidedown
: Boolean indicating whether or not the trapezoid is upsidedown or rightsideup.
When called, the function should print the specified trapezoid to the console.
Subtask 2: print_diamond:
Implement the function print_diamond
.
The function prints a diamond with a given center width to the console. A diamond is a rightsideup trapezoid on top of an upsidedown trapezoid.
For example, a diamond of width \(5\) looks as follows:
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0
0 0 0
0 0
0
Subtask 3: print_hourglass:
Implement the function print_hourglass
.
The function prints an hourglass with a given base width to the console. A hourglass is an upsidedown trapezoid on top of a rightsideup trapezoid.
For example, an hourglass of width \(5\) looks as follows:
0 0 0 0 0
0 0 0 0
0 0 0
0 0
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
Note: The autograder will only accept output that exactly matches the
expected result. Therefore, do not output anything with std::cout beyond
what is strictly required (i.e., ” ","0
", and std::endl
/"n=“). For
debugging messages, use =std::cerr
as in:
std::cerr << "This is a test message." << std::endl;
Those will be ignored by the autograder.
Solution
#include <assert.h>
#include <iostream>
#include "solution.h"
void print_row(int n_bricks, int offset_left) {
// print spaces for offset
for (int i = 0; i < offset_left; ++i) {
std::cout << " ";
}
// print bricks
for (int i = 0; i < n_bricks; ++i) {
if (i != 0) {
std::cout << " ";
}
std::cout << "0";
}
std::cout << std::endl;
}
void print_trapezoid(int base_width, int height, int offset_left,
bool upsidedown) {
assert(base_width >= height);
if (upsidedown == true) {
if (height == 0)
return;
print_row(base_width, offset_left);
print_trapezoid(base_width - 1, height - 1, offset_left + 1, upsidedown);
} else if (upsidedown == false) {
if (height == 0)
return;
print_row(base_width - height + 1, offset_left + height - 1);
print_trapezoid(base_width, height - 1, offset_left, upsidedown);
}
}
void print_diamond(int center_width) {
print_trapezoid(center_width, center_width, 0, false);
print_trapezoid(center_width - 1, center_width - 1, 0, true);
}
void print_hourglass(int base_width) {
print_trapezoid(base_width, base_width, 0, true);
print_trapezoid(base_width, base_width - 1, 0, false);
Made by JirR02 in Switzerland 🇨🇭