1 nd semester 2007 1 module7 arrays thanawin rakthanmanon email: [email protected] create by: aphirak...
TRANSCRIPT
11nd Semester 2007
ModuleModule77 ArraysArrays
Thanawin RakthanmanonEmail: [email protected]
Create by: Aphirak JansangComputer Engineering Department
Kasetsart University, Bangkok THAILAND
21nd Semester 2007
Outline
ArrayArray Overview Array Declaration & FunctionsArray Declaration & Functions ProblemsProblems
31nd Semester 2007
What is Array? (แถว)
““An An arrayarray is an indexed is an indexed collectioncollection of objects, all of the of objects, all of the same typesame type””
00
11
22
33
44
55
66
41nd Semester 2007
Why Array?
We want to store all the midterm scores of all students in a class with 60 students.
intint s1, s2, s3, s4, s5 s1, s2, s3, s4, s5,, s6, s7, s8, s9, s10; s6, s7, s8, s9, s10;iintnt s11, s12, s13, s14,s11, s12, s13, s14, s15s15,,s16, s17, s18s16, s17, s18;; :: ::intint s56, s57, s58, s59, s60; s56, s57, s58, s59, s60;
Declaration without ArrayDeclaration without Array
int int [] [] s s = = new new intint[[6060];];Declaration with ArrayDeclaration with Array
51nd Semester 2007
Array make our life easier We want to find the average of the
scores.
intint total=0, average; total=0, average;total = s1 + s2 + s3 + … + s60;total = s1 + s2 + s3 + … + s60;average = total / 60;average = total / 60;
Coding without ArrayCoding without Array
intint total=0, average; total=0, average;
for (for (intint i = 0; i<60; i++) i = 0; i<60; i++) total = total + s[i];total = total + s[i];
average = total / 60;average = total / 60;
Coding with ArrayCoding with Array
61nd Semester 2007
Outline
ArrayArray Overview Array Declaration & FunctionsArray Declaration & Functions ProblemsProblems
71nd Semester 2007
Array Declaration
Syntax: Declaration only
Declaration with creation
Declaration with initialization: 2 ways
<type> [] <name>;
<type> [] <name> = newnew <type>[<num-elts>];
<type> [] <name> = {<value-lists>};22
<type> [] <name> = newnew <type>[<num-elts>] {<value-lists>};
11
81nd Semester 2007
Array Declaration Example
int [] ai;
int [] ai = newnew int[5];
int [] ai = newnew int[5] {1, 2, 3, 4, 5};
int [] ai = {1, 2, 3, 4};
Declaration only
Declaration with creation
Declaration with initialization: 2 ways
91nd Semester 2007
scorescore 55 66 77 99
scorescore
More examples
int [] score = newnew int[6];
1100 22 33 44 55
int [] score = newnew int[4] {5,6,7,9};
1100 22 33
101nd Semester 2007
Accessing Array Elements Supply an integer index for accessing
array elements indexes are zero-basedindexes are zero-based
intint [] score = [] score = newnew intint[4][4];;
1100 22 33scorescore
score[0] = -score[0] = -33;;score[score[2+12+1] = ] = 77;;Score[3-1] = score[0]+score[3]Score[3-1] = score[0]+score[3]Console.WriteLine(score[Console.WriteLine(score[22]);]);
--33 77
44
44
111nd Semester 2007
How to find Array’s length?
By property Length
By method GetLength()
int int [] [] matrix matrix = = new new intint[[55];];
ConsoleConsole..WriteLineWriteLine((matrixmatrix..LengthLength););
int int [] [] matrix matrix = = new new intint[[77];];
ConsoleConsole..WriteLineWriteLine((matrixmatrix..GetLengthGetLength((00));));
55
77
121nd Semester 2007
Outline
ArrayArray Overview Array Declaration & FunctionsArray Declaration & Functions ProblemsProblems
131nd Semester 2007
Example1
Write a program for calculating summation of 82 student's scoresintint[] [] a a = = new new intint[82];[82];
forfor((int int ii==00; ; ii<<aa..LengthLength; ; ii++) ++)
{{
aa[[ii] = ] = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
}}
int int sumsum==00;;
forfor((int int ii==00; ; ii<(<(aa..LengthLength); ); ii++)++)
{{
sum sum = = sum sum + (+ (aa[[ii]);]);
}}
ConsoleConsole..WriteLineWriteLine((“Sum is {1}{0}"“Sum is {1}{0}",,sum,isum,i););
141nd Semester 2007
Example 2
Write a program for calculatingsummation of square (5 numbers, without array)
int int aa, , bb, , cc, , dd, , ee;;
a a = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
b b = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
c c = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
d d = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
e e = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
int int square_sum square_sum = = aa**a a + + bb**b b + + cc**c c + + dd**d d + + ee**ee;;
ConsoleConsole..WriteLineWriteLine(("Sum of square is {0}""Sum of square is {0}", , square_sumsquare_sum););
4*4 + 3*3 + 7*7 + 2*2 + 4*4 + 3*3 + 7*7 + 2*2 + 5*55*5
Quiz1Quiz1
151nd Semester 2007
Example 2
Write a program for calculatingsummation of square (5 numbers, with array!)
int int aa, , bb, , cc, , dd, , ee;;
a a = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
b b = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
c c = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
d d = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
e e = = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());
int int square_sum square_sum = = aa**a a + + bb**b b + + cc**c c + + dd**d d + + ee**ee;;
ConsoleConsole..WriteLineWriteLine(("Sum of square is {0}""Sum of square is {0}", , square_sumsquare_sum););
4*4 + 3*3 + 7*7 + 2*2 + 5*54*4 + 3*3 + 7*7 + 2*2 + 5*5
Quiz2Quiz2
intint[] [] a a = = new new intint[[55];];
forfor((int int ii==00; ; ii<<aa..LengthLength; ; ii++)++){{ aa[[ii] = ] = intint..ParseParse((ConsoleConsole..ReadLineReadLine());());}}
int int square_sumsquare_sum==00;;forfor((int int ii==00; ; ii<(<(aa..LengthLength); ); ii++)++){{
square_sum square_sum = = square_sum square_sum + (+ (aa[[ii]*]*aa[[ii]);]);}}
161nd Semester 2007
Example2
Write the program for counting the alphabets in input string!!!
171nd Semester 2007
Looping or Iteration in C#
forfor
whilewhile
do…whiledo…while
foreachforeachIterationIteration
181nd Semester 2007
foreach
foreach is used to access 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
// the above statement will be applied to each element in an array
191nd Semester 2007
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++;
}