// 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.
Loading
Jignesh TrivediPosted Jul 10, 2013, 11:04 PM
in .net, any array index is start from 0 so, your for loop must be 1 point less than number you are entering.
for (int i = 0; i <= n; i++)
{
a[i] = Console.Read();
}
instead of this
for (int i = 0; i < n; i++)
{
a[i] = Console.Read();
}
hope this will help you.
Posted Jul 10, 2013, 9:03 PM
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n, i;
Console.Write("Enter size of the array ");
n = int.Parse(Console.ReadLine());
int[] a = new int[n];
Console.WriteLine("You can enter " + n + " numbers in an array");
for (i = 0; i < a.Length; i++)
a[i] = int.Parse(Console.ReadLine());
Console.Write("Array elements ");
foreach (int x in a)
Console.Write("{0} ", x);
Console.ReadKey();
}
}
}
In future please display your problem in C# Language website not in General website.
amit kaushalPosted Jul 10, 2013, 8:30 AM
1) "System.IndexOutOfRangeException" occurred.
2) Additional Information: index was outside the bounds of the array.
Pankaj RawatPosted Jul 10, 2013, 7:40 AM
int n;
Here u are using an unassigned integer value. // means n contain no value //
int[] a = new int[n];
Here u are using unassigned integer to declare the array size(array initialization)
for (int i = 0; i <= n; i++)
Here suppose if n=5,then the loop must run from i=0 to i=4 or i=1 to i=5. (for IndexOutOfRangeException).
static void Main(string[] args)
{
int n = 0;
Console.WriteLine("enter the size of an array");
n = int.Parse(Console.ReadLine());
int[] a = new int[n];
Console.WriteLine("you can enter " + n + " numbers in an array");
for (int i = 0; i
a[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i< n; i++)
{
Console.WriteLine(a[i]);
}
Console.ReadLine();
}
Jignesh TrivediPosted Jul 10, 2013, 7:30 AM
hi,
I have made some small change in your code and it working
try
int n;
int[] a;
Console.WriteLine("enter the size of an array");
n = int.Parse(Console.ReadLine());
a = new int[n];
Console.WriteLine("you can enter " + n + " numbers in an array");
for (int i = 0; i < n; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < n; i++)
{
Console.WriteLine(a[i]);
}
Console.ReadLine();
hope this will help you.