1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND
1 nd Semester Outline Array Array Overview Array Declaration & Functions Problems
1 nd Semester What is Array? ( แถว ) “An array is an indexed collection of objects, all of the same type”
1 nd Semester Why Array? We want to store all the midterm scores of all students in a class with 60 students. int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10; int s11, s12, s13, s14, s15,s16, s17, s18; : : int s56, s57, s58, s59, s60; Declaration without Array int [] s = new int[60]; Declaration with Array
1 nd Semester Array make our life easier We want to find the average of the scores. int total=0, average; total = s1 + s2 + s3 + … + s60; average = total / 60; Coding without Array int total=0, average; for (int i = 0; i<60; i++) total = total + s[i]; total = total + s[i]; average = total / 60; Coding with Array
1 nd Semester Outline Array Array Overview Array Declaration & Functions Problems
1 nd Semester Array Declaration Syntax: Declaration only Declaration with creation Declaration with initialization: 2 ways [] ; new [] = new [ ]; [] = { }; 2 new [] = new [ ] { }; 1
1 nd Semester Array Declaration Example int [] ai; new int [] ai = new int[5]; new int [] ai = new int[5] {1, 2, 3, 4, 5}; int [] ai = {1, 2, 3, 4}; Declaration only Declaration with creation Declaration with initialization: 2 ways
1 nd Semester score 5679 score More examples new int [] score = new int[6]; new int [] score = new int[4] {5,6,7,9}; 102 3
1 nd Semester Accessing Array Elements Supply an integer index for accessing array elements indexes are zero-based int [] score = new int[4]; 1023score score[0] = -3; score[2+1] = 7; Score[3-1] = score[0]+score[3] Console.WriteLine(score[2]);
1 nd Semester How to find Array’s length? By property Length By method GetLength() int [] matrix = new int[5]; Console.WriteLine(matrix.Length); int [] matrix = new int[7]; Console.WriteLine(matrix.GetLength(0)); 5 7
1 nd Semester Outline Array Array Overview Array Declaration & Functions Problems
1 nd Semester Example1 Write a program for calculating summation of 82 student's scores int[] a = new int[82]; for(int i=0; i<a.Length; i++) { a[i] = int.Parse(Console.ReadLine()); a[i] = int.Parse(Console.ReadLine());} int sum=0; for(int i=0; i<(a.Length); i++) { sum = sum + (a[i]); } Console.WriteLine(“Sum is {1}{0}",sum,i);
1 nd Semester Example 2 Write a program for calculating summation of square (5 numbers, without array) 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); 4*4 + 3*3 + 7*7 + 2*2 + 5*5 Quiz1
1 nd Semester Example 2 Write a program for calculating summation of square (5 numbers, with array! ) 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); 4*4 + 3*3 + 7*7 + 2*2 + 5*5 Quiz2 int[] a = new int[5]; for(int i=0; i<a.Length; i++) { a[i] = int.Parse(Console.ReadLine()); a[i] = int.Parse(Console.ReadLine());} int square_sum=0; for(int i=0; i<(a.Length); i++) { square_sum = square_sum + (a[i]*a[i]); }
1 nd Semester Example2 Write the program for counting the alphabets in input string!!!
1 nd Semester Looping or Iteration in C# for while do…while foreach Iteration
1 nd Semester 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
1 nd Semester 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++; }