Final Review

1 / 59

# Final Review - PowerPoint PPT Presentation

Final Review. Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee. Final Review 22 September 2011. Outline. Major topics Methods Arrays structs. Major Topics. Methods Input Output Global vs. Local Scope of variables Array Declare, Create, Initialize, Access

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Final Review' - iria

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Final Review

Author: ThanachatThanomkulabut

Edited by Supaporn Erjongmanee

Final Review

22 September 2011

Outline
• Major topics
• Methods
• Arrays
• structs
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
Major Topics (cont.)
• struct
• Define, Create, Access
• Array of struct
• struct with methods
• File I/O
• Collection
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
Outline
• Major topics
• Methods
• Arrays
• structs
Methods
• Input
• Output
• Global vs. Local
• Scope of variables

Main()

Namespace

Class

Method1()

Method2()

Method3()

MethodN()

C# Structure – Multiple Methods
Define Method

Syntax

static <return-type> <method-name>(<parameter list>)

{

<const/variable declaration>;

<statements>;

}

10

When see a method, answer the followings:
• What is method name?
• Are there any input? If so, for each input:
• what’s data type?
• What’s variable name?
• Is it regular or ‘ref’ or ‘out’?
• Are there any output? If so, for each output:
• what’s data type?
• What’s variable name to store output?
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);

... ...

}

Method Input (cont.)
• Input (cont.)
• We must choose input to be Pass by Valueor 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 method

static void sub(int a, ref intb)

{

b = a - b;

}

Define Method

Syntax

static <return-type> <method-name>(<parameter list>)

{

<const/variable declaration>;

<statements>;

}

14

• return-type can be
• void= return no value
• int, double, string, …
• = return value with specified data type and need return statement inside method
Method Output
• Output
• We must choose input to be Returned Value or Pass by Reference
• Returned Value: appear before Method name
• static returned_data_typeMethod_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 int add(int a, int b) {

return a + b;

}

static void add(int a, ref intb, out int c) {

c = a + b;

}

Monitor

Pass by Value

num = 25

num = 5

• Changeincopied variablein the method has no effecton the original variable

static void Square(int num)

{

num = num*num;

Console.WriteLine("num = {0}",num);

}

num

25

5

static void Main()

{

int num=5;

Square(num);

Console.WriteLine(“num = {0}",num);

}

num

5

Monitor

Pass by reference

Hello x = 4, y = 6, z = 10

Finally a = 2, b= 6, c = 10

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);

}

x

y

z

“Out” does not pass in any value to method

10

4

2

6

3

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);

}

a

b

c

6

2

3

4

10

Monitor

Examples

n is 14th alphabet

50

20

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 voidPrintLine(char a, int b ) {

Console.WriteLine(“{0} is {1}th alphabet”, a, b);

}

static double Area(double w, double h) {

return w*h;

}

static doublecompAve(int x1, int x2, int x3) {

return(x1+x2+x3)/3;

}

Examples 2

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 voidsub(int a, int b, out double c) {

c = a – b ;

}

static voidmult(double p, double q, ref double r) {

r = p * q;

}

static voidswitch(ref char x1, ref char x2) {

char temp = x1; x1 = x2; x2 = temp;

}

static void Main() {

int x = 1, y = 2;

Console.WriteLine(“{0},{1}”,x,y);

Console.WriteLine(“{0},{1}”,x,y);

Console.WriteLine(“{0},{1}”,x,y)

}

num += 100; num2 += 100;

}

static voidAdd100_v2(refintnum, ref int num2) {

num += 100;

num2 += 100;

}

num+= 100;

num2 += 100;

}

Methods with ref

ref can do:

pass values into method

return the changed values back

Methods: Passing output

Monitor

1,2

101,102

201,102

ref can be used with regular or out

Variables passing with ref need to be initialized before passing.

Out can do:

• pass the changed values back.

Methods with out

static void Main() {

int x = 1, y = 2;

int result1;

Console.WriteLine(result1);

int result2;

Console.WriteLine(result2);

}

static void Add_v1(intnum, int num2, out num3) {

num3 = num + num2;

}

