Thanachat Thanomkulabut

Slides:



Advertisements
Similar presentations
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Advertisements

Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
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.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
Multidimensional Arrays Computer and Programming.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ARRAYS Multidimensional realities Image courtesy of
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Arrays. Arrays are objects that help us organize large amounts of information.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Arrays.
Array in C# Array in C# RIHS Arshad Khan
C# Basic Syntax, Visual Studio, Console Input / Output
C# — Console Application
C# Arrays.
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
ECE Application Programming
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.
Advanced Programming Chapter 8: Arrays
Arrays, For loop While loop Do while loop
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
An Introduction to Java – Part I, language basics
EKT150 : Computer Programming
Multidimensional Arrays
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
int [] scores = new int [10];
Lecture 4 2d Arrays CSE /26/2018.
Module8 Multi-dimensional Array
Multidimensional Arrays
Module 8 & 9 Method :Part I & Array :Part II
Thanachat Thanomkulabut
Dr Tripty Singh Arrays.
Lets Play with arrays Singh Tripty
Iteration (Loop) partII
Arrays.
EECE.2160 ECE Application Programming
Thanachat Thanomkulabut
Iteration (Loop) part II
Arrays and Strings CSCI293 - C# September 26, 2005.
EECE.2160 ECE Application Programming
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

Thanachat Thanomkulabut Arrays Thanachat Thanomkulabut

Introduction If you want to declare variable for store scores of all students in 204111 course If you want to find total of scores int s1, s2, s3, s4, s5, ... , s700; int[] s = new int[700]; int total=0; total = s1 + s2 + s3 + s4 + ... + s700; int total=0; for(int i=0; i<700;i++) total += s[i];

What is array? An array is an indexed collection of objects, all of the same type 1 2 3 4 5 6

Array We create arrays using new command. Array elements start with index 0. Array “a” with 6 elements has a[0], a[1], ... , a[5]. Each element in the array behaves like standard variables.

Array Declaration Declare Array Declare variable Syntax Syntax int x; Name : A Type : int Syntax <type> [] <name>; Declare variable Name : x Type : int Syntax <type> <name>; int x; int [] A;

Array Declaration & Creation Format I <type> [] <name>; <name> = new <type>[<num-elts>]; Format II <type> [] <name> = new <type>[<num-elts>]; double[] score; score = new double[5]; double[] score = new double[5];

Array Declaration & Creation double[] score; score = new double[5]; score id 1 2 3 4

Array Declaration & Creation & Initialization Format I <type> [] <name> = new <type>[num-elts] {<value-lists>}; int [] arrayA = new int[3] {3, 6, 9}; Format II new <type>[] {<value-lists>}; int [] arrayA = new int[] {3, 6, 9}; Format III <type> [] <name> = {<value-lists>}; int[] arrayA = {3, 6, 9};

Array Declaration Example Declaration only Declaration with creation Declaration with initialization int [] ai; int [] ai = new int[5]; int [] ai = new int[5] {1, 2, 3, 4, 5}; int [] ai = {1, 2, 3, 4};

More examples 1 2 3 4 5 score 1 2 3 score 5 6 7 9 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

Self test I Declare ,Create and Initialize following array Array I Name : times Type : Array of double Length : 5 Initial value : None Format I double[] times; times = new double[5]; Format II double[] times = new double[5];

Self test II Declare ,Create and Initialize following array Array I Name : text Type : Array of char Length : 3 Initial value : 'c', 'o', 'm'

Self test II Format I char[] text; text = new char[3]{'c','o','m'}; Format II char[] text; text = new char[]{'c','o','m'}; Format III char[] text = new char[3]{'c','o','m'}; Format IIII char[] text = new char[]{'c','o','m'}; Format IIII char[] text = {'c','o','m'};

Accessing Array Elements Supply an integer index for accessing array elements indexes are zero-based int [] score = new int[4]; 1 2 3 score Run time Error!! -3 4 7 score[0] = -3; score[2+1] = 7; Score[3-1] = score[0]+score[3] Console.WriteLine(score[2]); score[4] = 5; 4

Example1 Initial 0 value for every elements of array (assume length of array is 6) Show all value in each elements of array (assume length of array is 6) for(int i=0;i<6;i++) score[i] = 0; for(int i=0;i<6;i++) Console.WriteLine (“Score{0} = {1}”,i+1,score[i]);

