Click here to Skip to main content
15,886,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Star Print Problem Using Swift For-Loop.
I am a swift beginner.
The teacher gave me the questions.
I tried to work for it.
However, the teacher expect to have another solutions.
Could anybody give me some suggestions?

Swift
//Question01

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

// Write down a function with 2 arguments ("X", "Y")

func startPrintStarQ1(x: Int, y: Int) -> String {
    let finalResult = null
    
    // please start your implementation to present the Q1 result

    return finalResult
}


AND

Swift
//Question02

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

// Write down a function with 2 arguments ("X", "Y")

func startPrintStarQ2(x: Int, y: Int) -> String {
    let finalResult = null
    
    // please start your implementation to present the Q2 result

    return finalResult
}


What I have tried:

For Question01, I tried to do in this way.

Swift
for i in 1...2 {

    for _ in 1...i {
        print("*", terminator: "")
    }

    for k in 1..<i {
        print("*", terminator: "")
    }

    print("")

}

for a in 2...3 {

    for _ in 1...a {
        print("*", terminator: "")
    }

    for k in 2..<a {
        print("*", terminator: "")
    }

    print("")

}

for b in 2...3 {

    for _ in 1...b {
        print("*", terminator: "")
    }

    for k in 1..<b {
        print("*", terminator: "")
    }

    print("")

}



For Question02, I tried the basic method.

Swift
let stars = ["    **  " , "  ****** ", "   ****  " , " ********" , "  ****** " , "**********"]

for star in stars {
    
    print("\(star)")
    
}

However, the teacher expect me to give another solutions.

I am trying to do the research.

Could anyone can give me some suggestions?

Thank you.
Posted
Updated 24-Mar-22 2:53am
v6
Comments
Richard MacCutchan 24-Mar-22 3:56am    
If a simple loop with a couple of counters is too difficult for you then you should really be talking to your teacher. Many people here could write that code but it would not help you to improve your skills.
dex1996 24-Mar-22 5:37am    
Thank you for your comments.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
dex1996 24-Mar-22 6:06am    
Thank you for your comments.
I have added what I'd tried on the post.
Thank you.
You're not looking at the pattern in those lines, specifically, what happens to the count of the number of stars from one line to the next.

That's all the help you're going to get because the rest is easy.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900