Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 8 & 9 Method :Part I & Array :Part II

Similar presentations


Presentation on theme: "Module 8 & 9 Method :Part I & Array :Part II"— Presentation transcript:

1 Module 8 & 9 Method :Part I & Array :Part II
Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

2 Outlines Array Review Multi-dimensional Array Review Method Overview

3 Array examples 1 2 3 4 5 score 1 2 3 score 5 6 7 9
review Array examples int [] score = new int[6]; 1 2 3 4 5 score int [] score = new int[4] {5,6,7,9}; 1 2 3 score 5 6 7 9

4 Accessing Array Elements
review Accessing Array Elements Supply an integer index for accessing array elements indexes are zero-based int [] score = new int[4]; 1 2 3 score -3 7 score[0] = -3; score[3] = 7; Console.WriteLine(score[3]); 7

5 How to find Array’s length?
review How to find Array’s length? By property Length 4 int [] matrix = new int[4]; Console.WriteLine(matrix.Length); By method GetLength() 7 int [] matrix = new int[7]; Console.WriteLine(matrix.GetLength(0));

6 foreach foreach = “for each element in an array” Syntax:
review foreach 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

7 foreach example string s = Console.ReadLine(); int count = 0;
review foreach example string s = Console.ReadLine(); int count = 0; for (int i = 0; i < s.Length; i++){ if(s[i] == 'A') count++; } string s = Console.ReadLine(); int count = 0; foreach (char c in s) { if (c == 'A') count++; }

8 Outlines Array Review Multi-dimensional Array Method Overview

9 Multi-dimensional Array’s declaration
Syntax: <type> [ , ] <name>; <type> [ , , ] <name>; Creation: <name> = new <type>[<dim1>,<dim2>]; 2 dimensions 3 dimensions 2 dimensions

10 Array Declaration Example
int [ , ] grid2; grid2 = new int [3,2]; int [ , , ] grid3; grid3 = new int [3, 2, 4];

11 More examples 4 3 5 2 1 grid[1,1] == ? grid[1,2] == ? grid[0,2] == ?
1 5 4 3 2 1 grid[1,1] == ? grid[1,2] == ? grid[0,2] == ? grid.GetLength(0) == ? grid.GetLength(1) == ? grid.Length == ? 1 3 new int[2, 3] 2 3 6

12 How to find Array’s #dimension?
By property Rank int [] matrix = new int[5]; Console.WriteLine(matrix.Rank); 1 int [,] matrix = new int[5,2]; Console.WriteLine(matrix.Rank); 2 int [,,] matrix = new int[5,2,3]; Console.WriteLine(matrix.Rank); 3

13 Outline Method Overview Using Method Parameter Passing in Method
Pass by value Pass by reference

14 a function/procedure owned by class.
What is Method? Method is a function/procedure owned by class. System.Console.WriteLine(“Hello Kitty!!!”);

15 Pre-defined Method in C#
Console.WriteLine(”Hello, Kitty”); double j = Math.Sqrt(4); int N = int.Parse(”7”); Console.WriteLine(”{0} = {1} + {2}”, 5, 3, 2);

16 How to call method in C#? Non-Returned Value - Procedure
Console.WriteLine(“Hello, Kitty!!!”); Returned Value – Function J = Math.Max(5, 20); Parameter Method Name Class Name Parameter Variable Class Name Method Name

17 Method Idea name age width high DispAge CalArea area
Return no value (void) Return Value

18 Method Example 1 4 1 3 2

19 Outline Method Overview Using Method Parameter Passing in Method
Pass by value Pass by reference

20 Using User-Defined Method
1.Define Method 2.Call Method

21 Define Method in C# #remark return-type can be
static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } #remark return-type can be data type = int, double, string, … need return statement void = return no value

22 Method Declaration Example
static void displaybox() { } static int calage() { } static int calbox(int x,y;) { }

23 Example1: Non-Returned Value
using System; class NReturned { static void Main() { Console.WriteLine(”Hello, Pattara”); }

24 Example1: Non-Returned Value
using System; class NReturned { static void HelloP() { Console.WriteLine(”Hello, Pattara”); } static void Main() { HelloP(); Hello Pattara

25 Example2: Non-Returned Value with parameters
using System; class NReturned { static void Main() { Console.WriteLine(”Hi, AAA”); Console.WriteLine(”Hi, BBB”); Console.WriteLine(”Hi, CCC”); }

26 Example2: Non-Returned Value with parameters
using System; class NReturned { static void Hi(string s) { Console.WriteLine(”Hello, {0}”, s); } static void Main() { Hi(”AAA”); Hi(”BBB”); Hi(”CCC”); Hello, AAA Hello, BBB Hello, CCC

27 Example3: Returned Value
using System; class Returned { static void Main() { int j; j = 2*2; Console.WriteLine(”{0}”, j); j = 5*5; }

28 Example3: Returned Value
using System; class Returned { static double Power2(int n) { return n*n; } static void Main() { double j; j = Power2(2); //j=2*2; Console.WriteLine(”{0}”, j); j = Power2(5); //j=5*5; 4 25

29 C# Structure – Multiple Methods
Namespace Class Main() Method1() Method2() Method3() Method_N()

30 Variable & Constant Location
C# Structure - Methods Namespace Class Main() Variable & Constant Location statements Method1() Variable & Constant statements Method2() Variable & Constant statements

31 Self Test 1 Define Power4(n) Method Ex. int j = Power4(2); //Return 16
Return n*n*n*n Ex. int j = Power4(2); //Return 16 static ____ Power4(___________________) { }

32 Self Test 2 Define Max(x, y) Method
Return Max Value Ex. int j = Max(5, 20); //Return 20 static ____ MAX(___________________) { }

33 Outline Method Overview Using Method Parameter Passing in Method
Pass by value Pass by reference

34 Passing Parameter Type
Passing Type Pass by value Pass by reference

35 Method Example: with return value
*Methods can return only single value. int x = power3(2); … static int power3(int n) { int j = n * n * n; return j; }

36 Parameter Passing in C#
Parameter Passing Location static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; }

37 Pass by value Example Copy value s to st static void Main(){ string s;
s = "SuperMan2"; DisplayMovie(s); Console.WriteLine(s); } s = "SuperMan2" Copy value s to st s = st ="SuperMan2" static void DisplayMovie(string st) { Console.WriteLine("Movie = {0}",st); st = "SpiderMan"; } Movie = SuperMan2 SuperMan2

38 Pass by reference Example
static void Main(){ string s; s = "SuperMan2"; DisplayMovie(ref s); Console.WriteLine(s); } s = "SuperMan2" s is referred by st st = s ="SpiderMan" st = s ="SuperMan2" static void DisplayMovie(ref string st) { Console.WriteLine("Movie = {0}",st); st = "SpiderMan"; } Movie = SuperMan2 Changes st / changes s too SpiderMan

39 Summary Defining Method Calling Method Parameter Passing in Method
No-Returned Value Returned Value Parameter Passing in Method Pass by value Pass by reference


Download ppt "Module 8 & 9 Method :Part I & Array :Part II"

Similar presentations


Ads by Google