Dave Braunschweig
Arrays
// This program demonstrates array processing, including: // display, total, max, min, parallel arrays, sort, // fixed arrays, dynamic arrays, and multidimensional arrays. using System; using System.Collections.Generic; class Arrays { public static void Main (string[] args) { String[] names = {"Lisa", "Michael", "Ashley", "Jacob", "Emily"}; int[] ages = {49, 48, 26, 19, 16}; DisplayArray(ages); int total = CalculateSum(ages); int maximum = CalculateMaximum(ages); int minimum = CalculateMinimum(ages); Console.WriteLine("total: " + total); Console.WriteLine("maximum: " + maximum); Console.WriteLine("minimum: " + minimum); DemonstrateParallelArrays(names, ages); System.Array.Sort(ages); DisplayArray(ages); DemonstrateFixedArray(); DemonstrateDynamicArray(); DemonstrateMultidimensionalArray(); } public static void DisplayArray(int[] array) { for (int index = 0; index < array.Length; index++) { Console.WriteLine("array[" + index + "] = " + array[index]); } } public static int CalculateSum(int[] array) { int total = 0; for (int index = 0; index < array.Length; index++) { total += array[index]; } return total; } public static int CalculateMaximum(int[] array) { int maximum = array[0]; for (int index = 1; index < array.Length; index++) { if (maximum < array[index]) { maximum = array[index]; } } return maximum; } public static int CalculateMinimum(int[] array) { int minimum = array[0]; for (int index = 1; index < array.Length; index++) { if (minimum > array[index]) { minimum = array[index]; } } return minimum; } public static void DemonstrateParallelArrays(String[] names, int[] ages) { for (int index = 0; index < names.Length; index++) { Console.WriteLine(names[index] + " is " + ages[index] + " years old"); } } public static void DemonstrateFixedArray() { int[] array = new int[5]; Random random = new Random(); for (int index = 0; index < array.Length; index++) { int number = random.Next(0, 100); array[index] = number; } DisplayArray(array); } public static void DemonstrateDynamicArray() { List<int> array = new List<int>(); Random random = new Random(); for (int index = 0; index < 5; index++) { int number = random.Next(0, 100); array.Add(number); } for (int index = 0; index < array.Count; index++) { Console.WriteLine("array[" + index + "] = " + array[index]); } } public static void DemonstrateMultidimensionalArray() { String[,] game = new String[,] { {"X", "O", "X"}, {"O", "O", "O"}, {"X", "O", "X"} }; for (int row = 0; row < 3; row++) { for (int column = 0; column < 3; column++) { Console.Write(game[row, column]); if (column < 2) { Console.Write(" | "); } } Console.WriteLine(); } } }
Output
array[0] = 49 array[1] = 48 array[2] = 26 array[3] = 19 array[4] = 16 total: 158 maximum: 49 minimum: 16 Lisa is 49 years old Michael is 48 years old Ashley is 26 years old Jacob is 19 years old Emily is 16 years old array[0] = 16 array[1] = 19 array[2] = 26 array[3] = 48 array[4] = 49 array[0] = 65 array[1] = 45 array[2] = 78 array[3] = 32 array[4] = 4 array[0] = 24 array[1] = 62 array[2] = 97 array[3] = 40 array[4] = 82 X | O | X O | O | O X | O | X