Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22...
-
Upload
lisa-eaton -
Category
Documents
-
view
219 -
download
0
description
Transcript of Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22...
![Page 1: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/1.jpg)
Final ReviewAuthor: Thanachat ThanomkulabutEdited by Supaporn Erjongmanee
Final Review22 September 2011
![Page 2: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/2.jpg)
Outline2
Major topics Methods Arrays structs
![Page 3: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/3.jpg)
Major Topics Methods
Input Output Global vs. Local Scope of variables
Array Declare, Create, Initialize, Access Run ‘for’ loop with array Array with methods 1D vs. 2D vs. multiD
![Page 4: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/4.jpg)
Major Topics (cont.)
struct Define, Create, Access Array of struct struct with methods
File I/O Collection
![Page 5: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/5.jpg)
Pre-midterm Topics Data types & Variable declaration Math expression Logical expression
How to write condition If switch-case Loop
while, do…while, for break, continue
![Page 6: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/6.jpg)
Outline6
Major topics Methods Arrays structs
![Page 7: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/7.jpg)
Methods7
Input Output Global vs. Local Scope of variables
![Page 8: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/8.jpg)
8
NamespaceClass
Main()
Method1()
Method2()
Method3()
MethodN()
C# Structure – Multiple Methods
![Page 9: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/9.jpg)
9
Characteristics of Method
Method
Type of Method (Output)
No Returned
valueReturned
value
Parameter Passing (Input)
No Parameter
Pass by value
Pass by reference
ref out
![Page 10: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/10.jpg)
Define Method
static <return-type> <method-name>(<parameter list>){ <const/variable declaration>; <statements>;}
10
Syntax
![Page 11: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/11.jpg)
When see a method, answer the followings:
1. What is method name?2. Are there any input? If so, for each input:
a) what’s data type?b) What’s variable name?c) Is it regular or ‘ref’ or ‘out’?
3. Are there any output? If so, for each output:a) what’s data type?b) What’s variable name to store output?
![Page 12: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/12.jpg)
12
Method Input Input
Name of copied variable can be different from original variable
Data type of copied and original variables must be the same!
static void Subject (double num, bool okay, int unit){ ... ... ...}static void Main() { double n; int limit; bool found; ... ... Subject(12.7, true, (limit*3)+5); ... ...}
![Page 13: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/13.jpg)
Method Input (cont.) Input (cont.)
We must choose input to be Pass by Value or Pass by Reference
Pass by Value Data type + Variable name
Pass by Reference => Value returns automatically. Do not use “return”.
ref + Data type + Variable name Must initialize value of Variable before calling the
methodstatic void sub(int a, ref int b){ b = a - b;}
![Page 14: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/14.jpg)
Define Method
static <return-type> <method-name>(<parameter list>){ <const/variable declaration>; <statements>;}
• return-type can be• void = return no value• int, double, string, … = return value with specified data
type and need return statement inside method 14
14
Syntax
![Page 15: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/15.jpg)
Method Output Output
We must choose input to be Returned Value or Pass by Reference Returned Value: appear before Method name
static returned_data_type Method_name (input_parameter_list) { … } Need “return” as one statement in a method
Pass by Reference: appear in (..) after Method name Value returns automatically. Do not use “return”. ref + Data type + Variable name
Must initialize value of Variable before calling the method out + Data type + Variable name
No need to initialize value of Variable before calling the method Value of variable before calling the method will not be passed into the
method
static void add(int a, ref int b, out int c) { c = a + b;}
static int add(int a, int b) { return a + b;}
![Page 16: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/16.jpg)
Pass by Value
16
Change in copied variable in the method has no effect on the original variable
static void Square(int num){ num = num*num; Console.WriteLine("num = {0}",num);}
static void Main(){ int num=5; Square(num); Console.WriteLine(“num = {0}",num); }
5num
num525
num = 25num = 5
Monitor
![Page 17: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/17.jpg)
Pass by reference17
static void Main(){ int a, b, c; a = 2; b = 3; c = 4; Hello(a, ref b, out c);
Console.WriteLine(“Finally a = {0}, b = {1}, c = {2}”,a,b,c);}
static void Hello (int x, ref int y, out int z){ x = 2*x; y = 2*y; z = x+y; Console.WriteLine(“Hello x = {0}, y = {1}, z = {2}”, x,y,z);}
a2
b3
c4
x
2
y
3
z104 6
10
Hello x = 4, y = 6, z = 10Finally a = 2, b= 6, c = 10
6
Monitor
“Out” does not pass in any
value to method
![Page 18: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/18.jpg)
Examples
18
static void Main() { PrintLine(‘n’, 14);
double width = 5, height = 10; double area = Area(width, height); Console.WriteLine(area); Console.WriteLine(compAve(10,20,30));}
static void PrintLine(char a, int b ) { Console.WriteLine(“{0} is {1}th alphabet”, a, b);}
static double Area(double w, double h) { return w*h;}
static double compAve(int x1, int x2, int x3) { return (x1+x2+x3)/3;}
n is 14th alphabet5020
Monitor
![Page 19: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/19.jpg)
Examples 2
19
static void Main() { char x = ‘p’, y = ‘q’; int p = 3, q = 5; double a=5, b=10, c=0, d, e; sub( p, q, out d); mult( a , b, ref c); switch( ref x, ref y);}
static void sub(int a, int b, out double c) { c = a – b ;}
static void mult(double p, double q, ref double r) { r = p * q;}
static void switch(ref char x1, ref char x2) { char temp = x1; x1 = x2; x2 = temp;}
![Page 20: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/20.jpg)
Methods: Passing outputstatic void Main() { int x = 1, y = 2; Add100_v1(x,y); Console.WriteLine(“{0},{1}”,x,y); Add100_v2(ref x,ref y); Console.WriteLine(“{0},{1}”,x,y); Add100_v3(ref x,y); Console.WriteLine(“{0},{1}”,x,y)}static void Add100_v1(int num, int num2) { num += 100; num2 += 100;}static void Add100_v2(ref int num, ref int num2) { num += 100; num2 += 100;}static void Add100_v3(ref int num, int num2) { num += 100; num2 += 100;}
1,2101,102201,102
Monitor
Methods with ref ref can do:1) pass values into
method2) return the changed
values back
Variables passing with ref need to be initialized before passing.
ref can be used with regular or out
![Page 21: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/21.jpg)
static void Main() { int x = 1, y = 2; int result1; Add_v1(x,y,out result1); Console.WriteLine(result1); int result2; Add_v2(x,y,ref result2); Console.WriteLine(result2);}static void Add_v1(int num, int num2, out num3) { num3 = num + num2;}static void Add_v2(int num, int num2, ref num4) { num4 = num + num2;}
Methods with out • Out can do:• pass the changed values
back.
Variables passing with out DO NOT need to be initialized before passing.
No Initialized value for result1 (using out)
OK!
No initialized value for result2 (using ref)
ERROR!!!!
![Page 22: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/22.jpg)
static void Main() { int x = 1, y = 2; int result1; Add_v1(x,y,out result1); Console.WriteLine(result1); int result2 = 0; Add_v2(result1,y,ref result2); Console.WriteLine(result2);}static void Add_v1(int num, int num2, out num3) { num3 = num + num2;}static void Add_v2(int num, int num2, ref num4) { num4 = num + num2;}
35
Monitor
Methods with out • Out can do:• pass the changed values
back.
Variables passing with out DO NOT need to be initialized before passing.
No Initialized value for result1 (using out)
OK!
Initialized value for result2 (using ref)
OK!
![Page 23: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/23.jpg)
Global vs. Local Variables23
Global variables, constants, structs
namespace
class
Main()
MethodX()
Local variables, constants, statements
Local variables, constants, statements
![Page 24: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/24.jpg)
Global vs. Local Variables (cont.)
24class
Main() Method1()
x y
n
int x;
static int n;
x = 5;y = 12;n = 9;
int y;x = 5;y = 12;n = 9;
![Page 25: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/25.jpg)
Variable Scope: Example #1using System;class ScopeTest { static void Main() { for(int i=0;i<5;i++){ Console.Write(“*”); } Console.Write(i); }}
Scope of this
i starts hereThen ends
here
This line is not in i's
scope
25
![Page 26: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/26.jpg)
Variable Scope: Example #2using System;class ScopeTest { static void Main() { int x; x = 5; }
static void Method1() { Console.WriteLine(x); }}
Scope of this x starts here
Then ends here
This line is not in x's
scope
26
![Page 27: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/27.jpg)
Example 127
class Test { static void Main() { int x = 3; Add10(x); Console.WriteLine(x); } static void Add10(int x){ x += 10;
}}
3Monitor
Method Add10 will not pass new value of ‘x’ back since ‘x’ is not
passed by reference.
Program will show value of variable “x” in
Method Main
![Page 28: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/28.jpg)
Example 128
class Test { static int sum = 0; static void Main() { Console.Write(“sum={0}”,sum); Add(2); Console.Write(“sum={0}”,sum); } static void Add(int y){ sum += y; }}
sum=0Monitor
sum=2
Example 2
![Page 29: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/29.jpg)
Example 129
class Test { static int sum = 0; static void Main() { Console.Write(“sum={0}”,sum); Add(2); Console.Write(“sum={0}”,sum); } static void Add(int y){ sum += y; }}
sum=0Monitor
sum=2
Console.Write(“y={0}”,y);
Error
Example 2
![Page 30: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/30.jpg)
Example 3
30 class Test { static int sum = 0; static void Main() { Console.WriteLine(sum); methodA(); Console.WriteLine(sum); } static void methodA(){ int sum = 5; Console.WriteLine(sum); }}
In Main, value of GLOBAL variable ‘sum’ is used.
In methodA, value of LOCAL variable ‘sum’ is
used.
0Monitor 5
0Does this program work?
![Page 31: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/31.jpg)
Outline31
Major topics Methods Arrays structs
![Page 32: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/32.jpg)
Arrays
Declare, Create, Initialize, Access Run ‘for’ loop with array Array with methods 1D vs. 2D vs. multiD
![Page 33: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/33.jpg)
Array: 1D• Declare array
– int [] arr;• Create array: Must have size
– arr = new int [5];• Declare + create: Must have size
– int [] arr = new int [5];• If we want to initialize, we need to
declare (+create) array first. No need to have size– int [] arr = new int [] {5,6,7,8,9};– int [] arr ={5,6,7,8,9};
![Page 34: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/34.jpg)
Array: 1D34
Which ones are valid C# statements ?1. int [] arrayA = new int[] {3, 6, 9};2. int arrayA = new int[3] {3, 6, 9}; 3. int [] arrayA = new int {3, 6, 9};4. int arrayA = new int {3, 6, 9};5. int [] arrayA = new int[3] ;6. int [] arrayA = new int[] ;7. int [] arrayA = new int[3] {3, 6, 9};
![Page 35: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/35.jpg)
Array: 1D (cont.)• Accessing array: index = integer
– Use index: 0,1,…,size-1• Accessing 1 element in array: arr[index]
– Example: arr[0] = 10;• Accessing many elements in array: ‘for’
– for (int i = 0; i < arr.Length; i++) arr[i] = i+10;
• Only-Read all elements in array– foreach (int x in arr)
Console.WriteLine(x);
![Page 36: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/36.jpg)
Array: 1D (cont.)
• Only-Read all elements in array (cont.)– Assume arr2 is array with double typeint count = 0;foreach (double y in arr2) { if (y > 0) count++;}
![Page 37: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/37.jpg)
String37
String is “array of chars.”string name = “Bossa”;Console.WriteLine(name[0]);Console.WriteLine(name[4-3]);name[4] = ‘m’;
‘B’
‘o’ ‘s’nam
e
0 1 2‘s’3
‘a'
4 BOutput
o
Each element in string is for “reading
only”
![Page 38: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/38.jpg)
Array: 2D• Declare array
– int [,] arr;• Create array: Must have size
– arr = new int [3,2];• Declare + create: Must have size
– int [,] arr = new int [3,2];• If we want to initialize, we need to
declare (+create) array first. No need to have size– int [,] arr = new int [,] {{5,6},{7,8},
{9,0}};– int [,] arr ={{5,6},{7,8},{9,0}};
![Page 39: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/39.jpg)
Array: 2D • Accessing array: 2 indices
– Use row index: 0,1,…,row_size-1– Use column index: 0,1,…,col_size-1
• Accessing 1 element in 2D array: arr[row_index, col_index]– Example: arr[0,1] = 5;
• Get size:– arr.GetLength(0): Number of rows– arr.GetLength(1): Number of columns– arr.Length: Number of total elements
![Page 40: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/40.jpg)
Array: 2D (cont.)
• Accessing all elements in 2D array– Use 2 ‘for’ loops: go through all rows and
all columns– Different index for each ‘for’ loop– for (int i = 0; i < arr.GetLength(0); i++)
for (int j = 0; j < arr.GetLength(1); j++)arr[i,j] = i*10+j;
![Page 41: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/41.jpg)
Array: 2D (cont.)
• Accessing one row (e.g., 3rd row) in 2D array– Use 1 ‘for’ loops to go through all
columns– Fixed row index for given row– ‘i’ index for column– Example: for (int i = 0; i < arr.GetLength(1); i++)
Console.WriteLine(arr[2 , i]);
![Page 42: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/42.jpg)
Array: 2D (cont.)
42
• Accessing one column (e.g., 2nd column) in 2D array– Use 1 ‘for’ loops to go through all rows– Fixed column index for given column– ‘i’ index for row– for (int i = 0; i < arr.GetLength(0); i++)
Console.WriteLine(arr[ i, 1]);
![Page 43: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/43.jpg)
static void Main() { int [] arr = new int [10]; Arr_initial(arr); Display_arr(arr); Add10(arr); Display_arr(arr);}static void Arr_initial (int [] a) { for (int i = 0; i < a.Length; i++) a[i] = i;}static void Add10(int [] a) { for (int i = 0; i < a.Length; i++) a[i] = a[i]+10;}static void Display_arr(int [] a) { for (int i = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine();}
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
Monitor
Pass array into method without ‘ref’1) Pass the changed values back.2) Can’t pass the new array back
Methods: passing array with no ref
Array is initialized all elements with 0 automatically.
Print out values from first to last elements in array
![Page 44: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/44.jpg)
static void Main() { int [] arr = new int [10]; Arr_initial(arr); Display_arr(arr); Op_arr(arr); Display_arr(arr);}static void Arr_initial (int [] a) { for (int i = 0; i < a.Length; i++) a[i] = i;}static void Op_arr(int [] a) {
a = new int [5];}static void Display_arr(int [] a) { for (int i = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine();}
0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
Monitor
Pass array into method without ‘ref’1) Pass the changed values back.2) Can’t pass the new array back
Methods: passing array with no ref
Without ref: new array will not be passed back to Main
Declare new array with 5 elements
![Page 45: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/45.jpg)
static void Main() { int [] arr = new int [10]; Arr_initial(arr); Display_arr(arr); Op_arr(arr); Display_arr(arr);}static void Arr_initial (int [] a) { for (int i = 0; i < a.Length; i++) a[i] = i;}static void Op_arr(int [] a) {
a[0]=100; a = new int [5]; a[1]=1000;}static void Display_arr(int [] a) { for (int i = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine();}
0,1,2,3,4,5,6,7,8,9,100,1,2,3,4,5,6,7,8,9,
Monitor
Pass array into method without ‘ref’1) Pass the changed values back.2) Can’t pass the new array back
Methods: passing array with no ref
Without ref: changed values after new array will not be passed back to Main
Declare new array with 5 elements
![Page 46: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/46.jpg)
static void Main() { int [] arr = new int [10]; Arr_initial(arr); Display_arr(arr); Op_arr(ref arr); Display_arr(arr);}
static void Arr_initial (int [] a) { for (int i = 0; i < a.Length; i++) a[i] = i;}static void Op_arr(ref int [] a) {
a = new int [5];}static void Display_arr(int [] a) { for (int i = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine();}
0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,
Monitor
Methods: passing array with ref Pass array into method with ‘ref’
1) Pass the changed values back.2) Can pass the new array back
Declare new array with 5 elements
With ref: new array will be passed back to Main
![Page 47: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/47.jpg)
static void Main() { int [] arr = new int [10]; Arr_initial(arr); Display_arr(arr); Op_arr(ref arr); Display_arr(arr);}static void Arr_initial (int [] a) { for (int i = 0; i < a.Length; i++) a[i] = i;}static void Op_arr(ref int [] a) { a[0] = 100;
a = new int [5];a[2]=1000;
}static void Display_arr(int [] a) { for (int i = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine();}
0,1,2,3,4,5,6,7,8,9,0,0,1000,0,0,
Monitor
Methods: passing array with ref Pass array into method with ‘ref’
1) Pass the changed values back.2) Can pass the new array back
Declare new array with 5 elements
With ref: changed values after new array will be passed back to Main
![Page 48: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/48.jpg)
Outline48
Major topics Methods Arrays structs
![Page 49: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/49.jpg)
struct
Define, Create, Access Array of struct struct with methods
![Page 50: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/50.jpg)
Define struct
StudentInfo
Name: Paul
Dept: Math
Age: 18
Gender: M
string
string
int
char
struct StudentInfo { public string name; public string dept; public int age; public char gender;}
Must use "struct" keyword
Every struct needs a name
Protection level – for now always use "public”
Members of struct
50
![Page 51: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/51.jpg)
using System;class StructTest {
static void Main() { }}
Create struct: Format I & II
struct_name var_name;
StudentInfo
Name:
Dept:
Age:
Gender:
string
string
int
charStdInfo student
= new StdInfo ();StdInfo student;
struct StdInfo { public string name; public string dept; public int age; public char gender;}
51
struct_name var_name = new struct_name ();
![Page 52: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/52.jpg)
Syntax
student.name = “Paul”;
Console.WriteLine(student.name);52
Access struct
var_name.member_name
PaulMonitor
Name:
Dept:
Age:
Gender:
string
string
int
char
• Assign value
• Display value
Paul StdInfo student;
52
![Page 53: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/53.jpg)
Declare & Create Array of struct
Declare Array
Declare Array of struct
double[] score;score = new double[4];
StdInfo[] student;student = new StdInfo[4];
score0 1 2 3
idnamedept
idnamedept
idnamedept
idnamedept
0 1 2 3
student
struct StdInfo { public int id; public string name; public string dept;}
53
![Page 54: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/54.jpg)
Access Array of struct
Access Array
Access Array of structscore[2] = 5;
student[0].name = ”Paul";
idnamedept
idnamedept
idnamedept
idnamedept
0 1 2 3student
score0 1 2 3
5
student[3].id = 713;
713Paul
54 var_name[index].struct_member
![Page 55: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/55.jpg)
Example : Read 1 Student’s Informationclass StructTest { struct StdInfo { public int id; public string name; public string dept; } static void Main() {
}}
StdInfo student; Console.Write("Input ID: ");student.id = int.Parse(Console.ReadLine());Console.Write("Input Name: ");student.name = Console.ReadLine();Console.Write("Input Dept: ");student.dept = Console.ReadLine();
55
1 student
StdInfo
ID:
Name:
Dept:
int
string
string
![Page 56: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/56.jpg)
Example : Read 50 Students’ Informationclass StructTest { struct StdInfo { public int id; public string name; public string dept; } static void Main() {
}}
StdInfo student; Console.Write("Input ID: ");student.id = int.Parse(Console.ReadLine());Console.Write("Input Name: ");student.name = Console.ReadLine();Console.Write("Input Dept: ");student.dept = Console.ReadLine();
56
StdInfo[] student = new StdInfo[50];for(int i=0;i<50;i++){ Console.Write("Input ID: "); student[i].id = int.Parse(Console.ReadLine()); Console.Write("Input ์Name: "); student[i].name = Console.ReadLine(); Console.Write("Input Dept: "); student[i].dept = Console.ReadLine();}
StdInfo
ID:
Name:
Dept:
int
string
string
50 students
![Page 57: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/57.jpg)
Example: Display names of Math students
Display names of students from Math department
for(int i=0;i<50;i++){
if(student[i].dept == ”Math") Console.WriteLine(student[i].name);
}
idnamedept
601 idnamedept
623…
idnamedept
PamMath
0 1 49student
713BobPaul
Math English
57
57
![Page 58: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/58.jpg)
Methods: passing with structstruct complex_num {
public double real; public double imag; }static void Main() { complex_num num1, num2; assign_complex(5,2,out num1); assign_complex(1,7,out num2); display_complex(num1); add_complex(ref num1, num2); display_complex(num1);}static void assign_complex(double r, double im, out complex_num n) { n.real = r; n.imag = im; }static void add_complex(ref complex_num n, complex_num n2) { n.real = n.real + n2.real; n.imag = n.imag + n2.imag; }static void display_complex(complex_num n) { Console.WriteLine(“{0}+{1}i”,n.real, n.imag); }
5+2i6+9i
Monitor
Similar to regular variables, we can use struct to pass by value or pass by reference (ref or out).Replace data type with struct name
![Page 59: Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.](https://reader033.fdocuments.in/reader033/viewer/2022052419/5a4d1b587f8b9ab0599aa1da/html5/thumbnails/59.jpg)
Good Luck