hi i want to take input as shown below pls guide me

Locked hi i want to take input as shown below pls guide me

  • Sunday, March 11, 2012 5:56 PM
     
     
    Sample Input:
    5
    2 2
    1 1
    4 3
    10 1
    2 1

All Replies

  • Sunday, March 11, 2012 6:33 PM
     
     

    Hi Avengerzx,

    Could you clarify please your question,

    are you using C#? Console Application ? Only numbers input ? what you understand with input?

    this could help us to help you

    Regards Muli


    If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved when done...

  • Sunday, March 11, 2012 7:27 PM
     
      Has Code

    Try using C# array

    double[] input;


    Thanks & Regards, Bimal

  • Sunday, March 11, 2012 10:12 PM
     
     

    Deliberately not a good solution,  this will give you a start and next time write a sample of the code you have written and you will get useful help.

    for( int i = 0; i < 6; i++ )

    {

          string input = Console.Readline()

          string[] parts = input.split(' ');

          foreach( string value in parts )

         {

                DoSomething(value);

         }

    }


    Ta Ken

  • Monday, March 12, 2012 6:19 AM
     
     

    hi murali

     i want to take this as input in my c# console please help


    thahir

  • Monday, March 12, 2012 6:22 AM
     
     

    hi bimal the issue is the format of input as shown like this

    Sample Input:
    5
    2 2
    1 1
    4 3
    10 1
    2 1


    thahir

  • Monday, March 12, 2012 6:23 AM
     
     

    hi ken will pls elebrote the code

    i want to take input in this format

    Sample Input:
    5
    2 2
    1 1
    4 3
    10 1
    2 1


    thahir

  • Monday, March 12, 2012 7:40 AM
     
     Answered Has Code

    Hi,

    How do you want to have this data?

    Array of array? i mean IList<int[]> ?

     static void Main(string[] args)
            {
                var inputList = new List<int[]>();
                while (true)
                {
                    var intArray = new List<int>();
                    var value = Console.ReadLine();
                    //break with empty line
                    if (string.IsNullOrEmpty(value))
                    {
                        break;
                    }
                    int result = default(int);
    
                    foreach (var item in value.Split(' '))
                    {
                        if (!int.TryParse(item, out result))
                            Console.WriteLine("Not a valid data");
                        else
                            intArray.Add(result);
                    }
                    inputList.Add(intArray.ToArray());
                }
            }
    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

  • Monday, March 12, 2012 5:52 PM
     
     Answered Has Code

    I would do this:

    var q = File.ReadAllLines(filename).Skip( 1 ).Select( o => o.Split( ' ' ).Select( int.Parse ) );

    It's not really robust, but it's easy.

    The idea here is that you read all the lines of the file into an array of strings.  Then you skip the first line, because that's just the count.  It's not really necessary to verify that the count is the same as the number of lines that follow.  You can either use the number on the first line as the authoritative count or you can use the data that follows as the authoritative count.  I'm doing the latter.  It's not hard to parse the int on the first line to assert that they are the same, or to skip processing of extra data.  But if we assume your input is well formed, then we can skip that step.

    Then each line gets split by the space character into an array of strings.  We parse them all as integers by filtering that array with Select( int.Parse ).  This returns an IEnumerable<IEnumerable<int>>.

    To walk it you could do this:

    foreach( var line in q ) {
      foreach( var item in line ) {
        Console.WriteLine( item );
      }
    }

    If you want to index your data then you can .ToList() those queries.

    Here's an example:

    var data = File.ReadAllLines( filename ).Skip( 1 ).Select( o => o.Split( ' ' ).Select( int.Parse ).ToList() ).ToList();
    
    Console.WriteLine( data[2][1] ); // should be 3

    If you put the whole thing in a try-catch block, then you can handle missing files, malformed data, etc.

  • Monday, March 12, 2012 10:50 PM
     
     

    ahh I understood,

    just go on with:

     while(true)

    {

    string s = Console.ReadLine();

    if(s.ToLower() == "exit") break;

    Console.WriteLine(s);

    }

    this allows you whatever input you want to put in :)...

    But if you want strict input with an space in the middle containing then..

     while(true)

    {

    string s = Console.ReadLine();

    if(s.ToLower() == "exit") break; // if you write exit, it exit

    if(!s.Contains(' ')) continue; // if it does not conatin an space in the middle it reads again the input

    if(s.Split(' ').Length < 3 || s.Split(' ').Length > 3) continue; // if there are more than 3 splitet inputs, continue re take input

    Console.WriteLine(s); // at the end write this line :@ :)

    }

    Hope this was what you wanted.. if not reply back


    If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved when done...

  • Thursday, March 15, 2012 3:59 PM
     
     Answered Has Code

    Hi,

    here i use regex to get the result:

         Regex rEx = new Regex(@"(?<one>\d{1,2})*(?<two>\d)?");

                string s = null;
                do
                {
                    s = Console.ReadLine();

                    Match m = rEx.Match(s);
                    int? one = null;
                    int? two = null;            

                    if (m.Groups["one"].Success)
                    {
                        one = int.Parse(m.Groups["one"].Value);

                    }
                    if (m.Groups["two"].Success)
                    {
                        two = int.Parse(m.Groups["two"].Value);
                    }
                }
                while (!string.IsNullOrEmpty(s));


    Bilhan silva