1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Arrays.
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.
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.
Chapter 10 Introduction to Arrays
Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
A First Book of ANSI C Fourth Edition
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 9 (part of) Single-dimensional Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference.
Chapter 8 Arrays and Strings
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Chapter 8: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
1 Chapter 11 Arrays. 2 Chapter 11 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 Chapter 12-2 Arrays Dale/Weems. 2 Using Arrays as Arguments to Functions Generally, functions that work with arrays require 2 items of information n.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Review of Java1 Quick Review of ICS 102 Primitive and Reference Types Initializing Class Variables Defining Constructors How to Create a String How to.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Programming in C++ Dale/Weems/Headington Chapter 11 One-Dimensional Arrays.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
C++ Data Types Simple Structured Address Integral Floating
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.
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Presentation transcript:

1 Chapter 11 One-Dimensional Arrays

2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array l Manipulating the elements in an array l Using an array to count frequencies l Passing an array to a method

3 VB.NET Data Types Reference Array Interface Class primitive Integral Boolean Byte Char Short Integer Long Single Double Floating Point VB.NET Data Types

Scalar Data Type A scalar data type is a type in which l the values are ordered and each value is atomic (indivisible( 不可分割 )) Integer, Single, Double and Char data types are scalar.

5 Declare variables to store and total 3 blood pressures Dim bp0, bp1, bp2 As Integer Dim total As Integer bp1bp0bp2 Total = bp0 + bp1 + bp2

Composite Data Type A composite data type( 複合資料型態 ) is a type that l allows a collection of values to be associated with an identifier of that type. l In VB.NET, composite types are either classes, interfaces, or arrays. l There are 2 forms of composite types: unstructured and structured.

7 Structured Data Type( 結構化 ) A structured data type is a type which l is an organized collection of components; and allows individual components to be stored and retrieved. l The organization determines the method used to access individual components. l An array is a structured data type whose components are accessed by position.

8 What if you wanted to store and total 1000 values? Dim value(1000) As Integer ' declares and instantiates (creates) ' an array of 1000 int values ' and initializes all 1000 elements to zero value(0) value (1) value (2).... value (999)

Arrays( 陣列 ) Arrays are data structures( 資料結構 ) consisting of related data items all of the same type( 相同資料 型態 ). l An array type is a reference type. Contiguous memory locations( 連續記憶體位置 ) are allocated for the array, beginning at the base address of the array. l A particular element in the array is accessed by using the array name together with the position of the desired element in square brackets. The position is called the index or subscript( 索引 ).

10 angle Dim angle(4) As Single

11 Dim angle(4) As Single angle ( 0) angle ( 1) angle ( 2 ) angle ( 3 ) angle

12 Array Definition An array is a collection of elements, all of the same data type( 相同資料型態 ), given a single name. The subscript (or index) must have an integral value. In VB.NET, the first array element always has subscript 0. The second array element has subscript 1, etc. When allocated, the elements are automatically initialized to 0 for numeric primitive data type values, to False for Boolean variables, or to Nothing for references (non-primitive type values).

13 Another Example Declare and instantiate an array called angle to hold 4 individual double values. Dim angle ( 4 ) As Single ' declares and allocates memory angle(0) angle(1) angle(2) angle(3) number of elements in the array indexes or subscripts

14 Using an initializer list in a declaration Dim age( ) As Integer = { 23, 10, 16, 37, 12} Dim i As Integer For i = 0 to ages.Length – 1 Console.WriteLine("age("& i & ") = "& age(i) ) Next i age(0) age(1) age(2) age(3) age(4)

15 Dim ArrayName(Int Expression) As DataType Declaring and Allocating an Array l An array can be declared and allocated memory in one statement or an array can be declared in one statement and another statement ca be written later to allocate memory for the array, using Redim SYNTAX FORMS

16 Assigning values to individual array elements Dim angle(4) As Single ' creates array Dim m As Integer = 3 angle(0) = 4.93 angle(1) = angle(2) = 0.5 angle(3) = 1.67 angle(m) = angle (3) – 1.2 angle(0) angle(1) angle(2) angle(3)

17 Exmples angle(2) = 9.6 angle(2) = CDlb(inFile.ReadLine()) outFile.WriteLine(angle(2)) y = Math.Sqrt(angle(2)) x = 6.8 * angle(2) + 7.5

18 What values are assigned? Dim temps (4) As Double ' allocates array Dim m As Integer For m = 0 To temps.Length – 1 temps(m) = m * 0.2 Next temps(0) temps(1) temps(2) temps(3) temps(4) ? ? ? ? ?

19 What values are assigned? Dim temps (4) As Double ' allocates array Dim m As Integer For m = 0 To temps.Length – 1 temps(m) = m * 0.2 Next temps(0) temps(1) temps(2) temps(3) temps(4) ? ? ? ? ?

