user3265015
user3265015

Reputation: 25

Index out of bounds in c# while using this code

If i am having 1 to 100 digits in which i should get the output of

1--100 
2--99
 3--98 
.  
.. 
.. 
49---50 

the code is below its giving index out of bound ,arrays don't have to many dimensions

static void Main(string[] args)
{
    //// A. 2D array of strings.
    string[][] a = new string[100][];

    int bound0 = a.GetUpperBound(0);
    int bound1 = a.GetUpperBound(1);
    for (int i = 0; i <= bound0; i++)
    {
        for (int x = 100; x <= bound1; x--)
        {
            string s1 = a[i][x];
            Console.WriteLine(s1);
        }
    }
    Console.WriteLine();
    Console.ReadKey();
}

Upvotes: 2

Views: 1922

Answers (4)

Adil
Adil

Reputation: 148120

You need to give the second dimension for array. In the inner loop you are decrement the loop variable instead of increment that also results in out of bound exception. You probably need to know the difference between jagged and two dimensional array. The post would explain that. This statement int bound1 = a.GetUpperBound(1); gives the exception as the second dimension is not yet declared.

Using jagged array.

string[][] a = new string[100][];
int bound0 = a.GetUpperBound(0);
for(int i = 0; i <= bound0; i++)
a[i] = new string[3];

for (int i = 0; i <= bound0; i++)
{
        int bound1 = a[i].GetUpperBound(0);
        for (int x = 0; x <= bound1; x++)
        {
            a[i][x] = (i + x).ToString();
            string s1 = a[i][x];
            Console.WriteLine(s1);
        }
 }

Using two dimensional array.

string[,] a = new string[100,4];
int bound0 = a.GetUpperBound(0);
int bound1 = a.GetUpperBound(1);
for (int i = 0; i < bound0; i++)
{
    for (int x = 0; x < bound1; x++)
    {
        a[i,x] = (i+x).ToString();
        string s1 = a[i,x];
        Console.WriteLine(s1);
    }
}
Console.WriteLine();
Console.ReadKey();

Edit, based on updates

string[][] a = new string[100][];
int bound0 = a.GetUpperBound(0);
for(int i = 0; i <= bound0; i++)
a[i] = new string[100];

for (int i = 0; i <= bound0; i++)
{
        int bound1 = a[i].GetUpperBound(0);
        for (int x = bound1; x >= 0; x--)
        {
            a[i][x] = (i+1).ToString() +"--"+ (x+1).ToString();
            string s1 = a[i][x];
            Console.WriteLine(s1);
        }
 }

Upvotes: 6

doctorlove
doctorlove

Reputation: 19242

I believe you just want the 2nd loop to run while x is greater then the bound:

for (int i = 0; i <= bound0; i++)
{
    //                  | change here from <= to >=
    for (int x = 100; x >= bound1; x--)
    {
        string s1 = a[i][x];
        Console.WriteLine(s1);
    }
}

Upvotes: 0

Luaan
Luaan

Reputation: 63732

You're not creating a 2D array of strings. What you're creating is an array of string arrays. This means that the "inner" array can have any length whatsoever, and the "outside" array of course has no idea about that. So, either create the array as 2D (new string[100, something]) or ask for the upper bound later. A nicer code would be this:

string[][] a = new string[100][];

// Don't forget to create all the subarrays, eg.:
// for (int i = 0; i < a.Length; i ++) a[i] = new string[10];

for (int i = 0; i < a.Length; i++)
{
  for (int j = 0; j < a[i].Length; j++)
  {
    Console.WriteLine(a[i][j]);
  }
}

In any case, if what you're saying you want to do is true, you're doing this in a completely crazy way. Why not do something like this?

void PrintDigitPairs(int lowerBound, int upperBound)
{
 for (int i = lowerBound; i <= lowerBound + upperBound / 2; i++)
 {
   Console.WriteLine(i + "-" + (upperBound - i + 1));
 }
}

Upvotes: 0

Monah
Monah

Reputation: 6784

to use the a.GetUpperBound(1) => you have defined your 2nd dimension of your array which you didn't also you are decremented x ( x--) in the inner for loop

below is a working sample for your code, but the result will be empty strings since you didn't initialize the array

static void Main(string[] args)
{
    //// A. 2D array of strings.
    string[,] a = new string[100, 4];

    int bound0 = a.GetUpperBound(0);
    int bound1 = a.GetUpperBound(1);
    for (int i = 0; i <= bound0; i++)
    {
         for (int x = 0; x <= bound1; x++)
         {
               string s1 = a[i, x];
               Console.WriteLine(s1);
         }
    }
    Console.WriteLine();
    Console.ReadKey();
}

regards

Upvotes: 0

Related Questions