Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
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.
Introduction to C Programming
Arrays.
Cosc 2150 Arrays in assembly code. Variables and addresses Uncompiled ld [a], %r1 addcc %r1, 2, %r3 ARC has three addressing modes —immediate, direct,
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
An Introduction to Programming with C++ Fifth Edition
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Multiple-Subscripted Array
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Introduction to Programming with C++ Fourth Edition
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.
Chapter 9 Introduction to Arrays
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
A First Book of ANSI C Fourth Edition
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
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.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Working with Arrays in MATLAB
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.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
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.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
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.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
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;
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
Arrays Chapter 7.
Arrays.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 7 Part 2 Edited by JJ Shepherd
Dr Tripty Singh Arrays.
Multi-Dimensional Arrays
C++ Array 1.
Week 7 - Monday CS 121.
Presentation transcript:

Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass as a parameter and initalize

Some common two dimensional arrays or tables: Chessboard, train tables, spreadsheets int [ ] [ ] sales = new int [4] [7]; – sales is a new two dimensional array. Sales has 4 rows and 7 columns (Java is row major language, rows always first) – sales may be used by four stores (rows) – seven days (columns) a week. ( rows numbered 0..2, columns 0..6) All Java arrays start with zero.

Diagram of the ‘store’ array page 262 – see page 262 for the completely 2filled in array

int [ ] [ ] sales = new int [4] [7]; double temps = new double [10] [24]; Again rows first 4 for sales 10 for temps columns after 7 for sales, 24 for temps. – sales [2] [4] = Integer.parseInt (textField.getText( )); – chessBoard [3] [4] = textField.getText ( ); –Above: how individual elements may be loaded

sales [3] [2] = 99; chessBoard [2] [7] = “Knight” ; // place Knight on a square. –A poor way to sum a two dimensional array – sum = sales[0] [0] + sales [0] [1] + … –// plus all the rest of the elements of the two dimensional array sales Better way: use loops!

Using loops to sum every element of sales two dimensional array int [ ] [ ] sales = new int [4] [7]; int sum; sum = 0; for (int shop = 0; shop < = 3; shop++) { – for ( int dayNumber = 0; dayNumber < = 6; dayNumber++) { sum = sum + sales[shop] [dayNumber] ; } –} // shorter and more powerful

The size of an array double [ ] [ ] info = new double [20] [40]; Int maxsize = info.length; // remember.length from one dimensional array chapter

Passing arrays as parameter one or multi dimensional, all passed the same way private int sum ( int [ ] [ ] array ) { // text of sum } call to method: – int [ ] [ ] sales = new [24] [12] ; int total; – total = sum( sales); the array ‘sales’ is passed to the method sum. The array name ‘sales’ hold the address of the zeroth element, the beginning of the array.

constants private int [ ] [ ] sales = new [7] [7]; Both row and columns are 7 What happens if an additional shop is added to the program? final int day = 7; Final shops = 7; –Easily changed…

Using ‘final’ int final shop = 8; int final days = 7; int [ ] [ ] sales = new int [shops] [days] ; If shops or days rewritten the rest of the code in the program that access those variables and will work with the additional shop for example.

Initializing a two dimensional array int table = new int [10] [10]; sets up an array named table in memory, zeros out every element Arrays can be initialized explicitly – for ( int row = 0; row <.9; row++) { for (int column = 0; column <=9; column++) { table [row] [column] = 99 ; }}

More explicit loading of a two dimensional array int [ ] [ ] array = { { 1, 0,1 }, { 0, 1, 0}}; // loads ‘array’ with in the zeroth row And in the 1 st row

Program ‘Rainfall’ pages 267 screenshot A two dimensional array of rainfall over seven days at three locations. User may change values by entering location [index] day[index] and a new value of amount of rainfall as the data in the element The array is declared and initaliaed – private int [ ] [ ] rainData = {{ 10, 7, 3 …see page 267…}}

Methods used to display, change values and calculate the total rainfall The user must limit row (location) from 0..2 Must limit columns (days) from 0..6 Any integer value for the actual rainfall