Download presentation
Presentation is loading. Please wait.
1
Thanachat Thanomkulabut
Array 1 Thanachat Thanomkulabut
2
Outline Introduction Declaration, Creation, Initialization, Access
Length of Array Examples foreach
3
Introduction If you want to declare variable for store scores of 700 students in 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 + s s700; int total=0; for(int i=0; i<700;i++) total += s[i];
4
What is Array? An array is an indexed collection of objects, all of the same type 1 2 3 4 5 6
5
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.
6
Outline Introduction Declaration, Creation, Initialization, Access
Length of Array Examples foreach
7
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;
8
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
9
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];
10
Array Declaration & Creation
double[] score; score = new double[5]; score
11
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};
12
Summary Declaration only int [] ai; Declaration & creation
int [] 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};
13
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];
14
Self Test II Name : text Type : Array of char Length : 3
Declare, Create and, Initialize the following array Name : text Type : Array of char Length : 3 Initial value : 'c', 'o', 'm'
15
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'};
16
Access Array Elements ID 1 2 3 score int [] score = new int[4];
Supply an integer index for accessing array elements Indexes are zero-based int [] score = new int[4]; ID 1 2 3 score
17
Access Array Elements (2)
int [] score = new int[4]; 1 2 3 score Runtime 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
18
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
19
Outline Introduction Declaration, Creation, Initialization, Access
Length of Array Examples foreach
20
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(int i=0;i<4;i++) Console.WriteLine (“Score{0} = {1}”,i+1,score[i]);
21
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
22
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(int i=0;i<score.Length;i++) for(int i=0;i<4;i++) Console.WriteLine (“Score{0} = {1}”,i+1,score[i]); for(int i=0;i<score.Length;i++)
23
Outline Introduction Declaration, Creation, Initialization, Access
Length of Array Examples foreach
24
Output Example 4 Month4 has 30 days. 1 2 3 4 5 6 7 8 9 10 11 month This program report 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,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 reports the number of days in a given month. //assume Feb have 28 days
25
Example 2 Write a program to calculate summation of n numbers Monitor
1 2 3 Input n: 4 16 3 5 1 7 3 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; sum = sum+num[i]; Console.WriteLine("Sum = {0}",sum); }
26
Element in string can be 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’; Element in string can be read only
27
String Counting number of letter ‘a’ in a sentence count i I words a m
1 2 3 4 5 6 7 8 9 10 I words a m a m a n 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 3 2 1
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.ToUpper(); while (i < words.Length) { value = words[i]-‘A'; if (value<26 && value>=0) count[value]++; i++; } // loop for printing results for(int j=0; j<26;j++) { if (count[j]!=0) Console.WriteLine( “{0}={1}“, Convert.ToChar(j+65), count[j]); } Output A= 3 I=1 M=2 N=1
29
Self Test III Write a program using array to calculate summation of 5 squared numbers 4* *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);
30
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); }
31
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
32
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);
33
Outline Introduction Declaration, Creation, Initialization, Access
Length of Array Examples foreach
34
Looping or Iteration in C#
while Iteration for foreach do…while
35
foreach 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;
36
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++; }
37
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++; }
38
foreach is used to only read elements in array
Example 3 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
39
Any question?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.