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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Language.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Java Syntax Primitive data types Operators Control statements.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
STRUCT Thanachat Thanomkulabut 1. Array Review 2  Group multiple items of the same type into one "variable" double[] score; score = new.
ILM Proprietary and Confidential -
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
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.
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)
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
1 st Semester Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Multidimensional Arrays Computer and Programming.
1 st Semester Module11 Struct Type in C# Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Department of Computer Science Western Michigan University
Test 2 Review Outline.
C# — Console Application
C# Arrays.
Chapter 7: Working with Arrays
Two Dimensional Array Mr. Jacobs.
Two-Dimension Arrays Computer Programming 2.
Parameter Passing & Array Examples – Part 1
For loops, nested loops and scopes
Arrays Computer & Programming Group
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Arrays in C.
C++ Arrays.
CS 1430: Programming in C++.
Lists in Python.
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
C# Programming.
Thanachat Thanomkulabut
MSIS 655 Advanced Business Applications Programming
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
Module8 Multi-dimensional Array
Thanachat Thanomkulabut
CSE Module 1 A Programming Primer
Thanachat Thanomkulabut
Suggested self-checks: Section 7.11 #1-11
Array ISYS 350.
Arrays.
Thanachat Thanomkulabut
Arrays and Strings CSCI293 - C# September 26, 2005.
C++ Array 1.
CS31 Discussion 1H Fall18: week 6
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

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