static void Add_v2(intnum, int num2, ref num4) {

num4 = num + num2;

}

No Initialized value for result1 (using out)

OK!

No initialized value for result2 (using ref)

ERROR!!!!

Variables passing with out DO NOT need to be initialized before passing.

Out can do:

• pass the changed values back.

Methods with out

static void Main() {

int x = 1, y = 2;

int result1;

Console.WriteLine(result1);

intresult2 = 0;

Console.WriteLine(result2);

}

static void Add_v1(intnum, int num2, out num3) {

num3 = num + num2;

}

static void Add_v2(intnum, int num2, ref num4) {

num4 = num + num2;

}

No Initialized value for result1 (using out)

OK!

Initialized value for result2 (using ref)

OK!

Monitor

3

5

Variables passing with out DO NOT need to be initialized before passing.

Global vs. Local Variables

namespace

class

Global variables, constants, structs

Main()

Local variables, constants, statements

MethodX()

Local variables, constants, statements

Global vs. Local Variables (cont.)

class

n

static int n;

Main()

Method1()

int x;

int y;

x

y

x = 5;

x = 5;

y = 12;

y = 12;

n = 9;

n = 9;

Variable Scope: Example #1

Scope of this i starts here

using System;

class ScopeTest {

static void Main() {

for(int i=0;i<5;i++){

Console.Write(“*”);

}

Console.Write(i);

}

}

Then ends here

This line is not in i\'s scope

Variable Scope: Example #2

Scope of this x starts here

using System;

class ScopeTest {

static void Main() {

int x;

x = 5;

}

static void Method1() {

Console.WriteLine(x);

}

}

Then ends here

This line is not in x\'s scope

Monitor

Program will show value of variable “x” in Method Main

Example 1

3

class Test {

static void Main() {

int x = 3;

Console.WriteLine(x);

}

x += 10;

}

}

Method Add10 will not pass new value of ‘x’ back since ‘x’ is not passed by reference.

Example 2

Monitor

Example 1

class Test {

static int sum = 0;

static void Main() {

Console.Write(“sum={0}”,sum);

Console.Write(“sum={0}”,sum);

}

sum += y;

}

}

sum=0

sum=2

Example 2

Monitor

Example 1

class Test {

static int sum = 0;

static void Main() {

Console.Write(“sum={0}”,sum);

Console.Write(“sum={0}”,sum);

}

sum += y;

}

}

sum=0

sum=2

Console.Write(“y={0}”,y);

Error

0

Monitor

Does this program work?

Example 3

5

0

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 GLOBALvariable‘sum’ is used.

In methodA, value of LOCALvariable ‘sum’ is used.

Outline
• Major topics
• Methods
• Arrays
• structs
Arrays
• Declare, Create, Initialize, Access
• Run ‘for’ loop with array
• Array with methods
• 1D vs. 2D vs. multiD
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};
Array: 1D
• Which ones are valid C# statements ?
• int [] arrayA = new int[] {3, 6, 9};
• intarrayA= new int[3] {3, 6, 9};
• int [] arrayA = new int{3, 6, 9};
• intarrayA= new int{3, 6, 9};
• int [] arrayA = new int[3] ;
• int [] arrayA = new int[] ;
• int [] arrayA = new int[3] {3, 6, 9};

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 (inti = 0; i < arr.Length; i++)

arr[i] = i+10;

• Only-Read all elements in array
• foreach (int x in arr)

Console.WriteLine(x);

Array: 1D (cont.)
• Only-Read all elements in array (cont.)
• Assume arr2 is array with double type

int count = 0;

foreach (double y in arr2) {

if (y > 0)

count++;

}

Output

String

0

1

2

3

4

B

‘B’

‘o’

‘s’

‘s’

‘a\'

o

name

• String is “array of chars.”

string name = “Bossa”;

Console.WriteLine(name[0]);

Console.WriteLine(name[4-3]);

name[4] = ‘m’;

Each element in string is for “reading only”

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}};
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
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 (inti = 0; i < arr.GetLength(0); i++)

