Friday, January 15, 2021

Q.15: How to convert a one-dimensional array to a two-dimensional array?

 Ans.: The user will input 1-D array along with the number of rows and columns. The method should convert this 1-D array to a 2-D array(matrix) of given row and column. We will create matrix row wise.

  •  input: {1, 2, 3, 4, 5, 6} ,2 ,3

output: 1 2 3

4 5 6

internal static void SingleToMulti(int[] array, int row, int column)
{
int index = 0;
int[,] multi = new int[row, column];
for (int y = 0; y < row; y++)
{
for (int x = 0; x < column; x++)
{
multi[y, x] = array[index];
index++;
Console.Write(multi[y, x] + " ");
}
Console.WriteLine();
}
}

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...