Download presentation
Presentation is loading. Please wait.
Published byShawn Webster Modified over 8 years ago
1
1 st Semester 2005 1 Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND
2
1 st Semester 2005 2/25 Outlines C# Review Array Overview Array Usage Examples
3
1 st Semester 2005 3/25 C# Structure – Multiple MethodsNamespace Class Main() Method1() Method2() Method3() MethodNN()
4
1 st Semester 2005 4/25 Method Declaration static ( ) { ; } #remark return-type can be - data type = int, double, string, … need return statement need return statement - void = return no value
5
1 st Semester 2005 5/25 Outlines C# Review Array Overview Array Usage Examples
6
1 st Semester 2005 6/25 What is Array? “An array is an indexed collection of objects, all of the same type” 0 1 3 4 5 6 7
7
1 st Semester 2005 7/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
8
1 st Semester 2005 8/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
9
1 st Semester 2005 9/25 Outlines C# Review Array Overview Array Usage Examples
10
1 st Semester 2005 10/25 ARRAY One Dimensional Array Multi-dimensional Array
11
1 st Semester 2005 11/25 Array Declaration Syntax: Declaration only Declaration with creation Declaration with initialization: 2 ways [] ; [] = new [ ]; [] = { }; 2 [] = new [ ] { }; 1
12
1 st Semester 2005 12/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};
13
1 st Semester 2005 13/25 score 5679 score More examples int [] score = new int[6]; 102 345 int [] score = new int[4] {5, 6, 7, 9}; 102 3
14
1 st Semester 2005 14/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
15
1 st Semester 2005 15/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));
16
1 st Semester 2005 16/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);
17
1 st Semester 2005 17/25 Break 5 Minutes
18
1 st Semester 2005 18/25 ARRAY One Dimensional Array Multi-dimensional Array
19
1 st Semester 2005 19/25 Multi-dimensional Arrays Declaration Syntax: [,] ; [,,] ; Creation: = new [, ]; 2 dimensions 3 dimensions 2 dimensions
20
1 st Semester 2005 20/25 Array Declaration Example int [, ] grid2; grid2 = new int [3,2]; int [,, ] grid3; grid3 = new int [3, 2, 4];
21
1 st Semester 2005 21/25 More examples new int[2, 3] 5 432 1 0 grid[1,1] == ? grid.GetLength(0) == ? grid.GetLength(1) == ? grid.Length == ?
22
1 st Semester 2005 22/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);
23
1 st Semester 2005 23/25 Outlines C# Review Array Overview Array Usage Examples
24
1 st Semester 2005 24/25 Example 1 Write program for find result of matrix A + matrix B #matrix size = 3x3
25
1 st Semester 2005 25/25 Example 2 Write program for converting number in base 10 to base 16
26
1 st Semester 2005 26/25 Example 3 Write a program for calculating summation of 100 student's scores
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.