Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010.

Slides:



Advertisements
Similar presentations
8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day.
Advertisements

1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Lectures 10 & 11.
Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.
Introduction to arrays Data in economy size packages.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
C# Programming: From Problem Analysis to Program Design1 Arrays C# Programming: From Problem Analysis to Program Design 3 rd Edition 7.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
Review of C++ Programming Part II Sheng-Fang Huang.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
 2006 Pearson Education, Inc. All rights reserved Arrays.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn.
Methods Session 04 Mata kuliah: M0874 – Programming II Tahun: 2010.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
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.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Array in C# Array in C# RIHS Arshad Khan
Examples of Classes & Objects
C# Programming: From Problem Analysis to Program Design
Pass by Reference, const, readonly, struct
CSC 113 Tutorial QUIZ I.
7 Arrays.
Classes & Objects: Examples
Variables and Their scope
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
JavaScript Arrays.
CS2011 Introduction to Programming I Arrays (I)
7 Arrays.
Presentation transcript:

Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University 3 Outline Materi Arrays Declaring and Allocating Arrays Examples Using Arrays Passing Arrays to Methods Passing Arrays by Value and Reference Foreach Repetition Structure

Bina Nusantara University 4 Arrays An array is a group of contiguous memory locations that al have the same name and type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the element to which we refer. A program can refer to any element of an array by giving the name of the array followed by the position number of the element in square brackets ([]). The first element in every array is the zeroth element.

Bina Nusantara University 5 Declaring and Allocating Arrays Arrays occupy space in memory. String strEmotion = "happy"; String[] strEmotions = {"happy","sad","elated","afraid","angry","peaceful"}; int[] arr = new int[10]; double numbers = new double[5]; string[] names = new string[2]; By using the brackets [], the compiler knows that the variable strEmotions will hold more than one value.

Bina Nusantara University 6 Declaring and Allocating Arrays String [] strEmotions = {"happy","sad","elated","afraid","angry","peaceful"}; strEmotions[0] = happy Value in the 0 position of the array strEmotions[1] = sad Value in the 1 position of the array strEmotions[2] = elated Value in the 2 position of the array strEmotions[3] = afraid Value in the 3 position of the array strEmotions[4] = angry Value in the 4 position of the array strEmotions[5] = peaceful Value in the 5 position of the array

Bina Nusantara University 7 Examples Using Arrays If you do not initialize an array at the time of declaration, the array members are automatically initialized to the default initial value for the array type. Also, if you declare the array as a field of a type, it will be set to the default value null when you instantiate the type.

Bina Nusantara University 8 using System; namespace array_example1 { class Program { static void Main(string[] args) { int[] arr = new int[10]; for (int x = 0; x < 10; x++) { Console.WriteLine("Enter array element : {0}", x + 1); arr[x] = Int32.Parse(Console.ReadLine()); } foreach (int i in arr) { Console.WriteLine(i); } Console.ReadLine(); }

Bina Nusantara University 9 Passing Arrays to Methods To pass an array argument to a method, specify the name of the array without using brackets. For example, if array hourlyTemperatures declared as int[] hourlyTemperatures = new int [ 24 ]; The method call ModifyArray( hourlyTemperatures ); Every array object knows its own size (via the length instance variable).

Bina Nusantara University 10 Passing Arrays to Methods To receive an array through a method call, the method’s parameter list must specify that an array will be received. public void ModifyArray( int[] b )

Bina Nusantara University 11 Passing Arrays public class Example9 { static void ShowNumbers (params int[] numbers) { foreach (int x in numbers) { Console.Write (x+" "); } Console.WriteLine(); } public static void Main (string[] args) { int[] x = {1, 2, 3}; ShowNumbers (x); ShowNumbers (4, 5); }

12 Passing Arrays by Value and Reference

Bina Nusantara University 13 References