Question: Create a * based Christmas tree that looks like the following: * *** ***** ******* ********** * Use a loop to algoritmically create the top

  • Create a * based Christmas tree that looks like the following:
    • *
    • ***
    • *****
    • *******
    • **********
    • *
  • Use a loop to algoritmically create the top five lines of the tree
    • The first line has 4 spaces followed by 1 *
    • The next line has 3 spaces followed by 3 *
    • The next line has 2 spaces followed by 5 *
    • The next line has 1 spaces followed by 7 *
    • The last line has 0 spaces followed by 9 *
  • Store each output line on the tree in an Array
  • Finally use one more array to output the contents of your Array

My code is as follows: Create a * based Christmas tree that looks like the following:

var arr = [String]()

for i in 1...5 {

for _ in 1...2*i-1 {

arr.append("*")

}

arr.append(" ")

}

for i in arr{

print(i, terminator: "")

}

it outputs the image above. I need to make the output look like a tree. How do I fix this?

Transcribed image text

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!