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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
C#: Statements and Methods Based on slides by Joe Hummel.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Array and ArrayList ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items in an.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
int [] scores = new int [10];
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Multidimensional Arrays Computer and Programming.
1 st Semester Module11 Struct Type in C# Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
ARRAYS Multidimensional realities Image courtesy of
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Arrays Chapter 7.
C# Arrays.
Arrays Computer & Programming Group
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
C++ Arrays.
Array ISYS 350.
CS 1430: Programming in C++.
Array ISYS 350.
CSC 142 Computer Science II
Arrays, For loop While loop Do while loop
Array ISYS 350.
Array ISYS 350.
int [] scores = new int [10];
Introduction To Programming Information Technology , 1’st Semester
Thanachat Thanomkulabut
Arrays Chapter 7.
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
Module8 Multi-dimensional Array
Module 8 & 9 Method :Part I & Array :Part II
Loops.
Array ISYS 350.
Thanachat Thanomkulabut
Presentation transcript:

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++; }