for (intj = 0; j < arr.GetLength(1); j++)

arr[i,j] = i*10+j;

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 (inti = 0; i < arr.GetLength(1); i++)

Console.WriteLine(arr[2 , i]);

Array: 2D (cont.)
• 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 (inti = 0; i < arr.GetLength(0); i++)

Console.WriteLine(arr[ i, 1]);

Pass array into method without ‘ref’

Pass the changed values back.

Can’t pass the new array back

Methods: passing array with no ref

static voidMain() {

int [] arr = new int[10];

Arr_initial(arr);

Display_arr(arr);

Display_arr(arr);

}

static voidArr_initial (int [] a) {

for (inti= 0; i< a.Length; i++)

a[i] = i;

}

for (inti = 0; i < a.Length; i++)

a[i] = a[i]+10;

}

static void Display_arr(int [] a) {

for (inti = 0; i < a.Length; i++)

Console.Write(“{0},” a[i]);

Console.WriteLine();

}

Monitor

0,1,2,3,4,5,6,7,8,9,

10,11,12,13,14,15,16,17,18,19,

Array is initialized all elements with 0 automatically.

Print out values from first to last elements in array

Pass array into method without ‘ref’

Pass the changed values back.

Can’t pass the new array back

Methods: passing array with no ref

static voidMain() {

int [] arr = new int[10];

Arr_initial(arr);

Display_arr(arr);

Op_arr(arr);

Display_arr(arr);

}

static voidArr_initial (int [] a) {

for (inti= 0; i< a.Length; i++)

a[i] = i;

}

static voidOp_arr(int[] a) {

a = new int [5];

}

static void Display_arr(int [] a) {

for (inti = 0; i < a.Length; i++)

Console.Write(“{0},” a[i]);

Console.WriteLine();

}

Monitor

0,1,2,3,4,5,6,7,8,9,

0,1,2,3,4,5,6,7,8,9,

Declare new array with 5 elements

Without ref: new array will not be passed back to Main

Pass array into method without ‘ref’

Pass the changed values back.

Can’t pass the new array back

Methods: passing array with no ref

static voidMain() {

int [] arr = new int[10];

Arr_initial(arr);

Display_arr(arr);

Op_arr(arr);

Display_arr(arr);

}

static voidArr_initial (int [] a) {

for (inti= 0; i< a.Length; i++)

a[i] = i;

}

static voidOp_arr(int[] a) {

a[0]=100;

a = new int [5];

a[1]=1000;

}

static void Display_arr(int [] a) {

for (inti = 0; i < a.Length; i++)

Console.Write(“{0},” a[i]);

Console.WriteLine();

}

Monitor

0,1,2,3,4,5,6,7,8,9,

100,1,2,3,4,5,6,7,8,9,

Declare new array with 5 elements

Without ref: changed values after new array will not be passed back to Main

Pass array into method with ‘ref’

Pass the changed values back.

Can pass the new array back

Methods: passing array with ref

static voidMain() {

int [] arr = new int[10];

Arr_initial(arr);

Display_arr(arr);

Op_arr(ref arr);

Display_arr(arr);

}

static voidArr_initial (int [] a) {

for (inti= 0; i< a.Length; i++)

a[i] = i;

}

static voidOp_arr(ref int[] a) {

a = new int[5];

}

static voidDisplay_arr(int [] a) {

for (inti = 0; i < a.Length; i++)

Console.Write(“{0},” a[i]);

Console.WriteLine();

}

Monitor

0,1,2,3,4,5,6,7,8,9,

0,0,0,0,0,

Declare new array with 5 elements

With ref: new array will be passed back to Main

Pass array into method with ‘ref’

Pass the changed values back.

Can pass the new array back

Methods: passing array with ref

static voidMain() {

int [] arr = new int[10];

Arr_initial(arr);

Display_arr(arr);

Op_arr(ref arr);

Display_arr(arr);

}

static voidArr_initial (int [] a) {

for (inti= 0; i< a.Length; i++)

a[i] = i;

}

