390 likes | 528 Views
Array 1. Thanachat Thanomkulabut. Outline. Introduction Declaration, Creation, Initialization, Access Length of Array Examples foreach. Introduction. If you want to declare variable for store scores of 700 students in 01204111 course, If you want to find total score,.
E N D
Array 1 Thanachat Thanomkulabut
Outline • Introduction • Declaration, Creation, Initialization, Access • Length of Array • Examples • foreach
Introduction • If you want to declare variable for store scores of 700 students in 01204111 course, • If you want to find total score, 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 collectionof objects, all of the same type 0 1 2 3 4 5 6
Overview of Array • 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 • Introduction • Declaration, Creation, Initialization, Access • Length of Array • Examples • foreach
Array Declaration • Declare variable • Name : x • Type : int • Syntax • <type> <name>; • Declare Array • Name : A • Type : int • Syntax • <type> [] <name>; int x; int [] A;
Array Creation • Array differs from ordinary variable • Array can hold multiple values while ordinary variable holds only one. • We need to reserve how many values array will hold. • Reservation = Array Creation
Array Declaration & Creation • Format I • <type> [] <name>; • <name> = new <type>[<numElements>]; • Format II • <type> [] <name> = new <type>[<numElements>]; double[] score; score = new double[5]; double[] score = new double[5];
Array Declaration & Creation double[] score; score = new double[5]; score
Array Declaration & Creation & Initialization • Format I • <type> [] <name> = new <type>[numElements] {<valueList>}; • int [] arrayA = new int[3] {3, 6, 9}; • Format II • <type> [] <name> = new <type>[] {<valueList>}; • int [] arrayA = new int[] {3, 6, 9}; • Format III • <type> [] <name> = {<valueList>}; • int[] arrayA = {3, 6, 9};
Summary • Declaration only • int [] ai; • Declaration & creation • int [] ai =newint[4]; • Declaration & creation & initialization • int [] ai =newint[4]{1, 2, 3, 4}; • int [] ai = newint[]{1, 2, 3, 4}; • int [] ai = {1, 2, 3, 4};
Self Test I • Declare, Create and, Initialize the following array • 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 the following array • 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 IV char[] text = new char[]{'c','o','m'}; Format V char[] text = {'c','o','m'};
Access Array Elements • Supply an integer index for accessing array elements • Indexes are zero-based int [] score = newint[4]; ID 0 1 2 3 score
3 0 1 2 score Access Array Elements (2) int [] score = newint[4]; Runtime Error!! -3 4 7 score[4] = 5; score[0] = -3; score[2+1] = 7; Score[3-1] = score[0]+score[3] Console.WriteLine(score[2]); 4
score score 5 6 7 9 More Examples int [] score = newint[6]; 3 4 5 0 1 2 int [] score = newint[4]{5,6,7,9}; 3 0 1 2
Outline • Introduction • Declaration, Creation, Initialization, Access • Length of Array • Examples • foreach
Example • Initial 0 value for every element of array (assume length of array is 6) • Show all values in each element of array (assume length of array is 4) for(inti=0;i<6;i++) score[i] = 0; for(int i=0;i<4;i++) Console.WriteLine (“Score{0} = {1}”,i+1,score[i]);
How to find length of array? • By property Length int[] matrix = new int[5]; Console.WriteLine(matrix.Length); 5 • By method GetLength() int[] matrix = new int[7]; Console.WriteLine(matrix.GetLength(0)); 7
Example • Initial 0 value for every element of array (assume length of array is 6) • Show all values in each element of array (assume length of array is 4) for(int i=0;i<6;i++) score[i] = 0; for(inti=0;i<score.Length;i++) for(int i=0;i<4;i++) Console.WriteLine (“Score{0} = {1}”,i+1,score[i]); for(inti=0;i<score.Length;i++)
Outline • Introduction • Declaration, Creation, Initialization, Access • Length of Array • Examples • foreach
Output Example 4 Month4 has 30 days. 10 11 4 5 7 8 9 1 2 3 6 0 month • This program 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 • This program report the number of days in a given month. //assume Feb have 28 days 4 monthday 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
Monitor Example 2 sum 0 2 3 1 Input n: 4 3+5+1+7 0 3 5 1 7 16 3 num 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.ReadLine()); int[] num = new int[n]; for(int i=0;i<num.Length;i++) num[i] = int.Parse(Console.ReadLine()); int sum = 0; for(int i=0;i<num.Length;i++) sum = sum+num[i]; Console.WriteLine("Sum = {0}",sum); }
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’; Element in string can be read only
String i 2 3 4 10 0 1 5 6 7 8 9 I a n a m a m words • Counting number of letter ‘a’ in a sentence string words = “I am a man”;int count = 0;int i = 0;while (i < words.Length) {if (words[i] == 'a') count++; i++;}Console.WriteLine("Number of letter \'a\' = {0}", count); = 10 X False Output count Number of letter ‘a’ = 3 1 2 3 0
String: Counting number of each letter in a sentence Output A= 3 I=1 M=2 N=1
Self Test III Write a program using array to calculatesummation of 5 squared numbers 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; for(int i=0;i<num.Length;i++) squre_sum += num[i]*num[i]; Console.WriteLine("Sum = {0}",square_sum); }
Self Test IV • 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 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); }
Outline • Introduction • Declaration, Creation, Initialization, Access • Length of Array • Examples • foreach
Looping or Iteration in C# while Iteration foreach for do…while
foreach • foreach is used to (only) readeach 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 string s = Console.ReadLine(); intcount = 0; for (inti= 0; i< s.Length; i++){ if (s[i] == 'A') count++; } string s = Console.ReadLine(); intcount = 0; foreach(char c in s){ if (c == 'A') count++; }
Example 2 int[] score = newint[5]{4,9,0,8,7}; intcount = 0; foreach(intx in score){ if(x%2 == 0) count++; }
Example 3 int[] num = new int[5]{2, 4, 5, 8, 0}; foreach( intninnum){ n += 10; } foreach is used to only read elements in array