I need to create the following pattern:
It is homework, this question I failed the first time. I read now that I should have only used "*" one time, but how would this even work in that case? Would appreciate if anyone could give me some insight in how to think.
My code is down below:
using System;
class StarPattern
{
public static void Main()
{
for (int i = 0; i < 5; i++)
{
Console.Write("*");
}
for (int a = 0; a <= 0; a++)
{
Console.WriteLine("");
Console.Write("*");
}
for (int c = 0; c <= 0; c++)
{
Console.WriteLine(" *");
}
for (int d = 0; d <= 1; d++ )
{
Console.Write("*");
Console.WriteLine(" *");
}
for (int e = 0; e < 5; e++ )
{
Console.Write("*");
}
Console.ReadLine();
}
}
You can simplify your code by nesting loops so outer (i
) = rows and inner (j
) = columns. From looking at your sample, you want to write out if you're on the boundary of either so you can add a condition to just write out on min and max.
private static void Main(string[] args)
{
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 4; j++)
{
if (i == 0 || i == 4 || j == 0 || j == 4)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadKey();
}
I'd probably replace 0 with a constant called MIN and 4 with a constant called MAX to save duplicating them. That way, you could increase the size of the square by just changing the constants.
Hardly anyone is commenting their code for you. How disappointing!
The trick here is to focus on what is important and define values that you will;
These values are height and width - the core components of a rectangle.
The golden rule for the pattern is: If the row and column of each character is on the edge of the square, print a *
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stars
{
class Program
{
static int Width = 5; //define the width of the square in characters
static int Height = 5; //define the height of the square in characters
static void Main(string[] args)
{
for (int row = 0; row <= Height; row++) //Each iteration here is one row.
{
//loop representing columns. This is NESTED within the rows so
//that each row prints more than one column
for (int column = 0; column <= Width; column++)
{
if (IsCentreOfSquare(row, column)) //calculate if the current row and column coordinates are the interior of the square
{
Console.Write(" ");
}
else
{
Console.Write("*");
}
}
Console.WriteLine(); //this row is over. move to the next row
}
Console.ReadLine(); //pause so that the user can admire the pretty picture.
}
/// <summary>
/// Calculates if the row and column indexes specified are in the interior of the square pattern
/// </summary>
/// <returns></returns>
private static bool IsCentreOfSquare(int row, int col)
{
if (row > 0 && row < Height)
{
if (col > 0 && col < Width)
{
return true;
}
}
return false;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With