amit kaushal

amit kaushal

  • NA
  • 3
  • 1.4k

error in code

Jul 10 2013 5:55 AM
// i want to enter numbers in the array by taking size of array as input from user and then allowing user to enter nos. in the array//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            int[] a = new int[n];
            Console.WriteLine("enter the size of an array");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("you can enter "+n+ " numbers in an array");
            for (int i = 0; i <= n; i++)
            {
                a[i] = Console.Read();
            }
            for (int i = 0; i <= n; i++)
            {
                Console.WriteLine(a[i]);
            }
            Console.ReadLine();
        }
    }
}


i am not able to run this code also not able to understand the errors which are as follows:-
1) use of unassigned local variable n.
2) System.IndexOutOfRangeException.
3) index was outside the bounds of the array

i want to know the meaning of each error and why they are coming.

Answers (5)