1

I have a matrix, which is read from the console. The elements are separated by spaces and new lines. How can I convert it into a multidimensional int array in c#? I have tried:

String[][] matrix = (Console.ReadLine()).Split( '\n' ).Select( t => t.Split( ' ' ) ).ToArray();

but when I click enter, the program ends and it doesn't allow me to enter more lines.

The example is:

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9  
5
  • What language is this (add it to the tags)? Also you're only reading a single line. Commented Jan 22, 2014 at 13:03
  • 1
    Maybe I've been away from console apps for too long, but how would you accept \n in a Console.ReadLine? Isn't the read terminated on \n? Commented Jan 22, 2014 at 13:07
  • @BradChristie - by accepting multiple Console.ReadLine()'s in a loop Commented Jan 22, 2014 at 13:15
  • @Jamiec: Or, the answer is: "you cannot accept \n through a readline". Commented Jan 22, 2014 at 13:16
  • Is the question about how to enter the matrix from the console, or how to convert a string array into an int array? Please clarify your question. Commented Jan 22, 2014 at 14:50

4 Answers 4

1
int[,] Matrix = new int[n_rows,n_columns];


    for(int i=0;i<n_rows;i++){
        String input=Console.ReadLine();
        String[] inputs=input.Split(' ');
        for(int j=0;j<n_columns;j++){
            Matrix[i,j]=Convert.ToInt32(inputs[j]);
        }
    }

you can try this to load the matrix

Sign up to request clarification or add additional context in comments.

Comments

0

First things first, Console.ReadLine() reads a single line from the input. So in order to accept multiple lines you need to do 2 things:

  1. Have a loop which allows the user to continue entering lines of data. You could let them go until they leave a line blank, or you could fix it to 5 lines of input
  2. Store these "lines" of data for future processing.

Assuming the user can enter any number of lines, and a blank line (just hitting enter) indicates the end of data entry something like this will suffice

List<string> inputs = new List<string>();
var endInput = false;
while(!endInput)
{
   var currentInput = Console.ReadLine();
   if(String.IsNullOrWhitespace(currentInput))
   {
       endInput = true;
   }
   else
   {
        inputs.Add(currentInput);
   }
}

// when code continues here you have all the user's input in separate entries in "inputs"

Now for turning that into an array of arrays:

var result = inputs.Select(i => i.Split(' ').ToArray()).ToArray();

That will give you an array of arrays of strings (which is what your example had). If you wanted these to be integers you could parse them as you go:

var result = inputs.Select(i => i.Split(' ').Select(v => int.Parse(v)).ToArray()).ToArray();

Comments

0
// incoming single-string matrix:
String input = @"1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9";

// processing:
String[][] result = input
    // Divide in to rows by \n or \r (but remove empty entries)
    .Split(new[]{ '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
    // no divide each row into columns based on spaces
    .Select(x => x.Split(new[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries))
    // case from IEnumerable<String[]> to String[][]
    .ToArray();

result:

String[][] result = new string[]{
  new string[]{ "1","2","3","4","5" },
  new string[]{ "2","3","4","5","6" },
  new string[]{ "3","4","5","6","7" },
  new string[]{ "4","5","6","7","8" },
  new string[]{ "5","6","7","8","9" }
};

Comments

0

It can be done in multiple ways

You can read a single line containing multiple numbers separated by a char, split by that char obtaining an array of ints and then you should fetch a matrix.

With out-of-the-box linq there is no trivial way for the fetching step and i think it is not really the case to use third-party libraries from codeplex like LinqLib or something.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.