Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

Computer Science 1620 Loops.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
4. Statements and Methods. 2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
STRUCT Thanachat Thanomkulabut 1. Array Review 2  Group multiple items of the same type into one "variable" double[] score; score = new.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Type of Method No Returned value Returned value No Parameter Pass by value refout Pass by reference Parameter.
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
Computer Programming for Engineers
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 st Semester Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module11 Struct Type in C# Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Java Programming Language Lecture27- An Introduction.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Test 2 Review Outline.
Chapter 7 User-Defined Methods.
C# — Console Application
COMP 170 – Introduction to Object Oriented Programming
Arrays Computer & Programming Group
Arrays, For loop While loop Do while loop
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
Thanachat Thanomkulabut
Thanachat Thanomkulabut
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions continued.
Iteration (Loop) partII
Thanachat Thanomkulabut
Iteration (Loop) part II
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011

Outline 2  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 6  Major topics  Methods  Arrays  structs

Methods 7  Input  Output  Global vs. Local  Scope of variables

8Namespace Class Main() Method1() Method2() Method3() MethodN() C# Structure – Multiple Methods

9 Characteristics of Method Method Type of Method (Output) No Returned value Returned value Parameter Passing (Input) No Parameter Pass by value Pass by reference refout

Define Method static ( ) { ; } 10 Syntax

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?

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

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 method static void sub(int a, ref int b) { b = a - b; }

Define Method static ( ) { ; } return-type can be return-type can be void = return no value void = return no value int, double, string, … int, double, string, … = return value with specified data type and need return statement inside method = return value with specified data type and need return statement inside method 14 Syntax

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

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); } 5 num 525 num = 25 num = 5 Monitor

Pass by reference 17 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); } a 2 b 3 c 4 x 2 y 3 z Hello x = 4, y = 6, z = 10 Finally a = 2, b= 6, c = 10 6 Monitor “Out” does not pass in any value to method

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 14 th alphabet Monitor

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

Methods: Passing output static 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,2 101, ,102 Monitor Methods with ref ref can do: 1)pass values into method 2)return the changed values back Variables passing with ref need to be initialized before passing. ref can be used with regular or out

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!!!!

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; } 3535 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!

Global vs. Local Variables 23 Global variables, constants, structs namespace class Main() MethodX() Local variables, constants, statements

Global vs. Local Variables (cont.) 24 class Main()Method1() xy n int x; static int n; x = 5; y = 12; n = 9; int y; x = 5; y = 12; n = 9;  

Variable Scope: Example #1 using System; class ScopeTest { static void Main() { for(int i=0;i<5;i++){ Console.Write(“*”); } Console.Write(i); } using System; class ScopeTest { static void Main() { for(int i=0;i<5;i++){ Console.Write(“*”); } Console.Write(i); } Scope of this i starts here Then ends here This line is not in i's scope 25

Variable Scope: Example #2 using System; class ScopeTest { static void Main() { int x; x = 5; } static void Method1() { Console.WriteLine(x); } using 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

Example 1 27 class Test { static void Main() { int x = 3; Add10(x); Console.WriteLine(x); } static void Add10(int x){ x += 10; } class Test { static void Main() { int x = 3; Add10(x); Console.WriteLine(x); } static void Add10(int x){ x += 10; } 3 Monitor 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

Example 1 28 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; } 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=0 Monitor sum=2 Example 2

Example 1 29 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; } 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=0 Monitor sum=2 Console.Write(“y={0}”,y); Error Example 2

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); } 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. 0 Monitor 5 0 Does this program work?

Outline 31  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 34  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};    

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

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++; }

String 37  String is “array of chars.” string name = “Bossa”; Console.WriteLine(name[0]); Console.WriteLine(name[4-3]); name[4] = ‘m’; ‘B’‘o’‘s’ name 012 ‘s’ 3 ‘a' 4 B Output o  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 (int i = 0; i < arr.GetLength(0); i++) for (int j = 0; j < arr.GetLength(1); j++) arr[i,j] = i*10+j;

Array: 2D (cont.) Accessing one row (e.g., 3 rd 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]);

Array: 2D (cont.) 42 Accessing one column (e.g., 2 nd 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]);

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

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, 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

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

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

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

Outline 48  Major topics  Methods  Arrays  structs

struct  Define, Create, Access  Array of struct  struct with methods

Define struct StudentInfo Name: Paul Dept: Math Age: 18 Gender: M string int char struct StudentInfo { public string name; public string dept; public int age; public char gender; } 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

using System; class StructTest { static void Main() { } Create struct: Format I & II struct_name var_name; StudentInfo Name: Dept: Age: Gender: string int char StdInfostudent = newStdInfo (); StdInfostudent; struct StdInfo { public string name; public string dept; public int age; public char gender; } 51 struct_name var_name = new struct_name ();

 Syntax student.name = “Paul”; Console.WriteLine(student.name); 52 Access struct var_name.member_name Paul Monitor Name: Dept: Age: Gender: string int char Assign value Display value Paul StdInfo student; 52

Declare & Create Array of struct  Declare Array  Declare Array of struct double[] score; score = new double[4]; StdInfo[] student; student = new StdInfo[4]; score 0123 id name dept id name dept id name dept id name dept 0123 student struct StdInfo { public int id; public string name; public string dept; } 53

Access Array of struct  Access Array  Access Array of struct score[2] = 5; student[0].name = ”Paul"; id name dept id name dept id name dept id name dept 0123 student score student[3].id = 713; 713 Paul 54 var_name[index].struct_member

Example : Read 1 Student’s Information class 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

Example : Read 50 Students’ Information class 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 50 students

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); } id name dept 601id name dept 623 … id name dept Pam Math 0149 student 713 BobPaul MathEnglish 57

Methods: passing with struct struct 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+2i 6+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

Good Luck