Output to the screen without moving to a new line - c#

Console.WriteLine automatically move text on a new line and I have output on the console like:
1
2
3
4
5
..
But I need:
1234
5..
Code:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
Console.WriteLine(aField[i, j]);
Console.WriteLine();
}

If you don't want to write a line, don't use Console.Write*Line*. Just use Console.Write:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(aField[i, j]);
}
Console.WriteLine();
}
(Note that your sample output only contains four characters, whereas your inner loop has 5 iterations. I'm hoping this is just a sample discrepancy, and nothing more complicated.)

Related

Chess board positions

How can i make this so the user types positions in console (table[4,5]) i want that user types that?
int[,] table = new int[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if ((i + j) % 2 == 0)
{
table[i, j] = 0;
}
else
{
table[i, j] = 1;
}
}
}
Console.WriteLine("4 - king");
Console.WriteLine("3 - queen");
Console.WriteLine("4 - hunter");
table[4,5] = 2;
table[6,7] = 3;
table[2,2] = 4;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write(table[i, j] + " ");
}
Console.WriteLine();
}
What should i do to make this work?And if i type this it doesnt work:
Thats why i have to put figures in different rows or columns how do i fix this
table[4,5] = 2;
table[4,7] = 3;
table[2,2] = 4;
You are confusing the assignment operator "=" with the logical comparison operator "==". Your second line is just comparing table[4,5] with 2 and probably returning false.
Change it to:
table[4,5] = 2;
Also, even if you manage to assign a value to table[4,5], you will overwrite it in the next lines. You should move that line to the end of the first nested loop. Just before the second "for (int i = 0; i < 8; i++)"

Read matrix elements in row wise

I am writing C# program for a matrix.when I enter matrix inputs from console, each element is coming in the separate row.But, I want to read row elements in a single line.
This is my code
Console.WriteLine("Enter the matrix");
int n= Convert.ToInt32(Console.ReadLine());
int[ , ] matrix=new int[n,n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
matrix[i,j]=Convert.ToInt32(Console.ReadLine());
// Console.Write("\t");
}
}
present I am getting like
1
2
3
4
But, I want like
1 2
3 4
Help me.
If you want to read one row in one line, you can ask user to enter space-separated values like 1 2, 3 4 and read like this
Console.WriteLine("Enter the matrix size");
int n = Convert.ToInt32(Console.ReadLine());
//add size and other validation if required
int[,] matrix = new int[n, n];
Console.WriteLine("Enter your values separated by space.");
for (int i = 0; i < n; i++)
{
var values = (Console.ReadLine().Split(' '));
for (int j = 0; j < n; j++)
{
matrix[i, j] = int.Parse(values[j]);
}
}
//to write
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
You can enter the entire line and do the following:
for(int i=0; i<n; i++){
var input = Console.ReadLine().Split(' ').Select(t => int.Parse(t)).ToArray();
for (int j = 0 ; j < n ; j++){
matrix[i, j] = input[j];
}
}
Console.WriteLine("Enter the matrix");
int n= Convert.ToInt32(Console.ReadLine());
int[ , ] matrix=new int[n,n];
for(int i=0; i<n; i++){
string line = Console.ReadLine();
string[] elements = line.Split(' ');
for(int j=0; j<n || j < elements.Length; j++){
matrix[i,j]=Convert.ToInt32(elements[j]);
}
}
Please look same question
In your situation :
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j + 2){
string input = Console.ReadLine();
string[] split = input.Split(' ');
int firstNumber = Int32.Parse(split[0]);
int secondNumber = Int32.Parse(split[1]);
matrix[i,j] = firstNumber ;
matrix[i,(j+1)] = secondNumber ;
}
}

Printing a 2D-array into "squares"

I'm trying to learn how to work with 2D-array and I can't seem to understand how to print them correctly. I want to print them in a "square" like 5x5 but all I get is one line. I've tried both WriteLine and Write and changed some of the variables in the loops but I get either an error or not the result I want to have. The code is supposed to print out a 5x5 with a random sequence of 15 numbers in each column. I get the correct numbers out of it, it's only the layout that is wrong.
static void Main(string[] args)
{
Random rnd = new Random();
int[,] bricka = new int[5, 5];
int num = 0;
int num1 = 1;
for (int i = 0; i < bricka.GetLength(1); i++)
{
num += 16;
for (int j = 0; j < bricka.GetLength(0); j++)
{
bricka[j, i] = rnd.Next(num1, num);
}
num1 += 16;
}
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
}
Console.ReadKey();
}
This is my print, I would like to have the the 12 under the 8 and 14 under the 12 and so on.
http://i.imgur.com/tfyRxf1.png
You need to call WriteLine() after each line, so that each line is printed on a separate line:
for (int i = 0; i < bricka.GetLength(0); i++)
{
for (int j = 0; j < bricka.GetLength(1); j++)
{
Console.Write(bricka[i, j]+ " " );
}
Console.WriteLine();
}
That would be one way of doing it, anyway.

C# program that returns morse code for numbers not working

