Module 8 & 9 Method :Part I & Array :Part II Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
Outlines Array Review Multi-dimensional Array Review Method Overview
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
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
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));
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
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++; }
Outlines Array Review Multi-dimensional Array Method Overview
Multi-dimensional Array’s declaration Syntax: <type> [ , ] <name>; <type> [ , , ] <name>; Creation: <name> = new <type>[<dim1>,<dim2>]; 2 dimensions 3 dimensions 2 dimensions
Array Declaration Example int [ , ] grid2; grid2 = new int [3,2]; int [ , , ] grid3; grid3 = new int [3, 2, 4];
More examples 4 3 5 2 1 grid[1,1] == ? grid[1,2] == ? grid[0,2] == ? 0 1 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
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
Outline Method Overview Using Method Parameter Passing in Method Pass by value Pass by reference
a function/procedure owned by class. What is Method? Method is a function/procedure owned by class. System.Console.WriteLine(“Hello Kitty!!!”);
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);
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
Method Idea name age width high DispAge CalArea area Return no value (void) Return Value
Method Example 1 4 1 3 2
Outline Method Overview Using Method Parameter Passing in Method Pass by value Pass by reference
Using User-Defined Method 1.Define Method 2.Call Method
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
Method Declaration Example static void displaybox() { } static int calage() { } static int calbox(int x,y;) { }
Example1: Non-Returned Value using System; class NReturned { static void Main() { Console.WriteLine(”Hello, Pattara”); }
Example1: Non-Returned Value using System; class NReturned { static void HelloP() { Console.WriteLine(”Hello, Pattara”); } static void Main() { HelloP(); Hello Pattara
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”); }
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
Example3: Returned Value using System; class Returned { static void Main() { int j; j = 2*2; Console.WriteLine(”{0}”, j); j = 5*5; }
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
C# Structure – Multiple Methods Namespace Class Main() Method1() Method2() Method3() Method_N()
Variable & Constant Location C# Structure - Methods Namespace Class Main() Variable & Constant Location statements Method1() Variable & Constant statements Method2() Variable & Constant statements
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(___________________) { }
Self Test 2 Define Max(x, y) Method Return Max Value Ex. int j = Max(5, 20); //Return 20 static ____ MAX(___________________) { }
Outline Method Overview Using Method Parameter Passing in Method Pass by value Pass by reference
Passing Parameter Type Passing Type Pass by value Pass by reference
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; }
Parameter Passing in C# Parameter Passing Location static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; }
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
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
Summary Defining Method Calling Method Parameter Passing in Method No-Returned Value Returned Value Parameter Passing in Method Pass by value Pass by reference