1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
1 st Semester /25 Outlines C# Review Array Overview Array Usage Examples
1 st Semester /25 C# Structure – Multiple MethodsNamespace Class Main() Method1() Method2() Method3() MethodNN()
1 st Semester /25 Method Declaration static ( ) { ; } #remark return-type can be - data type = int, double, string, … need return statement need return statement - void = return no value
1 st Semester /25 Outlines C# Review Array Overview Array Usage Examples
1 st Semester /25 What is Array? “An array is an indexed collection of objects, all of the same type”
1 st Semester /25 Why Array? We want to store all the midterm scores of all students in a class with 60 students. int s1, s2, s3, s4, s5; int s6, s7, s8, s9, s10; int s11, s12, s13, s14, s15; int s16, s17, s18, s19, s20;... int s56, s57, s58, s59, s60; Declaration without Array int [] s = new int[60]; Declaration with Array
1 st Semester /25 Array make our life easier We want to find the mean of the scores. int total=0, mean; total = s1 + s2 + s3 + … + s60; mean = total / 60; Coding without Array int total=0, mean; for (int i = 0; i<60; i++) total = total + s[i]; total = total + s[i]; mean = total / 1000; Coding with Array
1 st Semester /25 Outlines C# Review Array Overview Array Usage Examples
1 st Semester /25 ARRAY One Dimensional Array Multi-dimensional Array
1 st Semester /25 Array Declaration Syntax: Declaration only Declaration with creation Declaration with initialization: 2 ways [] ; [] = new [ ]; [] = { }; 2 [] = new [ ] { }; 1
1 st Semester /25 Array Declaration Example int [] ai; int [] ai = new int[5]; int [] ai = new int[5] {1, 2, 3, 4, 5}; int [] ai = {1, 2, 3, 4};
1 st Semester /25 score 5679 score More examples int [] score = new int[6]; int [] score = new int[4] {5, 6, 7, 9}; 102 3
1 st Semester /25 Accessing Array Elements Supply an integer index for accessing array elements indexes are zero-based int [] score = new int[4]; 1023score score[0] = -1; score[3] = 5; Console.WriteLine(score[3]); 5
1 st Semester /25 How to find Array’s length? By property Length By method GetLength() int [] matrix = new int[5]; Console.WriteLine(matrix.Length); int [] matrix = new int[5]; Console.WriteLine(matrix.GetLength(0));
1 st Semester /25 foreach statement int [] matrix = new int[5]; …… Accessing matrix with for loop for (int i=0; i<5; i++) Console.WriteLine(matrix[i]); Console.WriteLine(matrix[i]); Accessing matrix with foreach loop foreach (int s in matrix) Console.WriteLine(s); Console.WriteLine(s);
1 st Semester /25 Break 5 Minutes
1 st Semester /25 ARRAY One Dimensional Array Multi-dimensional Array
1 st Semester /25 Multi-dimensional Arrays Declaration Syntax: [,] ; [,,] ; Creation: = new [, ]; 2 dimensions 3 dimensions 2 dimensions
1 st Semester /25 Array Declaration Example int [, ] grid2; grid2 = new int [3,2]; int [,, ] grid3; grid3 = new int [3, 2, 4];
1 st Semester /25 More examples new int[2, 3] grid[1,1] == ? grid.GetLength(0) == ? grid.GetLength(1) == ? grid.Length == ?
1 st Semester /25 How to find Array’s #dimension? By property Rank int [] matrix = new int[5]; Console.WriteLine(matrix.Rank); int [,,] matrix = new int[5,2,3]; Console.WriteLine(matrix.Rank); int [,] matrix = new int[5,2]; Console.WriteLine(matrix.Rank);
1 st Semester /25 Outlines C# Review Array Overview Array Usage Examples
1 st Semester /25 Example 1 Write program for find result of matrix A + matrix B #matrix size = 3x3
1 st Semester /25 Example 2 Write program for converting number in base 10 to base 16
1 st Semester /25 Example 3 Write a program for calculating summation of 100 student's scores