Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I was asked to get the matrix input.
I did it by getting the input line by line.

Such as

1
2
3
4
5
6
7
8
9

But I was asked to get the input as in matrix format

1(Space)2(Space)3
4(Space)5(Space)6
7(Space)8(Space)9

Is there any solution to do so.
Posted
Updated 9-Feb-14 20:24pm
v2
Comments
Karthik_Mahalingam 10-Feb-14 2:03am    
in console app ?
KUMAR619 10-Feb-14 2:51am    
@Karthick Yes I want to run in Console Application
Prasad Avunoori 10-Feb-14 2:21am    
Please improve your question.`
Sergey Alexandrovich Kryukov 10-Feb-14 2:22am    
What have you tried so far?
—SA

1 solution

Try this

C#
Console.WriteLine("input:3*3 matrics. number space number space number space enter");
       int[,] input = new int[3, 3];
       for (int i = 0; i < 3; i++)
       {
           for (int j = 0; j < 3; j++)
           {
               var key = Console.ReadKey();
               if (key.Key != ConsoleKey.Spacebar &&  key.Key != ConsoleKey.Enter)
                   input[i, j] = Convert.ToInt32(key.KeyChar.ToString());
               else
                   j--;
               if (key.Key == ConsoleKey.Enter)
                   Console.WriteLine();
           }
       }
       Console.WriteLine();
       Console.WriteLine("output");
       for (int i = 0; i < 3; i++)
       {

           for (int j = 0; j < 3; j++)
           {
               Console.Write(input[i,j] + " ");
           }
           Console.WriteLine( );
       }
 
Share this answer
 
Comments
KUMAR619 10-Feb-14 4:47am    
@Karthik Thanks for your code.
Its working fine.

Now by pressing space its working.
Is there any way to separate inputs bu using "TAB" key
Karthik_Mahalingam 10-Feb-14 4:48am    
change this ConsoleKey.Spacebar to tab
KUMAR619 10-Feb-14 5:15am    
@Karthik Thanks
Are you on facebook
Karthik_Mahalingam 10-Feb-14 5:20am    
yes
are you rey mysterio fan ?
KUMAR619 10-Feb-14 5:21am    
Yes What about you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900