static voidOp_arr(ref int[] a) {

a[0] = 100;

a = new int[5];

a[2]=1000;

}

static voidDisplay_arr(int [] a) {

for (inti = 0; i < a.Length; i++)

Console.Write(“{0},” a[i]);

Console.WriteLine();

}

Monitor

0,1,2,3,4,5,6,7,8,9,

0,0,1000,0,0,

Declare new array with 5 elements

With ref: changed values after new array will be passed back to Main

Outline
• Major topics
• Methods
• Arrays
• structs
struct
• Define, Create, Access
• Array of struct
• struct with methods
Define struct

StudentInfo

Must use "struct" keyword

Every structneeds a name

Name:

Paul

structStudentInfo {

public string name;

public string dept;

public int age;

public char gender;

}

string

Dept:

Math

string

Age:

18

int

Members of struct

Gender:

M

char

Protection level – for now always use "public”

Create struct: Format I & II

struct_namevar_name= new struct_name();

• struct_namevar_name;

using System;

class StructTest {

static void Main() {

}

}

StudentInfo

struct StdInfo {

public string name;

public string dept;

public int age;

public char gender;

}

Name:

string

Dept:

string

Age:

int

StdInfo

student

StdInfo

student;

Gender:

= new

StdInfo ();

char

Access struct

Monitor

Paul

52

var_name.member_name

• Syntax

Name:

Paul

• StdInfo student;

string

Dept:

• Assign value
• Display value

string

Age:

• student.name = “Paul”;

int

Gender:

char

• Console.WriteLine(student.name);
Declare & Create Array of struct

score

• Declare Array
• Declare Array of struct

0

1

2

3

double[] score;

score = new double[4];

structStdInfo {

public int id;

public string name;

public string dept;

}

StdInfo[] student;

student = new StdInfo[4];

student

id

id

id

id

name

name

name

name

dept

dept

dept

dept

0

1

2

3

Access Array of struct
• var_name[index].struct_member
• Access Array
• Access Array of struct

score[2] = 5;

5

score

0

1

2

3

student[0].name = ”Paul";

student[3].id = 713;

713

Paul

id

id

id

id

student

name

name

name

name

0

1

2

3

dept

dept

dept

dept

Example : Read 1 Student’s Information

classStructTest {

structStdInfo {

public int id;

public string name;

public string dept;

}

static void Main() {

}}

1 student

StdInfo student;

Console.Write("Input ID: ");

Console.Write("Input Name: ");

Console.Write("Input Dept: ");

StdInfo

ID:

int

Name:

string

Dept:

string

Example : Read 50 Students’ Information

classStructTest {

structStdInfo {

public int id;

public string name;

public string dept;

}

static void Main() {

}}

50 students

StdInfo[] student = new StdInfo[50];

for(inti=0;i<50;i++){

Console.Write("Input ID: ");

Console.Write("Input์Name: ");

Console.Write("Input Dept: ");

}

StdInfo student;

Console.Write("Input ID: ");

Console.Write("Input Name: ");

Console.Write("Input Dept: ");

StdInfo

ID:

int

Name:

string

Dept:

string

Example: Display names of Math students

57

• Display names of students from Math department

for(int i=0;i<50;i++){

if(student[i].dept == ”Math")

Console.WriteLine(student[i].name);

}

713

id

id

id

601

623

Paul

Bob

student

name

name

name

Pam

Math

English

dept

dept

dept

Math

0

1

49

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

Methods: passing with struct

structcomplex_num {

public double real;

public double imag;

}

static voidMain() {

complex_num num1, num2;

assign_complex(5,2,out num1);

assign_complex(1,7,out num2);

display_complex(num1);

display_complex(num1);

}

static voidassign_complex(double r, double im, out complex_numn) {

n.real = r; n.imag= im; }

static voidadd_complex(ref complex_num n, complex_num n2) {

n.real = n.real + n2.real;

n.imag= n.imag+ n2.imag; }

static voiddisplay_complex(complex_num n) {

Console.WriteLine(“{0}+{1}i”,n.real, n.imag); }

Monitor

5+2i

6+9i