How to find Array’s length? By property Length 5 int[] matrix = new int[5]; Console.WriteLine(matrix.Length); By method GetLength() 7 int[] matrix = new int[7]; Console.WriteLine(matrix.GetLength(0));

Example2 Initial 0 value for every elements of array (assume length of array is 6) Show all value in each elements of array (assume length of array is 6) for(int i=0;i<6;i++) score[i] = 0; for(int i=0;i<score.Length;i++) for(int i=0;i<6;i++) Console.WriteLine (“Score{0} = {1}”,i+1,score[i]); for(int i=0;i<score.Length;i++)

Output Example3 4 Month4 has 30 days. 1 2 3 4 5 6 7 8 9 10 11 month This program will reports the number of days in a given month. //assume Feb have 28 days 31 28 31 30 31 30 31 31 30 31 30 31 monthday 4 static void Main(){ } int[] monthday = {31,28,31,30,31,30, 31,31,30,31,30,31}; int month = int.Parse(Console.ReadLine()); Console.Write(“Month{0} has {1} days.”, month,monthday[month-1]); 4 3 This program will reports the number of days in a given month. //assume Feb have 28 days

Example24 Write the program for calculate sumation of n number Monitor 1 2 3 Input n: 4 16 3+5+1+7 3 5 1 7 3 5 1 Write the program for calculate sumation of n number 7 Sum = 16 static void Main(){ Console.Write("Input n: "); int n = int.Parse(Console.ReadLine()); int[] num = new int[n]; for(int i=0;i<num.Length;i++) num[i] = int.Parse(Console.ReadLine()); int sum = 0; sum = sum+num[i]; Console.WriteLine("Sum = {0}",sum); }

Self Test I Write a program for calculating summation of square (5 numbers, with array) 4*4 + 3*3 + 7*7 + 2*2 + 5*5 int a, b, c, d, e; a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine()); d = int.Parse(Console.ReadLine()); e = int.Parse(Console.ReadLine()); int square_sum = a*a + b*b + c*c + d*d + e*e; Console.WriteLine("Sum of square is {0}", square_sum);

Self test III static void Main(){ int[] num = new int[5]; for(int i=0;i<num.Length;i++) num[i] = int.Parse(Console.ReadLine()); int square_sum = 0; squre_sum += num[i]*num[i]; Console.WriteLine("Sum = {0}",square_sum); }

String in each elements can read only Output String 1 2 3 4 B name B o s s a o String is array of chars. string name = “Bossa”; Console.WriteLine(name[0]); Console.WriteLine(name[4-3]); name[4] = ‘m’;  String in each elements can read only

Looping or Iteration in C# while Iteration for foreach do…while

foreach foreach is used to access (read only) all elements in an array and do some action repeatedly foreach = “for each element in an array ” Syntax foreach(var_declaration in array_name) statement;

foreach example 1 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++; }

foreach example 2 int[] score = new int[5]{4,9,0,8,7}; int count = 0; foreach(int x in score){ if(x%2 == 0) count++; }

foreach is used to access (read only) foreach example 3 int[] num = new int[5]{2, 4, 5, 8, 0}; foreach(int n in num){ n += 10; }  foreach is used to access (read only)

Self test IV Write the Program to recieve n data & find max value of that data. Program Process Recieve “n” for check amount of data Recieve each number and collect it to each element in array Find max value of all data

