Get input from user and create 2D array

0

I want to take input from user and create 2D array the way that the user inputs values in two lines. First user define values separated by spaces then hits enter and gives another values also separated the spaces as shown below on example:

Give values:

2 3 4
5 6 7

variable should have this at the end:

[[2, 3, 4], [5, 6, 7]]

Another example:

Give values:

1 2
3 4

variable should have this at the end:

[[1, 2], [3, 4]]
c#
2021-11-24 06:06:06
1

2

I honestly don't know why you would make it this complicated, but here you go:

Console.Write("Please insert values separated by white-space: ");
string userInputLine1 = Console.ReadLine();
Console.Write("Please insert values seperated by white-space again: ");
string userInputLine2 = Console.ReadLine();

string[] userInputLine1Splitted = userInputLine1.Split(" ");
string[] userInputLine2Splitted = userInputLine2.Split(" ");

// Either this or catch an out-of-boundary exception when one is larger than the other and fill the space with 0's or something else.
if (userInputLine1Splitted.Length != userInputLine2Splitted.Length)
{
  throw new Exception("Both 1d arrays need to be the same length!");
}

int lengthOfArray = userInputLine1Splitted.Length;

// Since we  always have only 2 rows this can be hardcoded.
int[,] TwoDArrayFromUserInput = new int[2, lengthOfArray]; 

for (int col = 0; col < lengthOfArray; col++)
{
  TwoDArrayFromUserInput[0, col] = Convert.ToInt32(userInputLine1Splitted[col]);
  TwoDArrayFromUserInput[1, col] = Convert.ToInt32(userInputLine2Splitted[col]);
}

// Print to console to prove it worked.
for (int row = 0; row < 2; row++)
{
  for (int col = 0; col < lengthOfArray; col++)
  {
    Console.Write(TwoDArrayFromUserInput[row, col] + " ");
  }

  Console.WriteLine();
}

If you could specify your use-case I'm pretty sure I could help you come up with a way better solution.

2021-11-24 06:43:48

why u said its complicated? The tasks is to take 2D dime array from console. Whats wrong with that?
Arie

Maybe "complicated" was the wrong word. But depending on the use-case, there are much better alternatives than using 2 1D arrays as a kind of buffer to create a 2D array.
Axs

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................