Read matrix elements in row wise - c#

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 ;
}
}

Related

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.

Getting numbers from a matrix

Hi my problem is I can't get the numbers with more than 2 digits from this matrix if anyone can help I would appriciate it here i my code :
Console.Write("x: ");
int x = int.Parse(Console.ReadLine());
Console.Write("y: ");
int y = int.Parse(Console.ReadLine());
int[,] arr = new int[x, y];
int[,] arr2 = new int[x, y];
Random rand = new Random();
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
int randNUm = rand.Next(0, 20);
arr[i, j] = randNUm;
Console.Write(arr[i, j] + " ");
if (arr[i, j] >= 10)
{
arr2[i,j] = arr[i,j]
}
}
}
Actually you have done your job, all it remains is to display the results. If
the task specifies that the printing must be done in 2 steps as you imply,
try:
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
int randNUm = rand.Next(0, 20);
arr[i, j] = randNUm;
Console.Write(arr[i, j] + " ");
if (arr[i, j] >= 10)
{
arr2[i,j] = arr[i,j]
}
}
}
Console.WriteLine("---Proceeding to 2 digit numbers---");
for (int i = 0; i < arr2.GetLength(0); i++)
{
for (int j = 0; j < arr2.GetLength(1); j++)
{
Console.Write(arr2[i, j] + " ");
}
}
EDIT : Consider what Henk comments and try to optimise your solution.

Output to the screen without moving to a new line

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.)

How do I Transpose a multi dimensional array?

I think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so:
int[,] matris = new int[5, 8] {
{ 1, 2, 3, 4, 5,6,7,8 },
{9,10,11,12,13,14,15,16},
{ 17,18,19,20,21,22,23,24 },
{ 25,26,27,28,29,30,31,32 },
{ 33,34,35,36,37,38,39,40 },
};
and a for loop, like this:
for (int r = 0; r < 5; r++)
{
for (int j = 0; j < 8; j++)
Console.Write("{0} ", matris[r, j]);
Console.WriteLine();
}
So with this code I am printing out the multi dimensional array. But how do I print out a transpose of the array?
Just change your loops with each other:
for (int j = 0; j < 8; j++)
{
for (int r = 0; r < 5; r++)
Console.Write("{0} ", matris[r, j]);
Console.WriteLine();
}
Creating new array:
var newArray = new int[8, 5];
for (int j = 0; j < 8; j++)
for (int r = 0; r < 5; r++)
newArray[j, r] = matris[r, j];
You just need to do this:
for (int r = 0; r < 8; r++)
{
for (int j = 0; j < 5; j++)
Console.Write("{0} ", matris[j, r]);
Console.WriteLine();
}

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