Write a program that, prints a \'pine tree\' consisting of triangles of increasi
ID: 3933452 • Letter: W
Question
Write a program that, prints a 'pine tree' consisting of triangles of increasing sizes, filled with a character ('*' or '+' or '$' etc). Your program should consist of three functions: A function print_shifted_triangle (n, m, symbol). It prints an n-line triangle, filled with symbol characters, shifted m spaces from the left margin. For example, if we call print_shifted_triangle (3, 4, '+'), the expected output is: A function print_pine_tree (n, symbol). It prints a sequence of n triangles of increasing sizes (the smallest triangle is a 2-line triangle), which form the shape of a pine tree. The triangles are filled with the symbol character. For example, if we call print_pine_tree (3, '#'), the expected output is: For example, if we call print_pine_tree (3, '#'), the expected output is: A main () function that interacts with the user to read the number of triangles in tree and the character filling the tree.Explanation / Answer
Code:
#include <iostream>
using namespace std;
void print_shifted_triangle(int n, int m, char symbol);
void print_pine_tree(int n, char symbol);
void print_triangle(int n);
int main(){
print_shifted_triangle(3,4,'+');
cout << endl;
print_pine_tree(3,'#');
}
void print_shifted_triangle(int n, int m, char symbol){
int k=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;j++){
cout << " ";
}
for(int space=1;space<=n-i;++space){
cout<<" ";
}
while(k!=2*i-1){
cout << symbol;
++k;
}
k=0;
cout<<" ";
}
}
void print_pine_tree(int n, char symbol){
print_shifted_triangle(n-1,n-1,symbol);
print_shifted_triangle(n,n-2,symbol);
print_shifted_triangle(n+1,n-n,symbol);
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.