Module 8 Multidimensional Array Thanawin Rakthanmanon Email fengtwrku

  • Slides: 18
Download presentation
Module 8 Multi-dimensional Array Thanawin Rakthanmanon Email: fengtwr@ku. ac. th Create by: Aphirak Jansang

Module 8 Multi-dimensional Array Thanawin Rakthanmanon Email: fengtwr@ku. ac. th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND 1 nd Semester 200 7 1

Outlines Array Review ¡ Multi-dimensional Array ¡ 1 nd Semester 200 7 2

Outlines Array Review ¡ Multi-dimensional Array ¡ 1 nd Semester 200 7 2

Do you remember how to declare Array? ¡ Array for keeping 50 student's names

Do you remember how to declare Array? ¡ Array for keeping 50 student's names l ¡ Array for keeping 50 student's IDs l ¡ string [ ] NAME = new string[50]; string [ ] ID = new string[50]; Array for keeping 50 student’s GPAs l double[ ] GPA = new double[50]; 1 nd Semester 200 7 3

examples int [] score = new int[6]; score 0 1 2 3 4 5

examples int [] score = new int[6]; score 0 1 2 3 4 5 int [] score = new int[4] {5, 6, 7, 9}; score 1 nd Semester 200 7 0 5 1 6 2 7 3 9 4

Accessing Array Elements Supply an integer index for accessing array elements ¡ indexes are

Accessing Array Elements Supply an integer index for accessing array elements ¡ indexes are zero-based ¡ int [] score = new int[4]; 0 score -3 1 2 4 3 7 score[0] = -3; score[2+1] = 7; Score[3 -1] = score[0]+score[3] Console. Write. Line(score[2]); 1 nd Semester 200 7 4 5

How to find Array’s length? ¡ By property Length int [] matrix = new

How to find Array’s length? ¡ By property Length int [] matrix = new int[5]; Console. Write. Line(matrix. Length); ¡ 5 By method Get. Length() 7 int [] matrix = new int[7]; Console. Write. Line(matrix. Get. Length(0)); 1 nd Semester 200 7 6

How to access Array's member ? ¡ Displaying all members in Array ‘SCORE’ for

How to access Array's member ? ¡ Displaying all members in Array ‘SCORE’ for (int i = 0; i< SCORE. Length; i++) { Console. Write. Line(SCORE[i]); } ¡ Finding summation of all members in Array ‘SCORE’ double sum = 0; for (int i = 0; i< SCORE. Length; i++) { sum = sum + SCORE. Length; } 1 nd Semester 200 7 7

Looping or Iteration in C# while for Iteration foreach do…while 1 nd Semester 200

Looping or Iteration in C# while for Iteration foreach do…while 1 nd Semester 200 7 8

foreach is used to access all elements in an array and do some action

foreach is used to access all elements in an array and do some action repeatedly ¡ foreach = “for each element in an array” ¡ Syntax ¡ foreach (var_declaration in array_name) statement // the above statement will be applied to each element in an array 1 nd Semester 200 7 9

foreach example string s = Console. Read. Line(); int count = 0; for (int

foreach example string s = Console. Read. Line(); int count = 0; for (int i = 0; i < s. Length; i++){ if(s[i] == 'A') count++; } string s = Console. Read. Line(); int count = 0; foreach (char c in s) { if (c == 'A') count++; } 1 nd Semester 200 7 10

Outlines Array Review ¡ Multi-dimensional Array ¡ 1 nd Semester 200 7 11

Outlines Array Review ¡ Multi-dimensional Array ¡ 1 nd Semester 200 7 11

Multi-dimensional Array’s declaration ¡ Syntax: <type> [ , ] <name>; <type> [ , ,

Multi-dimensional Array’s declaration ¡ Syntax: <type> [ , ] <name>; <type> [ , , ] <name>; ¡ 2 dimensions 3 dimensions Creation: <name> = new <type>[<dim 1>, <dim 2>]; 2 dimensions 1 nd Semester 200 7 12

Array Declaration Example ¡ ¡ int [ , ] grid 2; grid 2 =

Array Declaration Example ¡ ¡ int [ , ] grid 2; grid 2 = new int [3, 2]; int [ , , ] grid 3; grid 3 = new int [3, 2, 4]; 1 nd Semester 200 7 13

More examples 0 0 1 1 nd 1 2 5 4 3 2 1

More examples 0 0 1 1 nd 1 2 5 4 3 2 1 0 grid[1, 1] == ? grid[1, 2] == ? grid[0, 2] == ? new int[2, 3] grid. Get. Length(0) == ? grid. Get. Length(1) == ? grid. Length == ? Semester 200 7 14

How to find Array’s #dimension? ¡ By property Rank 1 int [] matrix =

How to find Array’s #dimension? ¡ By property Rank 1 int [] matrix = new int[5]; Console. Write. Line(matrix. Rank); int [, ] matrix = new int[5, 2]; Console. Write. Line(matrix. Rank); 2 int [, , ] matrix = new int[5, 2, 3]; Console. Write. Line(matrix. Rank); 3 1 nd Semester 200 7 15

Example 1 ¡ Writing a program gets 5 student’s name and score. After that

Example 1 ¡ Writing a program gets 5 student’s name and score. After that displaying student’s name and score who get score less than 50 points. 1 nd Semester 200 7 16

Example 2 ¡ Write program for find result of matrix A + matrix B

Example 2 ¡ Write program for find result of matrix A + matrix B #matrix size = 3 x 3 1 nd Semester 200 7 17

Example 3 ¡ Write program for converting number in base 10 to base 16

Example 3 ¡ Write program for converting number in base 10 to base 16 1 nd Semester 200 7 18