Question

I'm having troubles drawing the following square:

* # # # # # 
* * # # # # 
* * * # # # 
* * * * # # 
* * * * * # 
* * * * * *

This is the method I wrote but it isn't working as it should be:

public void Draw(int width){
    char asterisk = '*';
    char hash = '#';
    int counter = 0;
    for (int h = 0; h < 6; h++) { //height?
        for (int w = 0; w < width; w++) {
            if (h == counter)
                Console.Write (asterisk);
            else
                Console.Write (hash);
        }
        counter++;

        Console.WriteLine ();
    }
    Console.WriteLine ();
}

Any suggestions on what I should be doing? I know that when it's the first row it should draw only a single asterisk, when it's the second row two asterisks and so on and so forth, but I really don't know how to do that.. Help please.

Was it helpful?

Solution 3

This lesson is intended to teach a student to use nested loops, and to understand that each instance of the inner loop can access the state of the outer loop. Unfortunately most teachers don't understand what the heck they're teaching and just give you the problem without explaining why it's a useful thing to practice.

Because you always want the exact same number of stars as the row you are on, you should just compare directly between the column index and the current row.

public void DrawSquare(int sideLength)
{
  for(int row = 1; row <= sideLength; row++)
  {
     for (int col = 1; col <= sideLength; col++)
     {
       if (col <= row) 
         Console.Write('*');
       else
         Console.Write('#');
     }
     Console.WriteLine();
  }
}

There are many shortcuts and c# tricks that could get you there faster, but this is the essence of what you should be trying to do.

OTHER TIPS

Instead of using a counter value, you can just change your criteria from if (h == counter) to if (h >= w).

Change h == counter with w <= counter

Furthermore you can drop the counter, since it is always equal to h

Why you do not use the reapeat constructor, something like this.

public void Draw(int width){
    int w_counter = 1;

    for (int l = 0; l < 6; l++) {
         var asterisk = new String('*', w_counter);
         var hash = new String('#', width - w_counter);
         Console.WrilteLine(asterisk + hash);
         w_counter++;
    }
}

Since counter is always equal to h, you will get all asterisks. It is the character position w on the line compared to the line number h, which determines the character to be printed. Therfore change the condition to w <= h

Also, since you want to print a square, height and width will have equal side lengths. Change the method signature to

public void DrawSquare(int sideLength)

Both loops will then have to test the loop variable against sideLength.

Actually I was looking for some nice algorithms to write, so here is my solution with only 1 for loop:

private static void Draw(int width)
{
   int numOfS = 1;
   string temp = "";
   int numOfH = width-1;
   int t1, t2;
   for (int i = 0; i < width; i++)
   {
       t1 = numOfS;
       t2 = numOfH;
       while (t1 > 0)
       {
         temp += "*";
         t1--;
       }
       while (t2 > 0)
       {
         temp += "#";
         t2--;
       }
       Console.WriteLine(temp);
       numOfH--;
       numOfS++;
       temp = "";
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top