Self test IV static void Main(){ int n; n = int.Parse(Console.ReadLine()); double[] data = new double[n]; for(int i=0;i<n;i++) data[i] = double.Parse(Console.ReadLine()); double max=0; for(int i=0;i<n;i++){ if(data[i] > max) max = data[i]; } Console.WriteLine("max = {0}",max);

Arrays Concept id 1 2 3 4 Array is a reference data type. Reference variables are used to refer to the data, not the data themselves. score Create Label double[] score; score = new double[5]; Reserve Memory id 1 2 3 4

Arrays with Methods :Example1 micky class AM { static void Main(){ int[] Arr = {1, 4, 5}; Console.WriteLine (“1:{0} {1} {2}”,Arr[0],Arr[1],Arr[2]); Add10(Arr); (“2:{0} {1} {2}”,Arr[0],Arr[1],Arr[2]); } static void Add10(int[] micky){ for(int i=0;i<micky.Length;i++) micky[i] += 10; Arr id 1 2 5 4 1 15 14 11 Output 1: 1 4 5 2: 11 14 15

Arrays with Methods :Example2 static void Main(){ char[] data; data = ReadArray(4); showArray(data); } static char[] ReadArray(int N){ char[] info = new char[N]; for(int i=0;i<N;i++) info[i] = char.Parse(Console.ReadLine()); return info; static void showArray(char[] Nobi){ for(int i=0;i<Nobi.Length;i++) Console.Write(Nobi[i]); data info Nobi info id 1 2 3 B e a r N=4 B e a r B e a r Monitor

Self test V What is the output of this partial program? static void Main(){ int[] mynum; mynum = new int[5] {4,0,-1,2,3}; Change(mynum); for(int i=0; i<mynum.Length; i++) Console.Write("{0} ",mynum[i]); } static void Change(int[] arr){ arr[0] = 5; arr[2] = 7; arr[3] = -8;

Self Test V 5 4 7 -1 -8 2 3 mynum arr id 1 2 3 4 static void Main(){ 1 2 3 4 5 4 7 -1 -8 2 3 mynum arr static void Main(){ int[] mynum; mynum = new int[5] {4,0,-1,2,3}; Change(mynum); for(int i=0; i<mynum.Length; i++) Console.Write("{0} ",mynum[i]); } static void Change(int[] arr){ arr[0] = 5; arr[2] = 7; arr[3] = -8; Output 5 0 7 -8 3

Example 5 What is the output of this partial program? Output NOBI IBON static void Main(){ char[] name1,name2; name1 = new char[4] {'N','O','B','I'}; name2 = reverse(name1); showArr(name1); showArr(name2); } static char[] reverse(char[] arr){ int j=0; char[] arr_re = new char[arr.Length]; for(int i=arr.Length-1;i>=0;i--){ arr_re[j] = arr[i]; j++; return arr_re; static void showArr(char[] arr){ foreach(char x in arr) Console.Write(x); Console.WriteLine(); What is the output of this partial program? Output NOBI IBON

Pass by value VS by reference We can only change the elements inside the array Pass by reference We can change the elements inside the array We can change the array that variable refers to

Pass by Value arr id 1 2 5 4 -1 mynum arr id 1 2 10 13 15 1 2 5 4 -1 static void Main(){ int[] mynum; mynum = new int[3] {4,0,-1}; Console.WriteLine ("Before mynum[0] = {0}",mynum[0]); Change(mynum); ("After mynum[0] = {0}",mynum[0]); } static void Change(int[] arr){ arr[0] = 5; arr = new int[3] {10,13,15}; ("In Change arr[0] = {0}",arr[0]); mynum arr id 1 2 10 13 15 Output Before mynum[0] = 4 In Change arr[0] = 10 After mynum[0] = 5

Pass by Reference mynum arr id 1 2 5 4 -1 mynum arr id 1 2 10 13 15 1 2 5 4 -1 static void Main(){ int[] mynum; mynum = new int[3] {4,0,-1}; Console.WriteLine ("Before mynum[0] = {0}",mynum[0]); Change(ref mynum); ("After mynum[0] = {0}",mynum[0]); } static void Change(ref int[] arr){ arr[0] = 5; arr = new int[3] {10,13,15}; ("In Change arr[0] = {0}",arr[0]); mynum arr id 1 2 10 13 15 Output Before mynum[0] = 4 In Change arr[0] = 10 After mynum[0] = 10

Multi-dimensional Array (introduction) If you want to keep score of 50 students If each student have 10 test double[] score = new double[50]; double[] score0 = new double[50]; double[] score1 = new double[50]; double[] score2 = new double[50]; double[] score9 = new double[50];

Multi-dimensional Array (introduction) 1 2 3 4 50 score 1 2 3 4 50 score0 score1 1 2 score2 9 score9 double[] score0 = new double[50]; double[] score1 = new double[50]; double[] score2 = new double[50]; double[] score9 = new double[50];

Multi-dimensional Array’s declaration <type> [] <name>; int [] score; Multi-dimensional 2 Dimension <type> [ , ] <name>; int [ , ] score; 3 Dimension <type> [ , , ] <name>; int [ , , ] score;

Multi-dimensional Array’s creation score 1 Dimension <name> = new <type>[<num-elts>]; score = new int[4]; 2 Dimension <name> = new <type>[<dim1>,<dim2>]; score = new int[4,2]; 3 Dimension <name> = new <type>[<dim1>,<dim2>,<dim3>]; score = new int[4, 2, 3]; score score

Multi-dimensional Array’s Initialization int[] score = new int[3] {6, 1, 3}; 2 Dimension int [,] score = new int[2, 3] { {1, 8, 4} , {3, 6, 9} }; int [,] score = { {1, 8, 4} , {3, 6, 9} }; int [,] score = { {1, 8, 4} , {3, 6, 9} }; score 6 1 3 score 1 8 4 3 6 9

Index of Multi-dimensional Array int[,] score = { {5,3,1}, {9,2,4} }; 1 2 score 5 3 3 7 3 1 3 1 9 2 4 score[0,1] = 7; score[2-1 , 1+1] = 0; Console.Write(score[0,0]); for(int i = 0; i<=2 ; i++) score[0,i] = 3; Console.Write(score.Length); 5 6

Selftest VI Declare, create and initialize array name Matrix Matrix Format I ‘v’ ‘a’ ‘p’ ‘y’ ‘q’ ‘s’ Matrix ‘z’ ‘b’ char[,] Matrix; Matrix = new char[4,2]{ {'v','a'}, {'y','q'}, {'p','z'}, {'s','b'} };

Array.GetLength() Get numbers of ROW in ARRAY score 1 2 5 3 9 4 Array.GetLength() Get numbers of ROW in ARRAY arrayname.GetLength(0); score.GetLength(0); Get numbers of Column in ARRAY arrayname.GetLength(1); score.GetLength(1); Get numbers of all elements in ARRAY arrayname.Length; score.Length; 2 3 6

Example 6 Student 1 Write the program to get score from 4 students (each student has 2 test) score1= 3 score2= 8 Student 2 score1= 6 score2= 7 Student 3 score1= 8 score2= 10 Student 4 score1= 9 score2= 7

Student 1 score1= score2= Student 2 Student 3 Student 4 3 8 6 7 10 9 Example 6 Write the program to get score from 4 students (each student has 2 test) double[,] score = new double[4,2]; for(int i=0;i<score.GetLength(0);i++){ Console.WriteLine("Student {0}",i+1); for(int j=0;j<score.GetLength(1);j++) { Console.Write("score{0} = ",j+1); score[i,j] = double.Parse(Console.ReadLine()); } Console.WriteLine(); score 1 2 3 3 8 6 7 8 10 9 7

Example 6 with Method static void Main(){ double [,] arr; arr = ReadArray2(2,4); } static double[,] ReadArray2(int row, int col){ double[,] score = new double[4,2]; for(int i=0;i<score.GetLength(0);i++){ Console.WriteLine("Student {0}",i+1); for(int j=0;j<score.GetLength(1);j++){ Console.Write("score{0} = ",j+1); score[i,j] = double.Parse(Console.ReadLine()); } Console.WriteLine(); return score;

score 1 2 3 8 6 7 10 9 Self test VI From example 6, write the partial program to find sumation of all scores of all students double sum = 0; for(int i=0;i<score.GetLength(0);i++){ for(int j=0; j<score.GetLength(1); j++){ sum += score[i,j]; } Console.WriteLine("Sumation = {0}",sum);

score 1 2 3 8 6 7 10 9 Self test VII From example 6, write the partial program to find average of scores of all students in first test double sum = 0; for(int i=0;i<score.GetLength(0);i++){ sum += score[i,0]; } double avg = sum/score.GetLength(0); Console.WriteLine ("Average of First test = {0}",avg);

score 1 2 3 8 6 7 10 9 Self test VIII From example 6, write the partial program to find average scores of all test of last student double sum = 0; for(int j=0;j<score.GetLength(1);j++){ sum += score[3,j]; } double avg = sum/score.GetLength(1); Console.WriteLine ("Average of last student = {0}",avg);

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

Any question?