Sunday, 29 September 2013

Create a Transpose Method for Converting 2D Array and Jagged Array



Transpose Method :
     
        Transpose method which will convert the rows in to the columns and columns in to the rows in array.In Mathematical notation convert a Matrix A to Transpose  A  -> A pow(T) . In this example we are going to see how to implement a Transpose for 2 dimensional array and Jagged array. 

What is 2 dimensional array ?
Array which have elements in rows and columns are known as 2 dimensional array.the columns in the each row should have same no of elements

array with 2 rows 3 columns

2  3  4
1  4  6

int[,] a=new int[2,3]; 

What is Jagged array ?
Array in which columns of each row need not to be have same no of elements

2  3  4
1  8
3  5  6  8

in the above example you can see each row have different number of elements in columns, first row have 3 elements, 2 row have 2 elements etc.

Lets we see the real implementation :
   
    class TArray<T> where T : struct,IComparable
    {
       internal static T[,] Transpose(T[,] a)
        {
            int row = a.GetUpperBound(0) + 1;
            int col = a.GetUpperBound(1) + 1;
            T[,] tarr = new T[col, row];

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    tarr[j, i] = a[i, j];
                }
            }
            return tarr;
        }    

        internal static Nullable<T>[,] Transpose(T[][] jagged)
        {
            int row = jagged.Length;
            T?[,] temparray = null;
            int col = 0;
            if (row >= 0)
            {
                for (int j = 0; j < row; j++)
                {
                    col = jagged[j].Length > col ? jagged[j].Length : col;
                }
            }
            temparray = new T?[col, row];

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    if (jagged[i].Length > j)
                    {
                        temparray[j, i] = jagged[i][j];
                    }
                    else
                    {
                        temparray[j, i] = default(T?);
                    }
                }
            }
            return temparray;           
        }
    }


 class Program
    {
        static void Main(string[] args)
        {
            int[,] array = new int[1, 3];
            array = new int [,]{ {1,2,3}};

            int [][] jagged=new int [3][];
            jagged[0] = new int[] { 1,2,3};
            jagged[1] = new int[] { 6,7};
            jagged[2] = new int[] { 1,6,3,8};
            Console.WriteLine();
            Console.WriteLine("Before Transpose");
            Console.WriteLine();
            Console.WriteLine("2 Dimensional Array");
            for(int i=0;i<=array.GetUpperBound(0);i++)
            {
                for (int j = 0; j<=array.GetUpperBound(1); j++)
                {
                    Console.Write(string.Format("[{0}]",array[i,j]));
                }
                Console.WriteLine();
            }

            Console.WriteLine();
            Console.WriteLine("Jagged Array");
            for (int i = 0; i <= jagged.GetUpperBound(0); i++)
            {
                for (int j = 0; j < jagged[i].Length; j++)
                {
                    Console.Write(string.Format("[{0}]",jagged[i][j]));
                }
                Console.WriteLine();
            }

            int [,] arrTranspose= TArray<int>.Transpose(array);          
            int?[,] jagTranspose = TArray<int>.Transpose(jagged);

            Console.WriteLine();
            Console.WriteLine("After Transpose");
            Console.WriteLine();
            Console.WriteLine("2 Dimensional Array");
            for (int i = 0; i <= arrTranspose.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= arrTranspose.GetUpperBound(1); j++)
                {
                    Console.Write(string.Format("[{0}]",arrTranspose[i, j]));
                }
                Console.WriteLine();
            }

            Console.WriteLine();
            Console.WriteLine("Jagged Array");
            for (int i = 0; i <= jagTranspose.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= jagTranspose.GetUpperBound(1); j++)
                {
                    if(jagTranspose[i,j].HasValue)
                    Console.Write(string.Format("[{0}]",jagTranspose[i,j].Value));
                    else
                     Console.Write(string.Format("[ ]"));
                }
                Console.WriteLine();
            }


            Console.Read();

        }
    }


Output:

Before Transpose
2 Dimensional Array
[1][2][3]

Jagged Array
[1][2][3]
[6][7]
[1][6][3][8]

After Transpose
2 Dimensional Array
[1]
[2]
[3]

Jagged Array
[1][6][1]
[2][7][6]
[3][ ][3]
[ ][ ][8]


I hope this article will help you to convert the 2d array and jagged array to the transpose.