0

I'm trying to use for loop.

If the input is 3, the result going to be:

1**
*2*
**3

So I used this code

void button1_Click(object sender, EventArgs e) {
    string message = " ";
    int value = numericUpDown1.Value;
    for(int count = 0; count < value; count++) {
        for(int m = -2; m < value; m++) {
            message += "*";
        }
        message += "\r\n";
    }
    MessageBox.Show(message);
}

I used the two for-loops but i could not fix it to show the number, but it give me

***
***
***
2
  • 1
    forgot to tag it as homework ? Commented Jul 25, 2012 at 4:14
  • Your number will be your count variable +1 Commented Jul 25, 2012 at 4:20

2 Answers 2

3
int digitCount = 3;
string message = " ";
for (int round = 1; round <= digitCount; round++)
{
    for (int digit = 1; digit <= digitCount; digit++)
    {
        if (digit == round)
        {
            message += digit;
        }
        else
        {
            message += "*";
        }
    }

    message += "\r\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

declare this instead string message;
1
private string StarNumbers(int input)
    {
        var range = Enumerable.Range(1, input);
        var sb = new StringBuilder(input*input);
        foreach (var number in range)
        {
            var line = String.Format("{0}{1}{2}", new string('*', number - 1), number, new string('*', input - number));
            sb.AppendLine(line);
        }
        return sb.ToString();
    }

EDIT:

This code for input=500 : 2ms

(5ms for input 1 000)

Code using += string concatenation for input=500 : 33 468ms (all it does is garbage collection)

(running it for several minutes for input 1 000 and still nothing)

5 Comments

Your proposed solution is over engineered and harder to read thus more prone to bugs. Also, your String.Format uses more resources than concatenating string in such a simple scenario. The size of your declared string builder is off by a constant equal to 2xinput, thus it needs to be expanded, thus taking extra resources (you didn't account for new lines). Simplicity is king.
Actually you are wrong in the resources thing, you can easily measure the difference ;)
The stringbuilder needs to be expanded just once, with += new string is created each time.
For Fun: You can measure the difference because you've read the String.Format code? Analyzed the work that is required to parse out your place holders and re-concatenate the strings for you? I didn't care enough to correct every mistake you made but a "non-beginner" would perform three appends to the string builder which is optimized for concatenating strings. P.S. Your solution is still overkill.
:) I fixed the loop of someone who appears to be learning to code with requirements which by all accounts appear trivial. At no point did I claim my solution to be optimized, note the simple scenario statement above. I do however claim that any solution which has the .Net framework create an enumeration with a for-each loop is overkill. Wait, is that three legitimate reasons for overkill... Humm... :)

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.