Default Pyramid Function

Pyramid Function

Write a function that prints a pyramid. Each block of the pyramid should consist of []. The function should take an int argument height. If height isn't passed as an argument, it should print a pyramid of 5 height.

Use default parameter values to solve this task. Do not use function overloading.

#include <iostream>

// Pyramid function here

int main()
{
	print_pyramid(3); // 3 line tall pyramid
	print_pyramid(); // 5 line tall pyramid
}
  []
 [][]
[][][]
    []
   [][]
  [][][]
 [][][][]
[][][][][]

Hint

How to make a pyramid of x height:

Answer

#include <iostream>

// NEW CODE START --------------------------------------
void print_pyramid(int height = 5) {

	for (int y = 0; y < height; y++)
	{
		int box_count = y + 1;
		int space_count = height - box_count;

		for (int x = 0; x < space_count; x++)
			std::cout << " ";

		for (int x = 0; x < box_count; x++)
			std::cout << "[]";

		std::cout << "\n";
	}
}
// NEW CODE END ----------------------------------------

int main()
{
	print_pyramid(3); // 3 line tall pyramid
	print_pyramid(); // 5 line tall pyramid
}
Questions