c# - WriteLine overlap - c#

Is it possible to write a loop that will write to the console with overlapping text?
Desired output:
Notice how the lines overlap. I was using '|' vertically and '-' horizontally.
The vertical line is the 4th column and the horizontal line is the 3rd row.
I know this is way off:
for (int i = 1; i <= 4; i++)
{
Console.Write(" | ");
for (int x = 1; x <= 6; x++)
{
Console.Write(" - ");
}
}

Try Extended ASCII Codes. These might help you draw pretty pseudographics:
╒╥╦╫

Related

positioning a pair of chars(*) center instead of left [duplicate]

This question already has an answer here:
C# christmas tree
(1 answer)
Closed 4 years ago.
So i am struggling with a console application. It is required the line of chars to form a tree like pattern instead of right wing triangle. Like this(3):
*
***
*****
so far i have this:
int rows = int.Parse(Console.ReadLine());
for(int i = 1; i <= rows; i++)
{
for(int x = 1; x <= i; x++)
{
Console.Write("*");
}
Console.WriteLine("\n");
}
I think you're looking for the PadLeft function. It adds spaces to your string on the left side, so you can position it correctly. Also, you need to multiply the amount of rows by 2 and increase the step size by 1. You'll get the following code:
int rows = int.Parse(Console.ReadLine()) * 2;
for (int i = 1; i <= rows; i += 2) {
Console.Write( "".PadLeft( (rows - i) / 2) );
for(int x = 1; x <= i; x++) {
Console.Write("*");
}
Console.WriteLine();
}
Also, if you really want to make your triangle look like this, you might need to remove this line:
Console.WriteLine("\n");
... and change it to this:
Console.WriteLine();
(This will remove the unnessecary newline, WriteLine already prints a newline).

How to set a maximum value of string that can be shown in a matrix?

I'm trying to make a matrix that will show the string s only if it it displays a number less than 10. It would probably be easier doing this with an integer instead of the string but it doesn't show the same 8x8(or any other combination) the same because the "string.Empty" seems to be being the key(if someone could explain that part too, it would help me).
I also want to know how to display lets say letter "a" in the whole matrix, so that if the matrix is 3x3 I see 3 "a" in 3 rows.
Code here shows me matrix which lets s go over 10 which makes it not looking like a square.
string s = "";
int[,] matrix = new int[8, 8];
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
s += matrix[i, j] = i + j;
}
Console.WriteLine(s);
s = string.Empty;
}

C# Patterns;for loops

Develop a C# console application that displays the following pattern. Use for loops (hint: nested) to generate the patterns. All asterisks should be displayed by a single statement of the form Console.Write("*"); which displays the asterisks leading up to the number value shown in the example. Note the sequence of each number in turn. You will need to deduce how the numbers are computed (they are the result of a computation) and where that computation will be placed in the loop structures. You may not hardcode the displayed numbers into your loops.
I already have the "*" but can't figure out the numbers.
The two patterns should look similar to the following:
*2
**4
***6
****8
*****10
******12
*******14
********16
*********18
**********20
{
for (int row = 0; row < 10; row++)
{
Console.Write(" ");
for (int col = 0; col <= row; col++)
{
Console.Write("*");
Console.Write(" ");
}
Console.WriteLine(" ");
}
Console.WriteLine(" ");
for (int row = 0; row < 10; row++)
{
Console.Write(" ");
for (int col = 10; col > row; col--)
{
Console.Write("*");
Console.Write(" ");
}
Console.WriteLine(" ");
}
}
}
}
I already have the "*" but can't figure out the numbers.
You have the hardest part then: getting the numbers to print is much easier. Note that the number that you print is equal to two times the number of asterisks in front of it. Therefore, what you need to write is 2*n, where n is the limit in the loop that wrote the asterisks (i.e. the row plus one in your code).
You can combine the printing with writing the end-of-line mark: replace
Console.WriteLine(" ");
with
Console.WriteLine(2*(row+1));
to get the desired result.
I decided to make my comment to dasblinkenlight's answer into an answer on its own:
for(int row = 0; row < 10; row++) {
for(int col = 0; col <= row; col++) {
Console.Write("*");
}
Console.Write("{0}{1}", (row+1)*2, Environment.NewLine);
}
Notice, that the rownumer displayed at the end of the line is calculated as (row+1)*2, because your loop starts at index 0. The output would be faulty otherwise. This is followed by a new line.

Unusual Console.Write behaviour

Console.Write isn't behaving as it should; I'm trying to create a class that handles drawing to specific regions of the console, and some really odd stuff's been happening. I've got no clue how to debug it.
When I throw this code in:
string output;
for (int i = 0; i < Console.WindowHeight; ++i) {
Console.SetCursorPosition(0, i);
output = "";
for (int j = 0; j < Console.WindowWidth; ++j) output += (i % 10).ToString();
Console.Write(output);
}
I get the following: http://snag.gy/LrFcU.jpg (I'm not allowed to post images because of reputation limits -.-)
Yet when I modify it like so:
string output;
for (int i = 0; i < Console.WindowHeight; ++i) {
Console.SetCursorPosition(0, i);
output = "";
// take one character away from the string
for (int j = 0; j < Console.WindowWidth - 1; ++j) output += (i % 10).ToString();
Console.Write(output);
}
I get this! http://snag.gy/jxLH8.jpg
WindowWidth is 80, WindowHeight is 25, and both buffer width and height are equal to window width and window height.
I'm resetting the cursor position every interation of the outer for loop... Why would it be omitting the first line?
The first line isn't omitted, it's just that it scrolled out of sight.
You're adding one more character in the first version, thus reaching the end of the line and putting the caret on the next line.

c# Print rows from array that only contain positive integers

I'm really close ,but I just can't seem to wrap my head around the last step.
So what I want is to have 2d matrix and print only the rows that have ALL positive ints.
Right now my code is just taking out the ints that are either 0 or negative,but I need to remove the entire line if said line contains a 0 or negative.
This is what I'm getting right now
Input:
1 2 6 4
1 5 0 9
Output:
1 2 6 4
1 5
So basically it should not have printed the 2nd row b/c it had a non positive integer
What am I doing wrong?
I appreciate you taking the time to give this a look over!
static void Main(string[] args)
{
// reads in multiples lines( array from console)
List<string> L = readAllLines();
// feeds read lines into matrix
int[,] m = convertListToIntMatrix(L);// feeds read lines into matrix
int Rows = m.GetLength(0);
int Cols = m.GetLength(1);
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < Cols; c++)
{
if (m[r, c] <= 0)
{
break;
}
else Console.Write("{0} ", m[r, c]);
}
Console.WriteLine();
}
Change your loop to something like this:
for (int r = 0; r < Rows; r++)
{
bool rowOkay = true;
for (int c = 0; c < Cols; c++)
{
if (m[r, c] <= 0)
{
rowOkay = false;
}
}
if (rowOkay)
{
for(int i=0;i<Cols;++i) {Console.Write("{0} ", m[r,i]);}
Console.WriteLine();
}
}
You break when you find the 0 but you output everything before. Instead you could set a flag and later evaluate it to output the row if the flag is not set. You would still break but defer all output until you know the row is good.
Are you in Jim Harris' PP2 class by any chance? You could also set a variable to count how many positive numbers there were in the row and if it equals the number or integers in the row then print the row out.
if you are using list collection, just check Min() value of the list;
list1.Min() <= 0

Categories