20 Now what values are printed? Const ARRAY_SIZE As Integer = 5 ' named constant Dim temps( ) As Double Redim temps(ARRAY_SIZE) Dim m As Integer For m = temps.Length – 1 To 0 Step –1 Console.WriteLine ("temps(“ & m & ") = " & temps(m)) temps(0) temps(1) temps(2) temps(3) temps(4)

21 Variable subscripts Dim temps(4) As Double Dim m As Integer = What is temps( m + 1 ) ? What is temps( m ) + 1 ? temps(0) temps(1) temps(2) temps(3) temps(4)

22 More about array index Array index can be an integral expression of type Char, Short, Byte, or Integer. l It is programmer’s responsibility to make sure that an array index does not go out of bounds. The index must be within the range 0 through the array’s length minus 1. Using an index value outside this range throws an IndexOutOfBoundsException. Prevent this error by using public instance method length.

23 Aggregate Array Operations numbers( 0) numbers( 1) numbers( 2 ) numbers values( 0) values( 1) values( 2 ) values Dim numbers( ) As Integer = {2, 4, 6} Dim values(3) As Integer values(0) = 2 values(1) = 4 values(2) = 6 If (numbers = values) then …

24 Aggregate Array Operations numbers( 0) numbers( 1) numbers( 2 ) numbers values( 0) values( 1) values( 2 ) values numbers = values If (numbers = values) then …

Examples of Declaring and Processing Arrays l Occupancy Rates Const BUILDING_SIZE as Integer = 350 Dim occupants(BUILDING_SIZE) AS Integer Dim totalOccupants As Inteer totalOccupants = 0 For counter = 0 To occupants.Length – 1 totalOccupants = totalOccupants +occupants(counter) Next counter occupants( 0) occupants( 1) occupants( 2 ).. occupants( 349 ) occupants

26 Sales Figures Dim gourmetBurgers (5) As Double gourmetBurgers( 0) gourmetBurgers( 1) gourmetBurgers( 2 ) gourmetBurgers( 3 ) gourmetBurgers( 4 ) gourmetBurgers

27 Using arrays for counters l Write a program to count the number of each alphabet letter in a text file. letterASCII ‘A’ 65 ‘B’ 66 ‘C’ 67 ‘D’ 68.. ‘Z’ 90 This is my text file. It contains many things! is not 14. Is it? datafile.dat

28 Dim letterCount(25) As Integer counts ‘A’ and ‘a’ counts ‘B’ and ‘b’. counts ‘ Y’ and ‘y’ counts ‘Z’ and ‘z’ letterCount ( 0 ) 2 letterCount ( 1 ) 0.. letterCount ( 24) 1 letterCount ( 25 ) 0

29 Dim letter As Integer While (dataFile.Peek<> -1) letter = (Char(dataFile.Read ( ) ) If ((letter >= "A"c And letter <= "Z"c ) Or _ ((letter >= "a"c And letter <= "z"c ) Then index = (Int (Asc (letter.ToUpper(letter)) – Asc ("A")) letterCount(index) = letterCount (index) + 1 End If End While Counting Frequency of Alphabetic Characters

30 For index = 0 To letterCount.Length – 1 outFile.WriteLine ("The total number of " & _ Char(index + (Int (Asc ("A"))) & _ " " & letterCount (index)) Next Printing Frequency of Alphabetic Characters ' print each alphabet letter and its frequency count

31 Dim groceryItems (10) As String ( 0 ) “cat food” ( 1 ) “rice”.. ( 8 ) “spinach” ( 9 ) “butter” groceryItems 11.5 Array Of Objects (Strings)

32 Dim groceryItems(10) As String ( 0 ) “cat food” ( 1 ) “rice”.. ( 8 ) “spinach” ( 9 ) “butter” groceryItems Expression Class/Type groceryItems Array groceryItems(0) String groceryItem(0).Chars(0) Char

Passing Arrays as Arguments l In VB.NET an array is a reference type. What is passed to a method with an array parameter is the address of where the array object is stored. The name of the array is actually a reference to an object that contains the array elements and the public instance variable Length.

34 Public Function sumSales (ByRef data( ) As Integer) As Integer Dim sum As Double = 0 Dim index As Integer For index = 0 To data.Length – 1 sum = sum + data ( index ) Next index Return sum End Function Passing an Array as Parameter

35 Passing an Array as Parameter Dim gourmetBurgers (5) As Double outFile.WriteLine (sumSales(gourmetBurgers)) gourmetBurgers( 0) gourmetBurgers( 1) gourmetBurgers( 2 ) gourmetBurgers( 3 ) gourmetBurgers( 4 ) gourmetBurgers