I have a matrix. And I need to get 1D arrays from my matrix. For example, I have follow matrix:
123
456
789
So it looks like 3 arrays: 147, 258, 369.
But I got "Index out of range exception" in this code:
int[] b = new int[n];
for (i = 0; i < n; i++)
{
b[i] = a[i, n];
Console.Write(b[i] + " ");
}
Console.WriteLine();
Thanx for any help.
Here's full code that works already:
static void Main(string[] args)
{
int n = 0, m = 0, i = 0, j = 0;
Random r = new Random();
Console.WriteLine("Please, input matrix size:");
Console.Write("\tn = ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("\tm = ");
m = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
int[,] a = new int[n, m];
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
a[i, j] = r.Next(0, 2);
}
}
showMe(a, n, m);
Console.WriteLine();
run(a, n, m);
Console.ReadKey();
int[][] b = new int[m][];
for (i = 0; i < m; i++)
{
b[i] = new int[n];
for (j = 0; j < n; j++)
{
b[i][j] = a[j, i];
Console.Write(b[i][j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.ReadKey();
}
ais of sizen? It would be helpful to see the definitions ofaandnin this example.