2

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();
}
Luaan
  • 62,244
  • 7
  • 97
  • 116

4 Answers4

6

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);
        }
 }
Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
  • sir again its showing Array does not have that many dimensions. – user3265015 Feb 04 '14 at 10:22
  • Declare array like this, string[][] a = new string[100][100]; – Adil Feb 04 '14 at 10:24
  • More importantly, `a.GetUpperBound(1)` can't ever give any meaningful value in a jagged array - the array `a` doesn't *have* two dimensions, so it will always throw. – Luaan Feb 04 '14 at 10:53
0

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

Monah
  • 6,714
  • 6
  • 22
  • 52
0

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));
 }
}
Luaan
  • 62,244
  • 7
  • 97
  • 116
0

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);
    }
}
doctorlove
  • 18,872
  • 2
  • 46
  • 62