// See https://aka.ms/new-console-template for more information Console.WriteLine("Chapter 8"); Console.WriteLine("========="); // Arrays in C# // Types of Arrays: Single, Multidimensional "matrix", Jagged "Array of Arrays", // Syntax for Arrays //// Single type[] ex. string[] int[] bool[] //// Multi or Matrix or Rectangular type[,] string[,] int[,] //// Example of 2 Dim Matrix string[,] [row,col] //// Example of 3 Dim Matrix string[,,] //// Jagged or "Array of Arrays" type[][] ex. string[][] /// // Single Dim Array // linear or series array // page 230 // elements // type: integer int Array [] // name: ages // assignment = // instance "new instance of an object in memory" new keyword // object in memory will store 5 integer elements int[5] // length 5 // positional index 0-4 int[] ages = new int[5]; // static value ages[0] = 21; // user input Console.WriteLine("Enter Contact Age: "); string i_age; i_age = Console.ReadLine(); ages[1] = Convert.ToInt32(i_age); // variable int age = 52; ages[2] = age; ages[3] = 42; ages[4] = 48; Console.WriteLine(ages); // print the array type // Loop thru the Array // "for x in ys" local variable scope "x" collection "ys" // foreach keyword foreach (int v_age in ages) { Console.WriteLine(v_age); // local variable defined in the scope // v_age } // foreach // Multi Dimensional Arrays / Rectangular page 239 // columns / fields / members //// sku, cost, price // rows "count to rows in the dataset before creating the object in memory int dataRows = 2; // get 10 from your count // query the count from the excel sheet and assign to dataRows int[,] myMatrix = new int[dataRows, 3]; // [2,3] // table myMatrix[0, 0] = 4581; // sku 4581 myMatrix[0, 1] = 12; // Cost of sku 4581 12 myMatrix[0, 2] = 25; // Price of sku 4581 25 myMatrix[1, 0] = 4582; // sku row 2 myMatrix[1, 1] = 18; // cost row 2 myMatrix[1, 2] = 36; // price row 2 int[,] myMatrix1 = { { 5875, 10, 18 }, // row[0] 3 cols { 5878, 12, 25 } // row[1] 3 cols }; // Looping thru multi dimensional arrays // for keyword for (int x = 0; x < myMatrix.GetLength(0); x++) // row ?? [ [0],[1] ] { for (int y = 0; y < myMatrix.GetLength(1); y++) { // value of something Console.WriteLine(myMatrix[x, y] + " "); } // for y Console.WriteLine(); } // for x