Reputation: 33
I'm having trouble getting 4 different triangle patterns to appear side by side. This is a console application program.
This is exactly what I'm trying to achieve through the use of nested for loops:
* ******************** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
*********** ***********
I already have the individual patterns working already, but of course they appear one after the other.
using System;
class Assignment5
{
static void Main()
{
for (int i = 1; i <= 10; i++) // Outer loop for number of rows
{
for (int j = 1; j <= i; j++) // Inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End First Pattern
for (int i = 10; i >= 1; i--) // Outer loop for number of rows
{
for (int j = 1; j <= i; j++) // Inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End Second Pattern
for (int i = 10; i >= 1; i--) // Outer Loop for number of rows
{
for (int j = 1; j <= 10 - i; j++) //Inner loop for number of spaces
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++) //Secondary inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End Third Pattern
for (int i = 1; i <= 10; i++) //Outer Loop for number of rows
{
for (int j = 1; j <= 10 - i; j++) //Inner loop for number of spaces
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++) //Secondary inner loop for number of stars
{
Console.Write("*");
}
Console.WriteLine();
} // End Fourth Pattern
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
} // End class Assignment5
Upvotes: 3
Views: 15133
Reputation: 31428
Here's a brain teaser for you.
for (int n = 10; n > 0; n--)
{
var tri = "".PadRight(11 - n, '*').PadRight(10, ' ') + "".PadRight(n, '*').PadRight(10, ' ');
Console.WriteLine(tri + String.Join("", tri.ToCharArray().Reverse()));
}
Output:
* ******************** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* *********** ***********
Upvotes: 2
Reputation: 56982
Try this (very short version)
int sz=10;
for (int i=0;i<sz;i++){
for(int j=0;j<(sz*4);j++){
if(j<=i || (j>=sz && j<=(sz*2)-(i+1))
|| (j>=sz*2 && j>=(sz*2)+i && j<sz*3)
|| (j>=(sz*4)-(i+1))
)
{
Console.Write("*");
}
else {Console.Write(" ");}
}
Console.WriteLine();
}
Typed here (Not tested). but this may do the trick!
Upvotes: 0
Reputation: 111810
Another solution: you create an array with all the '*' and then you display it.
I have used a 3 dimensional array: row, column, pattern number.
I have even shifted everything in the 0 < x < 10, because in the world of C and C# arrays are 0-based.
Notice that I fill even the "blank" spaces of the array.
static void Main()
{
var chars = new char[10, 10, 4];
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= j)
{
chars[i, j, 0] = '*';
}
else
{
chars[i, j, 0] = ' ';
}
}
} // End First Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= 9 - j)
{
chars[i, j, 1] = '*';
}
else
{
chars[i, j, 1] = ' ';
}
}
} // End Second Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= j)
{
chars[i, j, 2] = '*';
}
else
{
chars[i, j, 2] = ' ';
}
}
} // End Third Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= 9 - j)
{
chars[i, j, 3] = '*';
}
else
{
chars[i, j, 3] = ' ';
}
}
} // End Fourth Pattern
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 4; k++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(chars[i, j, k]);
}
}
Console.WriteLine();
}
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
As I told you, I filled empty spaces with space during the array creation. I could have skipped it and instead replaced the spaces that are chars[i, j, k] == 0
with ' '
in the Console.Write, like:
static void Main()
{
var chars = new char[10, 10, 4];
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= j)
{
chars[i, j, 0] = '*';
}
}
} // End First Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= 9 - j)
{
chars[i, j, 1] = '*';
}
}
} // End Second Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i <= j)
{
chars[i, j, 2] = '*';
}
}
} // End Third Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10; j++)
{
if (i >= 9 - j)
{
chars[i, j, 3] = '*';
}
}
} // End Fourth Pattern
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 4; k++)
{
for (int j = 0; j < 10; j++)
{
if (chars[i, j, k] != 0)
{
Console.Write(chars[i, j, k]);
}
else
{
Console.Write(' ');
}
}
}
Console.WriteLine();
}
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
From there we could remove the if
and make them part of the for
, but it becomes quite unreadable...
static void Main()
{
var chars = new char[10, 10, 4];
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j <= i; j++)
{
chars[i, j, 0] = '*';
}
} // End First Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 0; j < 10 - i; j++)
{
chars[i, j, 1] = '*';
}
} // End Second Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = i; j < 10; j++)
{
chars[i, j, 2] = '*';
}
} // End Third Pattern
for (int i = 0; i < 10; i++) // Outer loop for number of rows
{
for (int j = 9 - i; j < 10; j++)
{
chars[i, j, 3] = '*';
}
} // End Fourth Pattern
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 4; k++)
{
for (int j = 0; j < 10; j++)
{
if (chars[i, j, k] != 0)
{
Console.Write(chars[i, j, k]);
}
else
{
Console.Write(' ');
}
}
}
Console.WriteLine();
}
Console.WriteLine("Press Enter for Part 2 of this Program");
Console.ReadKey();
Console.Clear();
} // End main function
Upvotes: 0
Reputation: 324
using System;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
int length = 10;
for (int i = 0; i < length; i++)
{
string result = String.Format("{0}{1}{2}{3}",
fillWithStarFromLeft(i + 1, length),
fillWithStarFromLeft(length - i, length),
fillWithStarFromRight(length - i, length),
fillWithStarFromRight(i + 1, length)
);
Console.WriteLine(result);
}
Console.ReadKey();
}
private static object fillWithStarFromRight(int length, int segmentlength)
{
string result = String.Empty;
for (int i = 0; i < length; i++)
{
result += "*";
}
return result.PadLeft(segmentlength, ' ');
}
private static string fillWithStarFromLeft(int length, int segmentlength)
{
string result = String.Empty;
for (int i = 0; i < length; i++)
{
result += "*";
}
return result.PadRight(segmentlength, ' ');
}
}
}
Just a quick though hope it helps
Upvotes: 1
Reputation: 2630
This will give you the required result.
using System.IO;
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++) // Outer loop for number of rows
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
for (int k = 10; k >= i; k--)
{
Console.Write(" ");
}
for (int l = 10-i; l >= 0; l--)
{
Console.Write("*");
}
for (int k = 0; k <= i*2; k++)
{
Console.Write(" ");
}
for (int k = 10-i; k >= 0; k--)
{
Console.Write("*");
}
for (int k = 10; k >= i; k--)
{
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
Console.Clear();
}
}
Upvotes: 1
Reputation: 109537
You didn't specify how you wanted to implement, so I present you with:
private static void Main()
{
Console.WriteLine(
@"* ******************** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
*********** ***********");
}
[EDIT] Ok, here's a less facetious answer. ;)
int n = 10;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j <= i; ++j)
Console.Write("*");
for (int j = 0; j < n-i-1; ++j)
Console.Write(" ");
for (int j = 0; j < n-i; ++j)
Console.Write("*");
for (int j = 0; j < 2*i; ++j)
Console.Write(" ");
for (int j = 0; j < n-i; ++j)
Console.Write("*");
for (int j = 0; j < n-i-1; ++j)
Console.Write(" ");
for (int j = 0; j <= i; ++j)
Console.Write("*");
Console.WriteLine();
}
[Second edit]
It would be more readable to write methods for outputting n
stars or spaces, like so:
static void stars(int count)
{
for (int i = 0; i < count; ++i)
Console.Write("*");
}
static void spaces(int count)
{
for (int i = 0; i < count; ++i)
Console.Write(" ");
}
And then:
int n = 10;
for (int i = 0; i < n; ++i)
{
stars(i+1);
spaces(n-i-1);
stars(n-i+1);
spaces(2*i);
stars(n-i);
spaces(n-i-1);
stars(i+1);
Console.WriteLine();
}
Upvotes: 3
Reputation: 17621
You're using the Console.Write
and Console.WriteLine
commands which are writing at the current cursor position. Your loops are always working in a 10*10 character range, followed by a newline.
An approach to your problem would be absolute cursor position, setting the cursor to the right position and then drawing one character. Each subsequent triangle must be moved by a offset:
int leftOffset = 0;
// draw first triangle
for x in ...
for y in ...
Console.SetCursorPosition(x, y);
Console.Write("*");
leftOffset += 10;
// draw second triangle
for x in ...
for y in ...
Console.SetCursorPosition(x + leftOffset, y);
Console.Write("*");
.
.
.
Be sure to clean the console before you start!
Upvotes: 0