• Top Posts

    2-D array arr to the method


    • passing an 2-D array arr to the method transpose which gave the transpose of the matrix. GetLength()  method is used for the count the total number of elements in a particular dimension.


      class _2DasArgument  
       {  
         static int temp = 0;  
         static void transpose(int[,] arr)  
         {  
           for (int i = 0; i < arr.GetLength(0); i++)  
           {  
             for (int j = i; j < arr.GetLength(1); j++)  
             {  
               temp = arr[i, j];  
               arr[i, j] = arr[j, i];  
               arr[j, i] = temp;  
             }  
           }  
         }  
         // to display the transposed matrix   
         static void displayresult(int[,] arr)  
         {  
           Console.WriteLine("Matrix After Transpose: ");  
           for (int i = 0; i < arr.GetLength(0); i++)  
           {  
             for (int j = 0; j < arr.GetLength(1); j++)  
               Console.Write(arr[i, j] + " ");  
             Console.WriteLine();  
           }  
         }  
         static public void Main()  
         {  
           // declaration of an 2-d array   
           int[,] arr;  
           // initialzing 2-D array   
           // matrix of 4 rows and 4 colums   
           arr = new int[4, 4]{ { 1, 2, 3, 4},  
                    { 5, 6, 7, 8},  
                    {9, 10, 11, 12},  
                    {13, 14, 15, 16} };  
           Console.WriteLine("Matrix Before Transpose: ");  
           for (int i = 0; i < arr.GetLength(0); i++)  
           {  
             for (int j = 0; j < arr.GetLength(1); j++)  
               Console.Write(arr[i, j] + " ");  
             Console.WriteLine();  
           }  
           Console.WriteLine();  
           // calling transpose method   
           transpose(arr);  
           // calling displayresult method   
           // to display the result   
           displayresult(arr);  
           Console.ReadLine();  
         } 
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728