ARRAY 1 Thanachat Thanomkulabut Outline 2 Introduction Declaration
ARRAY 1 Thanachat Thanomkulabut
Outline 2 Introduction Declaration, Creation, Initialization, Access Length of Array Examples foreach
Introduction 3 If you want to declare variable for store scores of 700 students in 01204111 course, int s 1, s 2, s 3, s 4, s 5, . . . , s 700; int[] s = new int[700]; If you want to find total score, int total=0; total = s 1 + s 2 + s 3 + s 4 +. . . + s 700; int total=0; for(int i=0; i<700; i++) total += s[i];
What is Array? 4 An array is an indexed collection of objects, all of the same type 0 1 2 3 4 5 6
Overview of Array 5 n n We create arrays using new command. Array elements start with index 0. Array “a” with 6 elements: a[0], a[1], . . . , a[5]. Each element in the array behaves like standard variable.
Outline 6 Introduction Declaration, Creation, Initialization, Access Length of Array Examples foreach
Array Declaration 7 Declare variable Name : A Type : int Name : x Type : int Syntax <type> <name>; int x; Declare Array Syntax <type> [] <name>; int [] A;
Array Creation 8 n Array differs from ordinary variable n Array can hold multiple values while ordinary variable holds only one. n We need to reserve how many values array will hold. n Reservation = Array Creation
Array Declaration & Creation 9 Format I <type> [] <name>; <name> = new <type>[<num. Elements>]; double[] score; score = new double[5]; Format II <type> [] <name> = new <type>[<num. Elements>]; double[] score = new double[5];
Array Declaration & Creation 10 double[] score; score = new double[5]; score
11 Array Declaration & Creation & Initialization Format I <type> [] <name> = new <type>[num. Elements] {<value. List>}; int [] array. A = new int[3] {3, 6, 9}; Format II <type> [] <name> = new <type>[] {<value. List>}; int [] array. A = new int[] {3, 6, 9}; Format III <type> [] <name> = {<value. List>}; int[] array. A = {3, 6, 9};
Summary 12 Declaration int [] only ai; Declaration int [] & creation ai = new int[4]; Declaration & creation & initialization int [] ai = new int[4] {1, 2, 3, 4}; int [] ai = new int[] {1, 2, 3, 4}; int [] ai = {1, 2, 3, 4};
Self Test I 13 Declare, Create and, Initialize the following array § § Name : times Type : array of double Format I double[] times; Length : 5 times = new double[5]; Initial value : None Format II double[] times = new double[5];
Self Test II 14 Declare, Create and, Initialize the following array § § Name : text Type : Array of char Length : 3 Initial value : 'c', 'o', 'm'
Self Test II 15 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 IV char[] text = new char[]{'c', 'o', 'm'}; Format V char[] text = {'c', 'o', 'm'};
Access Array Elements 16 Supply an integer index for accessing array elements Indexes are zero-based int [] score = new int[4]; ID score 0 1 2 3
Access Array Elements (2) 17 int [] score = new int[4]; 0 score -3 1 2 3 4 7 Runtime Error!! score[4] = 5; score[0] = -3; score[2+1] = 7; Score[3 -1] = score[0]+score[3] Console. Write. Line(score[2]); 4
More Examples 18 int [] score = new int[6]; score 0 1 2 3 4 5 int [] score = new int[4] {5, 6, 7, 9}; score 0 5 1 6 2 7 3 9
Outline 19 Introduction Declaration, Creation, Initialization, Access Length of Array Examples foreach
Example 20 Initial 0 value for every element of array (assume length of array is 6) for(int i=0; i<6; i++) score[i] = 0; Show all values in each element of array (assume length of array is 4) for(int i=0; i<4; i++) Console. Write. Line (“Score{0} = {1}”, i+1, score[i]);
How to find length of array? 21 By property Length int[] matrix = new int[5]; Console. Write. Line(matrix. Length); n 5 By method Get. Length() int[] matrix = new int[7]; 7 Console. Write. Line(matrix. Get. Length(0));
Example 22 Initial 0 value for every element of array (assume length of array is 6) for(int i=0; i<6; i++) i=0; i<score. Length; i++) score[i] = 0; Show all values in each element of array (assume length of array is 4) for(int i=0; i<4; i++) i=0; i<score. Length; i++) Console. Write. Line (“Score{0} = {1}”, i+1, score[i]);
Outline 23 Introduction Declaration, Creation, Initialization, Access Length of Array Examples foreach
Output Example 24 0 1 4 Month 4 has 30 days. 2 3 4 5 6 7 8 9 10 11 month 31 28 31 30 31 This program report the number of days in a 4 given month. //assume Feb have 28 days static void Main(){ monthday int[] monthday = {31, 28, 31, 30, 31}; int month = int. Parse(Console. Read. Line()); } Console. Write(“Month{0} has {1} days. ”, monthday[month-1]); 4 3 This program reports the number of days in a given month. //assume Feb have 28 days
Monitor 0 2 3 1 sum Input n: 4 3+5+1+7 0 16 3 num 3 5 1 7 25 5 1 Write a program to calculate summation of n numbers 7 Sum = 16 static void Main(){ Console. Write("Input n: "); int n = int. Parse(Console. Read. Line()); int[] num = new int[n]; Example 2 for(int i=0; i<num. Length; i++) num[i] = int. Parse(Console. Read. Line()); int sum = 0; for(int i=0; i<num. Length; i++) sum = sum+num[i]; Console. Write. Line("Sum = {0}", sum); }
String 0 1 2 3 4 name B o s s a Output B o 26 String is array of chars. string name = “Bossa”; Console. Write. Line(name[0]); Console. Write. Line(name[4 -3]); name[4] = ‘m’; Element in string can be read only
String i 0 words I 1 2 3 a m 4 5 6 7 8 9 10 m a n a 27 Counting number of letter ‘a’ in a sentence string words = “I am a man”; int count = 0; = 10 int i = 0; while (i < words. Length) X False { if (words[i] == 'a') count++; i++; } Console. Write. Line("Number of letter 'a' = {0}", count); Output Number of letter ‘a’ = 3 count 3 2 1 0
28 String: Counting number of each letter in a sentence string words = "I am a man"; int[] count = new int[26]; int value, i = 0; // convert all letters to uppercase first words = words. To. Upper(); while (i < words. Length) { value = words[i]-‘A'; if (value<26 && value>=0) count[value]++; i++; } Output A= 3 I=1 M=2 N=1 // loop for printing results for(int j=0; j<26; j++) { if (count[j]!=0) Console. Write. Line( “{0}={1}“, Convert. To. Char(j+65), count[j]); }
Self Test III 29 Write a program using array to calculate summation of 5 squared numbers 4*4 + 3*3 + 7*7 + 2*2 + 5*5 int a, b, c, d, e; a = int. Parse(Console. Read. Line()); b = int. Parse(Console. Read. Line()); c = int. Parse(Console. Read. Line()); d = int. Parse(Console. Read. Line()); e = int. Parse(Console. Read. Line()); int square_sum = a*a + b*b + c*c + d*d + e*e; Console. Write. Line("Sum of square is {0}", square_sum);
Self Test III 30 static void Main(){ int[] num = new int[5]; for(int i=0; i<num. Length; i++) num[i] = int. Parse(Console. Read. Line()); int square_sum = 0; for(int i=0; i<num. Length; i++) squre_sum += num[i]*num[i]; Console. Write. Line("Sum = {0}", square_sum); }
Self Test IV 31 Write a program to receive n numbers & find maximum value. Process Read value of n Read n numbers, and store each read number in element of array one at a time Find maximum among n numbers
Self Test IV 32 static void Main(){ int n; n = int. Parse(Console. Read. Line()); double[] data = new double[n]; for(int i=0; i<n; i++) data[i] = double. Parse(Console. Read. Line()); double max=0; for(int i=0; i<n; i++){ if(data[i] > max) max = data[i]; } Console. Write. Line("max = {0}", max); }
Outline 33 Introduction Declaration, Creation, Initialization, Access Length of Array Examples foreach
Looping or Iteration in C# 34 while for Iteration do…while foreach
foreach 35 foreach is used to (only) read each element in an array and do some action repeatedly foreach = “for each element in an array” Syntax: foreach(var_declaration in array_name) statement;
Example 1 36 string s = Console. Read. Line(); int count = 0; for (int i = 0; i < s. Length; i++){ if (s[i] == 'A') count++; } string s = Console. Read. Line(); int count = 0; foreach (char c in s){ if (c == 'A') count++; }
Example 2 37 int[] score = new int[5]{4, 9, 0, 8, 7}; int count = 0; foreach (int x in score){ if(x%2 == 0) count++; }
Example 3 38 int[] num = new int[5]{2, 4, 5, 8, 0}; foreach( int n in num){ n += 10; } foreach is used to only read elements in array
ANY QUESTION?
- Slides: 39