Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 nd Semester 2007 1 Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.

Similar presentations


Presentation on theme: "1 nd Semester 2007 1 Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart."— Presentation transcript:

1 1 nd Semester 2007 1 Module7 Arrays Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Create by: Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

2 1 nd Semester 2007 2 Outline  Array  Array Overview  Array Declaration & Functions  Problems

3 1 nd Semester 2007 3 What is Array? ( แถว ) “An array is an indexed collection of objects, all of the same type” 0 1 2 3 4 5 6

4 1 nd Semester 2007 4 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

5 1 nd Semester 2007 5 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

6 1 nd Semester 2007 6 Outline  Array  Array Overview  Array Declaration & Functions  Problems

7 1 nd Semester 2007 7 Array Declaration Syntax: Declaration only Declaration with creation Declaration with initialization: 2 ways [] ; new [] = new [ ]; [] = { }; 2 new [] = new [ ] { }; 1

8 1 nd Semester 2007 8 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

9 1 nd Semester 2007 9 score 5679 score More examples new int [] score = new int[6]; 102 345 new int [] score = new int[4] {5,6,7,9}; 102 3

10 1 nd Semester 2007 10 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]); -3-3-3-3 7 4 4

11 1 nd Semester 2007 11 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

12 1 nd Semester 2007 12 Outline  Array  Array Overview  Array Declaration & Functions  Problems

13 1 nd Semester 2007 13 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);

14 1 nd Semester 2007 14 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

15 1 nd Semester 2007 15 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]); }

16 1 nd Semester 2007 16 Example2  Write the program for counting the alphabets in input string!!!

17 1 nd Semester 2007 17 Looping or Iteration in C# for while do…while foreach Iteration

18 1 nd Semester 2007 18 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

19 1 nd Semester 2007 19 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++; }


Download ppt "1 nd Semester 2007 1 Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart."

Similar presentations


Ads by Google