0

I have a matrix. And I need to get 1D arrays from my matrix. For example, I have follow matrix:
123
456
789
So it looks like 3 arrays: 147, 258, 369. But I got "Index out of range exception" in this code:

  int[] b = new int[n];
        for (i = 0; i < n; i++)
        {
            b[i] = a[i, n];
            Console.Write(b[i] + " ");
        }
        Console.WriteLine();

Thanx for any help.

Here's full code that works already:

static void Main(string[] args)
    {        
        int n = 0, m = 0, i = 0, j = 0;           

        Random r = new Random();

        Console.WriteLine("Please, input matrix size:");
        Console.Write("\tn = ");

        n = Convert.ToInt32(Console.ReadLine());
        Console.Write("\tm = ");

        m = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine();

        int[,] a = new int[n, m];           

        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {                  
                a[i, j] = r.Next(0, 2);                   
            }                
        }

        showMe(a, n, m);

        Console.WriteLine();
        run(a, n, m);

        Console.ReadKey();

        int[][] b = new int[m][];
        for (i = 0; i < m; i++) 
        { 
            b[i] = new int[n]; 
            for (j = 0; j < n; j++) 
            {
                b[i][j] = a[j, i];
                Console.Write(b[i][j] + " ");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
        Console.ReadKey();
    }
2
  • Can you confirm that the first dimension of a is of size n? It would be helpful to see the definitions of a and n in this example. Commented Feb 15, 2011 at 22:39
  • Do you mean 147, 258, and 369? Commented Feb 15, 2011 at 22:43

3 Answers 3

1

I'll assume n=3 and that it is a square matrix, a[i, n]; will be outside the bounds of a - the largest index you can reference is n-1.

I think what you want is

    int[][] b = new int[m][];
    for (i = 0; i < m; i++)
    {
        b[i] = new int[n];
        for (j = 0; j < n; j++)
        {
            b[i][j] = a[j,i];
        }

    }

b[0] is your first array, b[1] is your second....

Sign up to request clarification or add additional context in comments.

2 Comments

you need to validate that n is less than m before you attempt read the [_,n]th element of the array
Ok, tanx. What do I do wrong? I want to write matrix, press Enter, and get my arrays.
1

Try this:

        int n = 3;
        int[,] a = new int[,] { 
            { 1, 2, 3 }, 
            { 4, 5, 6 }, 
            { 7, 8, 9 } };

        int[] b = new int[n];
        for (int i = 0; i < n; i++)
        {
            b[i] = a[i, n - 1];
            Console.Write(b[i] + " ");
        }
        Console.ReadLine();

This will output 3 6 9 since n is not being changed. Jimmy is correct, a[i,n] needs to be a[i, n-1].

Comments

1

This works for two-dimensional arrays in general.

public static T[][] ToJaggedArray<T>(this T[,] arr)
{
    return Enumerable.Range(0, arr.GetUpperBound(0) + 1)
                     .Select(i => Enumerable.Range(0, arr.GetUpperBound(1) + 1)
                                            .Select(j => arr[i, j])
                                            .ToArray())
                     .ToArray();
}

public static T[][] ToJaggedArrayTranspose<T>(this T[,] arr)
{
    return Enumerable.Range(0, arr.GetUpperBound(1) + 1)
                     .Select(j => Enumerable.Range(0, arr.GetUpperBound(0) + 1)
                                            .Select(i => arr[i, j])
                                            .ToArray())
                     .ToArray();
}

// you'd be interested in ToJaggedArrayTranspose()
var mat = new[,]
{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
};
var arr = mat.ToJaggedArrayTranspose();
// arr === new[][] { new[] {1, 4, 7}, new[] {2, 5, 8}, new[] {3, 6, 9} }

p.s., Always use GetUpperBound() on multi-dimensional arrays to get the length of the dimensions, don't try to guess it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.