I'm trying to make a program that: When given a 4 digit number (for example, 1001) it sums the digits of that number, for 1001 this is 1 + 0 + 0 + 1 = 2, than it finds all sequences of 6 numbers from 1 to 6 (including permutations, i.e. 1*1*1*1*1*2 is different than 2*1*1*1*1*1) whose product is that number.
The result should be printed on the console in the following format: each sequence of 6 numbers, with their Morse representation, separated with a single pipe: 1 is .---- , 2 is ..---: .----|.----|.----|.----|..---|, on a new row the next permutation: .----|.----|.----|..---|.----| and so on.
The problem is, my solution doesn't show the correct answers, not even the correct number of them.
Here's my code (and please, if possible, tell me where my mistake is, and not some one line hack solutions to the problem with LINQ and regex and God knows what):
string n = Console.ReadLine();
string[] digitsChar = new string[n.Length];
for (int i = 0; i < 4; i++)
{
digitsChar[i] = n[i].ToString();
}
int[] digits = new int[digitsChar.Length];
for (int i = 0; i < 4; i++)
{
digits[i] = Convert.ToInt32(digitsChar[i]);
}
int morseProduct = digits.Sum();
Console.WriteLine(morseProduct);
List<int> morseCodeNumbers = new List<int>();
for (int i = 1; i < 6; i++)
{
for (int j = 1; i < 6; i++)
{
for (int k = 1; i < 6; i++)
{
for (int l = 1; i < 6; i++)
{
for (int m = 1; i < 6; i++)
{
for (int o = 1; o < 6; o++)
{
int product = i * j * k * l * m * o;
if (product == morseProduct)
{
morseCodeNumbers.Add(i);
morseCodeNumbers.Add(j);
morseCodeNumbers.Add(k);
morseCodeNumbers.Add(l);
morseCodeNumbers.Add(m);
morseCodeNumbers.Add(o);
}
}
}
}
}
}
}
int numberOfNumbers = morseCodeNumbers.Count;
string[] morseCodes = new string[] { "-----", ".----", "..---", "...--", "....-", "....." };
for (int i = 0; i < numberOfNumbers; i++)
{
int counter = 0;
if (i % 5 == 0)
{
Console.WriteLine();
counter = 0;
}
if (counter < 5)
{
int index = morseCodeNumbers[i];
Console.Write(morseCodes[index] + "|");
counter++;
}
A lot of your for-loop conditions refer to i instead of j,k,l and m. The same for the increment part. For example:
for (int j = 1; i < 6; i++)
should be
for (int j = 1; j < 6; j++)
Furthermore if the range is from 1 to 6 you need to change < to <=, see:
for (int i = 1; i <= 6; i++)
You don't need to convert the string to a string array to get the int array of digits btw, so while this is correct:
for (int i = 0; i < 4; i++)
{
digitsChar[i] = n[i].ToString();
}
int[] digits = new int[digitsChar.Length];
for (int i = 0; i < 4; i++)
{
digits[i] = Convert.ToInt32(digitsChar[i]);
}
you could it do like that (sry for LINQ):
var digits = n.Select(c=>(int)char.GetNumericValue(c) ).ToArray();

To solve upside-down rendering?

I have written this segment of C# to help me understand how nested for loops can be used to render 2 dimensional data.
Here is what the output looks like.
████
███
██
█
I would like to make it so that the 4 blocks up top are rendered at the bottom, basically in the reverse order so that the steps ascend. However the console window only renders downward, so the conventional thinking won't be right. The following is my code.
static void Main(string[] args)
{
int i = 0;
int j = 0;
for (i = 0; i < 4; i++)
{
Console.Write('\n');
for (j = i; j < 4; j++)
{
Console.Write("█");
}
}
Console.ReadKey();
}
This is what I'd like the output to look like.
█
██
███
████
You need to reverse your loop condition from inremant to decremant..
for (i = 0; i < 4; i++)
{
Console.Write('\n');
for (j = i; j >= 0; j--)
{
Console.Write("█");
}
}
Output will be;
Here is a DEMO.
UPDATE: Since you change your mind, you need to add space every column (column number isi) 4 - 1 times.
public static void Main(string[] args)
{
int i = 0;
int j = 0;
for ( i = 0; i < 4; i++ )
{
for ( j = 0; j < 4; j++ )
{
if ( j < 3 - i )
Console.Write(" ");
else
Console.Write("█");
}
Console.Write('\n');
}
Console.ReadKey();
}
Here is a DEMO.
class Program
{
const int Dimension = 4;
static void Main(string[] args)
{
char[] blocks = new char[Dimension];
for (int j = 0; j < Dimension; j++)
blocks[j] = ' ';
for (int i = 0; i < Dimension; i++)
{
blocks[Dimension - i - 1] = '█';
for (int j = 0; j < Dimension; j++)
Console.Write(blocks[j]);
Console.WriteLine();
}
Console.ReadKey();
}
}
Should be:
for (j = 3 - i; j < 4; j++)
{
Console.Write("█");
}
The easiest way would be: just reverse your inner loop condition and decrement the counter instead of incrementing it:
for (i = 0; i < 4; i++)
{
Console.Write('\n');
for (j = i; j >= 0; j--)
{
Console.Write("█");
}
}
Console.ReadKey();
returning:
█
██
███
████
And for right-to-left version:
for (i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(j < 3 - i)
Console.Write(" ");
else
Console.Write("█");
}
Console.Write('\n');
}
Console.ReadKey();
with a result:
█
██
███
████

Categories

Resources