Advertisement
Guest User

matrix and functions

a guest
Apr 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         int[] arr = {1,2,3}; //same: int[] arr = new int[] {1,2,3};
  8.         int[,] mat = new int[3, 3];
  9.         int[][] jagged_arr = new int[3][];
  10.         int[][] jagged_arr2 = {
  11.             new int[] {1, 2, 3, 4},
  12.             new int[] {11, 34, 67},
  13.             new int[] {89, 23},
  14.             new int[] {0, 45, 78, 53, 99}
  15.         };
  16.        
  17.         mat = fill_mat(mat);
  18.         Print2DArray(mat);
  19.        
  20.         jagged_arr = fill_jag(jagged_arr);
  21.         PrintJaggedArray(jagged_arr);
  22.         PrintJaggedArray(jagged_arr2);
  23.        
  24.         //3D array
  25.         Console.WriteLine("\n3D array");
  26.         int[, ,] threeDimensional = new int[3, 5, 4];
  27.        
  28.         //fill all the layers
  29.         for(int x = 0; x < threeDimensional.GetLength(0); x++)
  30.             for (int y = 0; y < threeDimensional.GetLength(1); y++)
  31.                 for(int layer = 0; layer < threeDimensional.GetLength(2); layer++)
  32.                     threeDimensional[x, y, layer] = layer+1;
  33.        
  34.  
  35.         //print all the layers
  36.         for (int i = 0; i < threeDimensional.GetLength(2); i++)
  37.         {
  38.             for (int y = 0; y < threeDimensional.GetLength(1); y++)
  39.             {
  40.                 for (int x = 0; x < threeDimensional.GetLength(0); x++)
  41.                     Console.Write(threeDimensional[x, y, i]);
  42.                 Console.WriteLine();
  43.             }
  44.             Console.WriteLine();
  45.         }
  46.     } // end
  47.    
  48.     static int[,] fill_mat(int[,] mat){
  49.        
  50.         mat = new int[,] {
  51.             {1,2,3},
  52.             {3,4,5},
  53.             {5,6,7}
  54.         };
  55.        
  56.         return mat;
  57.     }
  58.    
  59.     static void Print2DArray(int[,] arr)
  60.     {
  61.         Console.WriteLine("\nMatrix:");
  62.        
  63.         // Display the array elements.
  64.         for (int i = 0; i < arr.GetLength(0); i++)
  65.             for (int j = 0; j < arr.GetLength(1); j++)
  66.                 Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
  67.     }
  68.    
  69.     static int[][] fill_jag(int[][] arr)
  70.     {
  71.         arr[0] = new int[] {1, 2, 3, 4};
  72.         arr[1] = new int[] {1, 2, 3, 4};
  73.         arr[2] = new int[] {1, 2, 3, 4};
  74.        
  75.         return arr;
  76.     }
  77.    
  78.     static void PrintJaggedArray(int[][] arr)
  79.     {
  80.         Console.WriteLine("\nJagged:");
  81.        
  82.         for (int n = 0; n < arr.Length; n++)
  83.         {
  84.             // Print the row number
  85.             System.Console.Write("Row({0}): ", n);
  86.  
  87.             // Print the elements in the row
  88.             for (int k = 0; k < arr[n].Length; k++)
  89.                 System.Console.Write("{0} ", arr[n][k]);
  90.            
  91.             System.Console.WriteLine();
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement