Reputation: 410
Take a look at this problem: Write a program that prints a triangle of 9 copyright symbols ©
The console should print a simple triangle using those ©
symbols. The triangle has to be empty, not filled with ©
thingies.
I changed the console character encoding:
Console.OutputEncoding = Encoding.UTF8;
so ©
prints correctly. I also changed the font.
I've been trying to figure out a way to draw the triangle by using a for loop, but my rookie's knowledge is obviously still too insufficient for this kind of tasks.
Could you give me some tips/hints as to how to draw the triangle with a for loop?
By the way, I found the problem solved like this:
Console.OutputEncoding = Encoding.UTF8;
char copyRight = '\u00A9';
Console.WriteLine("{0,4}\n{0,3}{0,2}\n{0,2}{0,4}\n{0}{0,2}{0,2}{0,2}", copyRight);
Upvotes: 1
Views: 2268
Reputation: 19482
I've always wondered nobody used PadLeft and PadRight:
public void DrawEmptyTriangle( char symbol, char padSymbol, int triangleBase)
{
// heuristics - this is because of the 2nd line
int alignment = triangleBase - 3;
int half = triangleBase / 2 + 1;
// top and bottom line are different from the others,
// so it is easier to generate them separately
string top = symbol.ToString ( ).PadLeft ( half, padSymbol).PadRight ( triangleBase, padSymbol);
// if this is new to you - Enumerable.Repeat will generate a sequence of n character elements
// string.Join will make a string inserting, in this case, a space between them
string bottom = string.Join ( " ", Enumerable.Repeat ( 'x', half ) );
Console.WriteLine ( top );
for ( int index = 1; index < triangleBase; index+=2 )
{
string line =
string.Format ( "{0}{1}{0}", symbol, new string ( padSymbol, index ) )
.PadLeft ( alignment, padSymbol )
.PadRight ( alignment, padSymbol );
alignment++;
Console.WriteLine ( line );
}
Console.WriteLine ( bottom );
}
Read the comments in the code, and play a little with this method :)
Upvotes: 2
Reputation: 111
Here is a plain example. It looks scarier than it is.
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Draw(' ', '\u00A9', 20, true);
}
static void Draw(char sym, char back, int length, bool padding)
{
if (length < 5)
{
length = 5;
}
else if ((length % 2) == 0) // Ensure an odd length using a modulo operation
{
length++;
}
if (padding)
{
Console.WriteLine(new string(back, length));
}
int mid = length / 2;
int left = mid;
int right = mid + 1;
do
{
Console.Write(new string(back, left));
Console.Write(new string(sym, right - left));
Console.Write(new string(back, length - right));
Console.WriteLine();
left--;
right++;
}
while (left != 0);
if (padding)
{
Console.WriteLine(new string(back, length));
}
Console.WriteLine();
}
Output:
©©©©©©©©©
©©©© ©©©©
©©© ©©©
©© ©©
© ©
©©©©©©©©©
Alternative version for 9 symbols:
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Render(Draw('\u00A9', 4));
Render(Draw('\u00A9', 8));
Render(Draw('\u00A9', 16));
}
static char[,] Draw(char sym, int height)
{
int width = (height * 2) - 1;
char[,] map = new char[height, width];
int x = width - 1;
int y = height - 1;
for (int i = 0; i < height; i++)
{
map[y, i * 2] = sym;
if (i != 0)
{
map[y - i, i] = sym;
map[y - i, x - i] = sym;
}
}
return map;
}
static void Render(char[,] map)
{
int width = map.GetLength(1);
int height = map.GetLength(0);
for (int i = 0; i < height; i++)
{
if (i != 0)
{
Console.WriteLine();
}
for (int j = 0; j < width; j++)
{
char c = map[i, j];
Console.Write(c == '\0' ? ' ' : c);
}
}
Console.WriteLine();
}
Output:
©
© ©
© ©
© © © ©
©
© ©
© ©
© ©
© ©
© ©
© ©
© © © © © © © ©
©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© ©
© © © © © © © © © © © © © © © ©
Upvotes: 3
Reputation: 659956
Could you give me some tips/hints as to how to draw the triangle with a for loop?
Sure. Start with: can you write a program that you understand that draws a triangle without using any loops?
Once you've done that, can you write that program using a simpler loop, like a while
?
If not, learn how while
works before tackling for
. You should be able to clearly answer the question "precisely what is the difference between code in the form if(condition) statement
and while(condition) statement
?"
Once you've done that, can you transform your correct program written using while
into an equivalent program that uses for
? There is a very clear relationship between while
and for
.
Upvotes: 10