Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
PAC Introduction to Methods Professor: Evan Korth New York University.
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 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.
Introduction to Computers and Programming Introduction to Methods in Java.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 17: Arrays (cont) Professor: Evan Korth New York University.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
 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.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
 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.
Computer Science I Arrays Professor: Evan Korth New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference Parameters.
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays. Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
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.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
 Pearson Education, Inc. All rights reserved Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Review for Nested loops & Math class methods & User defined methods.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 Pearson Education, Inc. All rights reserved Passing Arrays to Methods To pass array argument to a method – Specify array name without.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Introduction to Methods
7 Arrays.
Object Oriented Programming in java
Presentation transcript:

Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University

Road Map More array examples. Passing arrays to methods Reading: Chapter 5, Section 5.4 Reading: Chapter 6, Section 6.4 Reading: Chapter 7, Section 6.4

review What is an array? What is a subscript? What is another term for a subscript? Given an array of size n, what is the valid range of subscripts? How can you obtain the size of an array in Java? What happens if you try to access an element outside the valid range of subscripts? What are the default values given to array elements in Java?

 2003 Prentice Hall, Inc. All rights reserved. 7.4 Examples Using Arrays (Cont.) Histogram Using the elements of an array as counters –Use a series of counter variables to summarize data

 2003 Prentice Hall, Inc. All rights reserved. Outline RollDie.java Line 9 Declare frequency as array of 7 int s Lines Generate 6000 random integers in range 1-6 Line 13 Increment frequency values at index associated with random number 1 // Fig. 7.7: RollDie.java 2 // Roll a six-sided die 6000 times. 3 import javax.swing.*; 4 5 public class RollDie { 6 7 public static void main( String args[] ) 8 { 9 int frequency[] = new int[ 7 ]; // roll die 6000 times; use die value as frequency index 12 for ( int roll = 1; roll <= 6000; roll++ ) 13 ++frequency[ 1 + ( int ) ( Math.random() * 6 ) ]; String output = "Face\tFrequency"; // append frequencies to String output 18 for ( int face = 1; face < frequency.length; face++ ) 19 output += "\n" + face + "\t" + frequency[ face ]; System.out.println(”Rolling a Die 6,00 times” ); System.out.println ( output ); System.exit( 0 ); } // end main } // end class RollDie Declare frequency as array of 7 int s Generate 6000 random integers in range 1-6 Increment frequency values at index associated with random number

 2003 Prentice Hall, Inc. All rights reserved. 7.4 Examples Using Arrays (Cont.) Using arrays to analyze survey results –40 students rate the quality of food 1-10 Rating scale: 1 mean awful, 10 means excellent –Place 40 responses in array of integers –Summarize results

 2003 Prentice Hall, Inc. All rights reserved. Outline StudentPoll.jav a Lines 9-11 Declare responses as array to store 40 responses Line 12 Declare frequency as array of 11 int and ignore the first element Lines For each response, increment frequency values at index associated with that response 1 // Fig. 7.8: StudentPoll.java 2 // Student poll program. 3 import javax.swing.*; 4 5 public class StudentPoll { 6 7 public static void main( String args[] ) 8 { 9 int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 11 4, 8, 6, 8, 10 }; 12 int frequency[] = new int[ 11 ]; // for each answer, select responses element and use that value 15 // as frequency index to determine element to increment 16 for ( int answer = 0; answer < responses.length; answer++ ) 17 ++frequency[ responses[ answer ] ]; String output = "Rating\tFrequency\n"; // append frequencies to String output 22 for ( int rating = 1; rating < frequency.length; rating++ ) 23 output += rating + "\t" + frequency[ rating ] + "\n"; 24 JOpSystem.out.println( output ); System.exit( 0 ); } // end main } // end class StudentPoll Declare responses as array to store 40 responses Declare frequency as array of 11 int and ignore the first element For each response, increment frequency values at index associated with that response

 2003 Prentice Hall, Inc. All rights reserved. Outline StudentPoll.jav a

 2003 Prentice Hall, Inc. All rights reserved. 7.4 Examples Using Arrays (Cont.) Some additional points –When looping through an array Index should never go below 0 Index should be less than total number of array elements –When invalid array reference occurs Java generates ArrayIndexOutOfBoundsException –Chapter 15 discusses exception handling

 2003 Prentice Hall, Inc. All rights reserved. 7.6 Passing Arrays to Methods To pass array argument to a method –Specify array name without brackets Array hourlyTemperatures is declared as int hourlyTemperatures [] = new int[24]; The method call modifyArray( hourlyTemperatures ); Passes array hourlyTemperatures to method modifyArray

Passing arrays to methods (cont) In the method header, use similar syntax as that for array declaration: public static void modifyArray(int array [] ) or public static void modifyArray(int [] array )

 2003 Prentice Hall, Inc. All rights reserved. 7.5 References and Reference Parameters Two ways to pass arguments to methods –Pass-by-value Copy of argument’s value is passed to called method In Java, every primitive is pass-by-value –Pass-by-reference Caller gives called method direct access to caller’s data Called method can manipulate this data Improved performance over pass-by-value In Java, every object is simulated pass-by-reference –In Java, arrays are objects Therefore, arrays are passed to methods by reference –Technically, we are using pass by value but the “value” we are passing is a reference to the array.