“7. Half-pine tree. Request the height. See below for an image. This tree is mad
ID: 3664795 • Letter: #
Question
“7. Half-pine tree. Request the height. See below for an image. This tree is made up of multiple branches. Each row in a branch increases by 1. Each branch has height 4, and each branch starts at one location more horizontally than the previous branch.
ex:
Welcome to Picture Maker!
Which shape should I draw?7
Height?10
*
**
***
****
**
***
****
*****
***
My current c++ code is below:
case 7:
cout<<"height?";
cin>>height;
for(i=1; i<=height; i++){
for(j = 1; j<=(i % 5)+ (i / 5); j++){
cout"*";
}
cout<<" ";
}
cout<<endl;
break;
As you know it is not working properly. I dont know what went wrong. please fix it, thank you very much.
Explanation / Answer
/*C++ test program that prints pine tree of height and each
branch of size=4.
Note : The loops conitnues for each height value a brance of 4
so there would 4*height lines.
*/
#include<iostream>
using namespace std;
int main()
{
int height=5;
const int BRANCH_HEIGHT=4;
char ch='*';
int add=0;
for(int index=1;index<=height;index++)
{
int j;
//print branch height
for(j=1;j<=BRANCH_HEIGHT;j++)
{
//Add for each next branch from j+add starts
for(int m=1;m<=(j+add);m++)
cout<<ch;
cout<<endl;
}
//Check if new branch is to started
if((j-1)%BRANCH_HEIGHT==0)
{
//increment add=add+1
add++;
//set j=1
j=1;
}
}
system("pause");
return 0;
}
------------------------------------------------------------------------------------
use code for case 7:
case 7:
cout<<"height?";
cin>>height;
const int BRANCH_HEIGHT=4;
char ch='*';
int add=0;
for(int index=1;index<=height;index++)
{
int j;
//print branch height
for(j=1;j<=BRANCH_HEIGHT;j++)
{
//Add for each next branch from j+add starts
for(int m=1;m<=(j+add);m++)
cout<<ch;
cout<<endl;
}
//Check if new branch is to started
if((j-1)%BRANCH_HEIGHT==0)
{
//increment add=add+1
add++;
//set j=1
j=1;
}
}
------------------------------------------------------------------------------------------------------------------------------------
Sample output:
*
**
***
****
**
***
****
*****
***
****
*****
******
****
*****
******
*******
*****
******
*******
********
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.