Chapter Nine Arrays. Sixth Edition

Similar documents
Chapter Ten String Manipulation and Menus

Chapter 12 Two-Dimensional Arrays

Microsoft Visual Basic 2015: Reloaded

Microsoft Visual Basic 2015: Reloaded

Microsoft Visual Basic 2015: Reloaded

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Chapter 8. Arrays and More Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

Chapter 6: Using Arrays

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Chapter 9 Introduction to Arrays. Fundamentals of Java

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Subject: Computer Science

Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

Microsoft Visual Basic 2005: Reloaded

Data Structures: Cell Arrays and Structures. Chapter 8. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Flag Quiz Application

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Unit 3 Fill Series, Functions, Sorting

Unit 3 Functions Review, Fill Series, Sorting, Merge & Center

Creating Two-dimensional Arrays

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Sorting Pearson Education, Inc. All rights reserved.

Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts. Microsoft Excel 2013 Enhanced

AP Computer Science Lists The Array type

Chapter 7 : Arrays (pp )

Array Basics: Outline

Arrays and Strings. Arash Rafiey. September 12, 2017

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

INDIAN SCHOOL MUSCAT COMPUTER SCIENCE(083) CLASS XI

Reg. No. : Question Paper Code : B.E./B.Tech. DEGREE EXAMINATION, JANUARY First Semester GE 6151 COMPUTER PROGRAMMING

Chapter Two Creating a User Interface

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

eschoolplus+ Cognos Query Studio Training Guide Version 2.4

TOPICS TO COVER:-- Array declaration and use.

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

New Perspectives on Microsoft Excel Module 5: Working with Excel Tables, PivotTables, and PivotCharts

Procedural Programming

Review of Important Topics in CS1600. Functions Arrays C-strings

Any Integer Can Be Written as a Fraction

Pace University. Fundamental Concepts of CS121 1

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Grouping and Sorting

Searching, Sorting. Arizona State University 1

C++ PROGRAMMING SKILLS Part 4: Arrays

JME Language Reference Manual

Programming Logic and Design Sixth Edition

Advanced Computer Programming

Lecture Outline. Rainfall Solution Attempt 1. Motivation for arrays. Introducing the Array. Motivation

Name: Class: Date: Access Module 2

Introduction to Computer Science Midterm 3 Fall, Points

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts

User Guide of UNCTAD Global Database on Creative Economy

Microsoft Excel 2010

Arrays. Storing Multiple Values

Instructor: Eng.Omar Al-Nahal

Unit 3 Decision making, Looping and Arrays

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Programming for Electrical and Computer Engineers. Pointers and Arrays

Chapter 7: Arrays and the ArrayList Class

M i c r o s o f t E x c e l A d v a n c e d. Microsoft Excel 2010 Advanced

UNIT 2 UNDERSTANDING ARRAYS AND LISTS IN C#

Multiple-Subscripted Arrays

Lab 09: Advanced SQL

SQL - Tables. SQL - Create a SQL Table. SQL Create Table Query:

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Microsoft Visual Basic 2005: Reloaded

Arrays. Theoretical Part. Contents. Keywords. Programming with Java module 3

Principles of Programming. Chapter 6: Arrays

This page intentionally left blank

Transform data - Compute Variables

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Programming Language 2 (PL2)

BITG 1113: Array (Part 1) LECTURE 8

Loops / Repetition Statements

Lecture 3 Tao Wang 1

Burrows & Langford Chapter 10 page 1 Learning Programming Using VISUAL BASIC.NET

One-dimensional Array

Data Structures. Chapter 04. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Query Studio Training Guide Cognos 8 February 2010 DRAFT. Arkansas Public School Computer Network 101 East Capitol, Suite 101 Little Rock, AR 72201

Formulas and Functions

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Bubble sort is so named because the numbers are said to bubble into their correct positions! Bubble Sort

Introduction To Programming. Chapter 5: Arrays and More

Arrays (Deitel chapter 7)

Microsoft Visual Basic 2012: Reloaded

Transcription:

Microsoft Visual Basic 2015: Reloaded Sixth Edition Chapter Nine Arrays

