Sunday 31 May 2015

Transpose a 2 Dimensional Array in c#

In this post we are going to see how to Transpose a 2 Dimensional array with out restriction in the Type.Now we can see the generic logic to implement this.

Program.cs

           int[,] count = new int[3, 3]
            {
                {1,2,3},
                {4,5,6},
                {8,3,1}
            };

            Console.WriteLine("\n*************************\n");
            count.PrintMatrix();

            Console.WriteLine("\n********  Transpose a Matrix **********\n");
            int [,] Transpose = Algorithm.TransposeMatrix(count);

            Transpose.PrintMatrix();

            string[,] wCount = new string[2, 3] {
              {"Algo","Extn","Where"},
              {"Cv","What","Sample"}
            };

            Console.WriteLine("\n*************************\n");
            wCount.PrintMatrix();

            string[,] Tword = Algorithm.TransposeMatrix(wCount);
            Console.WriteLine("\n********    Transpose a Matrix   *********\n");

            Tword.PrintMatrix();




Logic 

    public class Algorithm
    {          
        public static T[,] TransposeMatrix<T>(T[,] matrix )
        {           
            int dimension = matrix.Rank;
            int rows = matrix.GetLength(0);
            int columns = matrix.GetLength(1);
           
            T[,] Tmatrix= new T[columns,rows];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    Tmatrix[j, i] = matrix[i, j];
                }
            }

            return Tmatrix;
        }
    }


    public static class Extension
    {
        public static void PrintMatrix<T>(this T[,] matrix)
        {
            int rows = matrix.GetLength(0);
            int columns = matrix.GetLength(1);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    Console.Write(matrix[i, j] + "\t");
                }
                Console.WriteLine(Environment.NewLine);
            }

        }
    } 



Output





From this post you can see how to Transpose a two Dimensional Array.

Reverse a integer with out casting the values in C#

In this post we are going to see how to reverse a integer with out casting the values, you can ask why we need this kind of 10 lines of code to do rather than two of casting and reverse the string, Normally Casting takes huge performance issue, for small operation it doesn't see as Big , for Big operations it Hit the performance of the applications.

Here we are 
1. Initialize a variable result with 0;
2.  Diving and module a value with 10, 
3. Assign the quotient to value to reset the original value
4. Then the result of module is added with previous operation result and multiply by 10.
4.  loop the process 2 to 4 until the quotient 0

Program.cs

            int num = 123456;
            num = Algorithm.ReverseInteger(num);


Output : now num value is 654321

Logic :
        public static int ReverseInteger(int value)
        {
            if(value<0)
            return value;

            int result = 0;
           
            while(true){
                Tuple<int,int> temp = CheckModule(value);
                value = temp.Item1;
                if(temp.Item1==0){
                  result =  result+temp.Item2;
                    break;
                }
                else
                    result = (result + temp.Item2 ) * 10;
            }
            return result;
        }


        private static Tuple<int, int> CheckModule(int value)
        {
            return Tuple.Create<int, int>(value / 10, value % 10);
        }      
    

From this post you can see how to reverse a string with out casting.