2

Using the following:

func isosceles(triangleSides: Int)
{
    var y = 0

    for _ in 1...triangleSides {
        y = y + 1

        var stringBuilder = String()

        for _ in 1...y
        {
            stringBuilder += "*"
        }            
        println(stringBuilder)          
    }


}

isosceles(3)

I can make the following pattern:

*
**
***

I'm trying to write a function that outputs a sideways triangle of height 2n-1 and width n, so the output for n = 4 would be:

*
**
***
**
*

func triangle(triangleSides: Int)
{
    var y = 0
    var x = 0
    var index: Int

    for _ in 1...triangleSides {
        y = y + 1

        var stringBuilder = String()

        for _ in 1...y
        {
            stringBuilder += "*"
        }

        println(stringBuilder + "\n")

    }

    for _ in 1..<triangleSides{
        x = triangleSides - 1


        var index: Int
        var stringBuilder = String()

        for index in stride(from: triangleSides - 1, through: 1, by: -1) {
            stringBuilder += "*"
        }


        println(stringBuilder + "\n")


    }
}

I get

*
**
***
**
**

Obviously it's going to be me thats the problem with the way I am trying to do this. If somebody could point me in the right direction that would be appreciated.

Thanks

1
  • learn how do use debugger and breakpoints to debug your program Commented Oct 6, 2014 at 23:20

2 Answers 2

1

Well, there are quite a few things you're doing that you don't really need to do... but, your main problem is that your 3rd and 4th for loops are wrong.

The way it is now, it's looping from 1 to triangleSides - 1 in the outer loop and triangleSize - 1 to 1 in the inner loop. Since you're not using the actual index of the for loop, that just makes both the outer and inner loops run triangleSides - 1 times.

What you want to do is loop from triangleSize - 1 to 1 in your outer loop and 1 to the current value of your outer loop in your inner loop. It'll make it easier if you let the for loop do its job and assign x directly instead of ignoring the loop's index with _ and manually calculating x.

Those changes would look like this (just the second set of for loops):

for x in stride(from: triangleSides - 1, through: 1, by: -1) {
    var stringBuilder = String()

    for _ in 1...x {
        stringBuilder += "*"
    }

    println(stringBuilder + "\n")
}

If you want to clean it up a little more, you can also let the for loop do its job in the first set of loops and get rid of the declarations of x, y, and index at the start of your function:

func triangle(triangleSides: Int)
{
    for y in 1...triangleSides {
        var stringBuilder = String()

        for _ in 1...y
        {
            stringBuilder += "*"
        }

        println(stringBuilder + "\n")

    }

    for x in stride(from: triangleSides - 1, through: 1, by: -1) {
        var stringBuilder = String()

        for _ in 1...x {
            stringBuilder += "*"
        }

        println(stringBuilder + "\n")
    }
}

Now, this:

triangle(3)

Outputs:

* 
**
***
**
*
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent thank you so much for you help. And the explaining things a bit further. I was working through some old C simple I/O examples and made things more confusing for myself than needed.
1

Here is my version:

func triangle(triangleSides: Int)
{
    var y = 0
    var x = 0
    var index: Int
    var stringBuilder = ""

    for _ in 1...triangleSides {
        stringBuilder += "*"
        println(stringBuilder)
    }

    for z in reverse(1..<triangleSides) {
        let index = advance(stringBuilder.endIndex, -1)
        stringBuilder = stringBuilder.substringToIndex(index)
        println(stringBuilder)
    }
}

triangle(4)

Output:

*
**
***
****
***
**
*

Comments. I think the double loop was too complex.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.