11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Arrays.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A First Book of ANSI C Fourth Edition
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Chapter 6: Using Arrays.
Arrays.
Presentation transcript:

11 Chapter 8 ARRAYS Continued

22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The scores each student in a class earned on a test –The batting averages of the players on a baseball team –The monthly sales produced by a salesperson

33 MULTI-DIMENSIONAL ARRAYS Sometimes we need to be able to store multiple sets of data. For example: –The scores the students in a class earned on three exams –The batting averages of the players on a baseball team, for every team in the league –The monthly sales generated by each salesperson in a company –A table of data –A matrix representing the pixels of an image Multiple sets of data can be stored in a multi-dimensional array.

44 MULTI-DIMENSIONAL ARRAYS When declaring a reference variable that will hold the address of an n-dimensional array you must have n brackets after the data type of the individual elements. When using the new operator to create an n-dimensional array there must be a bracketed size declarator for each of the n dimensions.

55 MULTI-DIMENSIONAL ARRAYS Example: The two brackets after the key word float specify that the variable battAvgs is a reference to a two-dimensional array of floats. The new operator is used to create an array of floats with NUM_TEAMS elements in the first dimension and NUM_PLAYERS elements in the second dimension. In total there are NUM_TEAMS * NUM_PLAYERS (1000) elements in the array. final int NUM_TEAMS = 40; final int NUM_PLAYERS = 25; float[ ][ ] battAvgs = new float[NUM_TEAMS][NUM_PLAYERS]; Each size declarator is in its own set of brackets.

66 MULTI-DIMENSIONAL ARRAYS To access a particular element (variable in the array) we must specify its subscript/offset in each dimension. Given the declarations below, the valid subscripts in the first dimension are 0 through 39 and the valid subscripts in the second dimension are 0 through 24. final int NUM_TEAMS = 40; final int NUM_PLAYERS = 25; float[ ][ ] battAvgs = new float[NUM_TEAMS][NUM_PLAYERS]; battAvgs[0][0] is the element at subscript/offset zero in both dimensions. It corresponds to the first team’s first player.

77 MULTI-DIMENSIONAL ARRAYS Problem: Given the following declarations, how many dimensions does the array referenced by temperatures have? _________________ How many elements does the array have? _________________ What are the valid subscript ranges in each dimension? _________________ final int NUM_YEARS = 10; final int NUM_DAYS = 365; final int NUM_HOURS = 24; double[ ][ ][ ]temperatures = new double[NUM_YEARS][NUM_DAYS][NUM_HOURS];

88 MULTI-DIMENSIONAL ARRAYS Each element of an array can be used just like an individual variable of the same data type.

99 MULTI-DIMENSIONAL ARRAYS Problem: Write a statement to assign the value 91.5 to the element that corresponds to the sixth year, the 208th day, and the 15th hour, of the array referenced by temperatures that is declared below. final int NUM_YEARS = 10; final int NUM_DAYS = 365; final int NUM_HOURS = 24; double[ ][ ][ ]temperatures = new double[NUM_YEARS][NUM_DAYS][NUM_HOURS];

10 MULTI-DIMENSIONAL ARRAYS Problem: Write a statement to display the temperature stored in the array referenced by temperatures for the last hour of the 10th day of the first year. final int NUM_YEARS = 10; final int NUM_DAYS = 365; final int NUM_HOURS = 24; double[ ][ ][ ]temperatures = new double[NUM_YEARS][NUM_DAYS][NUM_HOURS];

11 MULTI-DIMENSIONAL ARRAYS The length Attributes of a Two-dimensional Array A one-dimensional array has a length attribute that stores the number of elements in the array. A two-dimensional array has multiple length attributes.

12 MULTI-DIMENSIONAL ARRAYS The length attributes make sense when we visualize a two- dimensional array as an array of arrays. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; This is the first dimension. There are three elements in the candidates dimension. This is the second dimension. There are four elements in the precincts dimension.

13 MULTI-DIMENSIONAL ARRAYS The length Attributes of a Two-dimensional Array Given the declaration of the array referenced by votes from the segment and illustration below, votes.length is the number of elements in the first dimension of the array, which in this case is three. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

14 MULTI-DIMENSIONAL ARRAYS The length Attributes of a Two-dimensional Array The attribute votes[0].length is the number of elements in the array referenced by votes[0], which is this case is four. The attribute votes[1].length is the number of elements in the array referenced by votes[1], which is also four in this example, etc. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

15 MULTI-DIMENSIONAL ARRAYS When you create a numeric array locally it is automatically initialized to contain all zeroes. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

16 MULTI-DIMENSIONAL ARRAYS To access a particular element/integer variable in the array referenced by votes, you must give the subscript in both dimensions. For example, votes[1][2] is the element at offset 1 in the first dimension and offset 2 in the second dimension. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; votes[0][0] 0 votes[0][1] 0 votes[0][2] 0 votes[0][3] 0 votes[1][0] 0 votes[1][1] 0 votes[1][2] 0 votes[1][3] 0 votes[2][0] 0 votes[2][1] 0 votes[2][2] 0 votes[2][3] 0 votes[0] address votes[1] address votes[2] address votes

17 MULTI-DIMENSIONAL ARRAYS Passing Multi-Dimensional Arrays as Arguments to Methods A multi-dimensional array may be passed to a method as an argument. When an array is passed as an argument, we use the array name without brackets in the method call. Remember arrays are passed by reference. The parameter that receives an array argument must include one set of square brackets for each dimension.

18 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Programs that manipulate the contents of multi-dimensional arrays usually use nested loops. One loop is used to cycle through the subscripts in each dimension.

19 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays The inner loop of a nested loop iterates more quickly than the outer loop. We need to consider which dimension we need/want to move through more quickly. These subscripts should be cycled through in the inner loop.

20 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Suppose we wanted to store the votes three candidates received in four precincts in an election. We might declare a two-dimensional array referenced by votes as shown below: final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; The valid subscripts in the first dimension of this array are The valid subscripts in the second dimension of this array are

21 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays We could use nested loops to cycle through all the subscripts in each dimension to access each element of the array. final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; Do we want to move through the array like following: votes[0][0], votes[0][1], votes[0][2], votes[0][3], votes[1][0], votes[1][1], votes[1][2],... If this is the case we need to cycle through the subscripts in the PRECINCTS dimension in the inner loop. This is the way we would want to move if we wanted to find the total votes received by a candidate across all precincts.

22 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays final int CANDIDATES = 3; final int PRECINCTS = 4; int[ ][ ] votes = new int[CANDIDATES][PRECINCTS]; Do we want to move through the array like following: votes[0][0], votes[1][0], votes[2][0], votes[0][1], votes[1][1], votes[2][1],... If this is the case we need to cycle through the subscripts in the CANDIDATES dimension in the inner loop. This is the way we would want to move if we wanted to find the total votes cast in each precinct.

23 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays ***See the program ProcessVotes.java on webct

24 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Problem: Define an array that can be used to store 5 test scores for 100 students. Assume that the test scores are real numbers.

25 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Problem: Given your declaration from the previous problem, if we wanted to get every student’s grade on a test and put it in the array before moving on to the next test, what is the correct loop header for the inner loop?

26 MULTI-DIMENSIONAL ARRAYS Processing Data In Multi-Dimensional Arrays Problem: Given your declaration from slide 23, if we wanted to calculate each student’s average test score, what is the correct loop header for the inner loop?

27 THE ArrayList class We will not cover the ArrayList class in this course. You will not be asked any questions about this class on the exam.