Objectives After studying this chapter, you should be able to: Declare and initialize one-dimensional and twodimensional arrays Store and access data in an array Determine the number of array elements and the highest subscript Traverse an array Code a loop using the For Each Next statement Compute the total and average of an array s contents 2

Objectives (cont'd.) Find the highest value in an array Associate a list box with a one-dimensional array Use a one-dimensional array as an accumulator or a counter Sort a one-dimensional array Search an array 3

Arrays Simple variable (or scalar variable): a variable that is unrelated to any other variable in memory Array A group of variables with the same name and data type that are related in some way Used to temporarily store related data in memory Increases the efficiency of a program Commonly used arrays: One-dimensional Two-dimensional 4

One-Dimensional Arrays One-dimensional array Can be viewed as a column of variables Each variable in the array has the same name and data type Subscript A unique number that identifies each variable in a onedimensional array Starts at 0 for first element in the array Use array name and subscript to refer to each individual variable in the array 5

One-Dimensional Arrays (cont d.) Element An individual variable in the array When an array is declared: Must specify the data type, name, and highest subscript to be used First element has a subscript of 0 The array will contain one element more than the highest subscript because subscripts start at 0 6

One-Dimensional Arrays (cont'd.) Figure 9-1: Illustration of the one-dimensional scientists array 7

One-Dimensional Arrays (cont'd.) Declaring a One-Dimensional Array Figure 9-2: How to declare a one-dimensional array 8

One-Dimensional Arrays (cont'd.) Populating the array Assigning initial values to an array List the values in a comma-separated list enclosed in curly braces ({}) After declaration, can use an assignment statement to store a value in an array element Figure 9-3: Illustration of the tvseries and rates arrays 9

One-Dimensional Arrays (cont'd.) Storing Data in a One-Dimensional Array Figure 9-4: How to store data in a one-dimensional array 10

One-Dimensional Arrays (cont'd.) Determining the Number of Elements in an Array Length property Indicates number of elements Figure 9-5: How to determine the number of elements in an array 11

One-Dimensional Arrays (cont'd.) Determining the Highest Subscript in a One- Dimensional Array GetUpperBound method Returns the highest subscript Figure 9-6: How to determine the highest subscript in a one-dimensional array 12

One-Dimensional Arrays (cont'd.) Traversing a One-Dimensional Array Look at each array element, one by one, from beginning to end of the array Use a loop to traverse an array 13 Figure 9-7: How to traverse a one-dimensional array

The For Each Next Statement For Each Next statement Used to code a loop that processes each element Creates a variable used to represent each Data type of the element must match the data type of the group 14 Figure 9-8 How to use the For Each Next statement

The For Each Next Statement (cont'd.) Calculating the Total and Average Values Figure 9-9: Problem specification, code, and sample run for the Popcorn Factory application (continues) 15

The For Each Next Statement (cont'd.) Figure 9-9: Problem specification, code, and sample run for the Popcorn Factory application 16

Finding the Highest Value Figure 9-10: Problem specification, code, and sample run another version of the Popcorn Factory application 17

Arrays and Collections Programmers associate the values in an array with the items in a list box Items in a list box belong to the Items collection and collections and arrays have several things in common Each is a group of individual objects treated as one unit Each individual object in the group is identified by a unique number (index or subscript) 18

Arrays and Collections (cont'd.) Figure 9-11: Problem specification, code, and sample run for the Presidents Vice Presidents application 19

Accumulator and Counter Arrays One-dimensional arrays are often used to either accumulate or count related values These arrays are commonly referred to as: accumulator arrays counter arrays 20

Accumulator and Counter Arrays (cont'd.) Figure 9-12 Problem specification, code, and sample run for the Allen School application (continues) 21

Accumulator and Counter Arrays(cont'd.) Figure 9-12 Problem specification, code, and sample run for the Allen School application 22

Sorting a One-Dimensional Array Sorting: arranging data in a specific order Ascending: first element is smallest, last element is largest Descending: first element is largest, last element is smallest Array.Sort method: used to sort elements in a one-dimensional array in ascending order Array.Reverse method: used after Array.Sort method to change to descending order 23

Sorting a One-Dimensional Array (cont'd.) 24 Figure 9-13: How to use the Array.Sort and Array.Reverse methods

Two-Dimensional Arrays Two-dimensional array Resembles a table with rows and columns Each element is identified by a unique combination of two subscripts: (row, column) Subscripts are zero-relative Refer to an element using the name followed by the (row, column) pair in parentheses Figure 9-14: Names of some of the elements in the songs array 25

Two-Dimensional Arrays (cont'd.) Two-dimensional array are declared with highest row subscript and highest column subscript (zero-relative) Number of rows = highest row subscript + 1 Number of columns = highest column subscript + 1 Can specify initial values for array elements If no initial values are declared, array elements are automatically initialized 26

Two-Dimensional Arrays (cont'd.) Figure 9-15: How to declare a twodimensional array 27

Two-Dimensional Arrays (cont'd.) Figure 9-16: How to store data in a two-dimensional array 28

Two-Dimensional Arrays (cont'd.) Figure 9-17: How to determine the highest subscripts in a two-dimensional array 29

Two-Dimensional Arrays (cont'd.) Traversing a Two-Dimensional Array Use two loops Outer loop: tracks the row subscript Nested loop: tracks the column subscript Can also use a For Each Next loop 30

Two-Dimensional Arrays (cont'd.) Figure 9-18: How to traverse a twodimensional array 31

Two-Dimensional Arrays (cont'd.) Totaling the Values Stored in a Two-Dimensional Array Figure 9-19: Problem specification, code, and sample run for the Tyler Motors application (continues) 32

Two-Dimensional Arrays (cont'd.) Totaling the Values Stored in a Two-Dimensional Array Figure 9-19: Problem specification, code, and sample run for the Tyler Motors application (continues) 33

Two-Dimensional Arrays (cont'd.) 34 Figure 9-19: Problem specification, code, and sample run for the Tyler Motors application

Two-Dimensional Arrays (cont'd.) Searching a Two-Dimensional Array Figure 9-20: Problem specification, code, and sample run for the O Reilly Studios application (continues) 35

Two-Dimensional Arrays (cont'd.) 36 Figure 9-20: Problem specification, code, and sample run for the O Reilly Studios application

Programming Tutorial 1 Figure 9-21: TOE chart and MainForm for the Lottery Game application Figure 9-22: Pseudocode for the getbutton_click procedure Figure 9-24: Sample run of the Lottery Game application 37

Programming Tutorial 2 Figure 9-26: Membership fees, TOE chart, and MainForm for the Jenkins Gym application Figure 9-27: Sample run of the Jenkins Gym application 38

Programming Example Figure 9-29: TOE Chart 39 Figure 9-30: MainForm and tab order

Summary Programmers use arrays to temporarily store related data in the internal memory of the computer All of the elements in an array have the same name and data type When declaring a one-dimensional array, you provide either the highest subscript or the initial values Each element in a one-dimensional array is identified by a unique subscript that appears in parentheses after the array s name 40

Summary (cont'd.) You refer to an element in a one-dimensional array using the array s name followed by the element s subscript, which is enclosed in parentheses Examples of statements that you can use to change the data stored in an array include assignment statements and statements that contain the TryParse method A one-dimensional array s Length property contains an integer that represents the number of elements in the array 41

Summary (cont'd.) A one-dimensional array s GetUpperBound method returns an integer that represents the highest subscript in the array You use a loop to traverse a one-dimensional array You can associate the items in a list box with the elements in an array You can use the elements in an array as accumulators or counters 42

Summary (cont'd.) The Array.Sort method sorts the elements in a one-dimensional array in ascending order The Array.Reverse method reverses the order of the elements in a one-dimensional array A two-dimensional array resembles a table in that the elements are in rows and columns When declaring a two-dimensional array, you provide either the highest row and column subscripts or the initial values 43

Summary (cont'd.) The number of rows in a two-dimensional array is one number more than its highest row subscript You can determine the number of elements in a twodimensional array by multiplying the number of its rows by the number of its columns. You can also use the array s Length property Each element in a two-dimensional array is identified by row and column subscripts 44

Summary You can use a two-dimensional array s GetUpperBound method to determine the highest row subscript and column subscript in the array You can traverse a two-dimensional array using either two loops (coded with the For Next or Do Loop statements) or one loop (coded with the For Each Next statement) 45