Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program
- {
- public static void Main()
- {
- int[] arr = {1,2,3}; //same: int[] arr = new int[] {1,2,3};
- int[,] mat = new int[3, 3];
- int[][] jagged_arr = new int[3][];
- int[][] jagged_arr2 = {
- new int[] {1, 2, 3, 4},
- new int[] {11, 34, 67},
- new int[] {89, 23},
- new int[] {0, 45, 78, 53, 99}
- };
- mat = fill_mat(mat);
- Print2DArray(mat);
- jagged_arr = fill_jag(jagged_arr);
- PrintJaggedArray(jagged_arr);
- PrintJaggedArray(jagged_arr2);
- //3D array
- Console.WriteLine("\n3D array");
- int[, ,] threeDimensional = new int[3, 5, 4];
- //fill all the layers
- for(int x = 0; x < threeDimensional.GetLength(0); x++)
- for (int y = 0; y < threeDimensional.GetLength(1); y++)
- for(int layer = 0; layer < threeDimensional.GetLength(2); layer++)
- threeDimensional[x, y, layer] = layer+1;
- //print all the layers
- for (int i = 0; i < threeDimensional.GetLength(2); i++)
- {
- for (int y = 0; y < threeDimensional.GetLength(1); y++)
- {
- for (int x = 0; x < threeDimensional.GetLength(0); x++)
- Console.Write(threeDimensional[x, y, i]);
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- } // end
- static int[,] fill_mat(int[,] mat){
- mat = new int[,] {
- {1,2,3},
- {3,4,5},
- {5,6,7}
- };
- return mat;
- }
- static void Print2DArray(int[,] arr)
- {
- Console.WriteLine("\nMatrix:");
- // Display the array elements.
- for (int i = 0; i < arr.GetLength(0); i++)
- for (int j = 0; j < arr.GetLength(1); j++)
- Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
- }
- static int[][] fill_jag(int[][] arr)
- {
- arr[0] = new int[] {1, 2, 3, 4};
- arr[1] = new int[] {1, 2, 3, 4};
- arr[2] = new int[] {1, 2, 3, 4};
- return arr;
- }
- static void PrintJaggedArray(int[][] arr)
- {
- Console.WriteLine("\nJagged:");
- for (int n = 0; n < arr.Length; n++)
- {
- // Print the row number
- System.Console.Write("Row({0}): ", n);
- // Print the elements in the row
- for (int k = 0; k < arr[n].Length; k++)
- System.Console.Write("{0} ", arr[n][k]);
- System.Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement