Loops – Drawing a tree
include
using namespace std;
int main() {
int trunkHeight, trunkWidth, leavesWidth;
cout << "Enter trunk height: ";
cin >> trunkHeight;
cout << "Enter trunk width: ";
cin >> trunkWidth;
if(trunkWidth % 2 == 0) {
cout << "Enter an odd number for trunk width: ";
cin >> trunkWidth;
}
cout << "Enter leaves width: ";
cin >> leavesWidth;
if(leavesWidth % 2 == 0) {
cout << "Enter an odd number for leaves width: ";
cin >> leavesWidth;
}
// Calculates spacing for different levels and the leaves of the tree
for(int a = 1; a <= leavesWidth; a = a + 2){
for(int b = 0; b < (leavesWidth - a) / 2; ++b) {
cout << " ";
}
for(int c = 0; c < a; c++) {
cout << "*";
}
cout << endl;
}
//Calculates length/width of trunk and positioning under leaves
int trunkSpaces = (leavesWidth - trunkWidth) / 2;
for(int d = 0; d < trunkHeight; ++d) {
for(int e = 0; e < trunkSpaces; ++e) {
cout << " ";
}
for(int f = 0; f < trunkWidth; ++f) {
cout << "*";
}
cout << endl;
}
return 0;
}
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
