none
user input of array is this right?plz help me

    Question

  • using System;

    namespace ConsoleApplication1
    {

    class Program
    {

    public static void Main(String [] args)
    {
    int a;
    Console.WriteLine("Enter size of index:");
    a= int.Parse(Console.ReadLine());
    int [] index = new int [a];
    for (int i=0; i<index.Length;i++)
    {
    Console.WriteLine("Enter number:");
    index[i]=int.Parse(Console.ReadLine());
    }

    }
    }
    }
    Tuesday, July 05, 2011 12:25 PM

Answers

  • Code looks fine, maybe some work to do.

    Try this code:

    int number = 0;
          Console.WriteLine("Enter size of index array:");
          if (int.TryParse(Console.ReadLine(), out number))
          {
            int[] array = new int[number];
            for (int i = 0; i < number; i++)
            {
              Console.WriteLine("Enter a number for {0}. number, {1} remaining:", i.ToString(), (number - (i)).ToString());
              int indexNumber = 0;
              if (int.TryParse(Console.ReadLine(), out indexNumber))
              {
                array[i] = indexNumber;
              }
            }
            Console.WriteLine("---------");
            Console.WriteLine("This is it, now let show the result:");
            foreach (int item in array)
              Console.WriteLine("{0}", item);       
          }
          Console.ReadLine();
    



    Mitja
    Tuesday, July 05, 2011 1:15 PM

All replies

  • Hi,

    The program seems right. however I didn't understood what you want to do?

    Thanks,

    Ajit

    Tuesday, July 05, 2011 12:31 PM
  • Hi

    Where is your problem?


    The complexity resides in the simplicity Follow me at: http://smartssolutions.blogspot.com
    Tuesday, July 05, 2011 12:41 PM
  •  ah k so that's it? :) no missing?i think it have...:D
    Tuesday, July 05, 2011 12:57 PM
  • ah k so that's it? :) no missing?i think it have...:D
    Tuesday, July 05, 2011 12:58 PM
  • Hey Ajit and MASNSN,

           jaevonheart Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals is asking whether he has written the code in right way or not. check for the subject line.

    Am I right jaevonheart Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals


    Mark Answered, if it solves your question
    Rohit Arora
    Tuesday, July 05, 2011 12:59 PM
  • Code looks fine, maybe some work to do.

    Try this code:

    int number = 0;
          Console.WriteLine("Enter size of index array:");
          if (int.TryParse(Console.ReadLine(), out number))
          {
            int[] array = new int[number];
            for (int i = 0; i < number; i++)
            {
              Console.WriteLine("Enter a number for {0}. number, {1} remaining:", i.ToString(), (number - (i)).ToString());
              int indexNumber = 0;
              if (int.TryParse(Console.ReadLine(), out indexNumber))
              {
                array[i] = indexNumber;
              }
            }
            Console.WriteLine("---------");
            Console.WriteLine("This is it, now let show the result:");
            foreach (int item in array)
              Console.WriteLine("{0}", item);       
          }
          Console.ReadLine();
    



    Mitja
    Tuesday, July 05, 2011 1:15 PM