ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.

Slides:



Advertisements
Similar presentations
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Advertisements

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.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Arrays Chapter 6 Chapter 6.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programming with Collections Collections in Java Using Arrays Week 9.
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 15: Arrays Professor: Evan Korth New York University.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
© 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.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
1 Week 9 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays 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.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
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 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
BIT115: Introduction to Programming
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
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.
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays

ARRAYS 2 Handling seven temperature readings class TemperatureReadings { BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in)); public static void main(String [] args) { double temperature1, temperature2, temperature3,temperature4, temperature5,temperature6,temperature7; // more code will go here }

ARRAYS 3 Code to enter all seven temperatures Getting one temperature is easy: System.out.println("max temperature for day 1 ?"); temperature1 = Double.parseDouble (keyboard.readLine( ));

ARRAYS 4 Try using a loop? for (int i=1; i<=7; i++) { System.out.println("max temperature for day " + i); temperature1 = Double.parseDouble (keyboard.readLine( )); }

ARRAYS 5 Arrays l An array is an ordered list of values An array of size N is indexed from zero to N-1 scores The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9

ARRAYS 6 Arrays l A particular value in an array is referenced using the array name followed by the index in brackets l For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) l That expression represents a place to store a single integer and can be used wherever an integer variable can be used

ARRAYS 7 CREATING AN ARRAY (Arrays are used to hold a collection of items all of the same type. ) Array creation is a two stage process 1.Declare an array. ArrayType [ ] arrayName; 2.Create memory to store the array. arrayName = new ArrayType [sizeOfArray]; Examples: 80-element character array: char[] symbol = new char[80]; 100-element array of doubles: double[] reading = new double[100];

ARRAYS 8 CREATING AN ARRAY OF TEMPERATURES Since each temperature is of type double an array of temperature readings is declared as follows: double [ ] temperature; Since there will be seven temperature readings, memory is reserved for this array ass follows: temperature = new double [7];

ARRAYS 9 Java Instructions temperature = new double[7]; Computer Memory temperature item of type 'double' The effect on computer memory of declaring an array of values of type 'double'

ARRAYS 10 NAMING OF ARRAY ELEMENTS l Each element in an array shares the same name as the array. l The individual elements are then uniquely identified by an additional index value. l Array indices start from 0 (and not from 1). l This index value is always enclosed in square brackets : first temperature : temperature[0] second temperature: temperature[1]

ARRAYS 11 Some array terminology temperature[n + 2] temperature[n + 2] = 32; Array name Index - also called a subscript - must be an int, - or an expression that evaluates to an int Indexed variable - also called an element or subscripted variable Note that "element" may refer to either a single indexed variable in the array or the value of a single indexed variable. Value of the indexed variable - also called an element of the array

ARRAYS 12 ACCESSING ARRAY ELEMENTS temperature[0] = Double.parseDouble (keyboard.readLine( )); System.out.println(temperature[5]); System.out.println ("temperature for day 1 is ” + temperature[0]); temperature[4] = temperature[4] * 2; if (temperature[2] >= 18) { System.out.println("it was hot today"); }

ARRAYS 13 USING A LOOP TO ENTER VALUES INTO AN ARRAY for(int i = 0; i < 7; i++) { System.out.println("max temperature for day ” +(i+1)); temperature[i] = Double.parseDouble (keyboard.readLine( )); }

ARRAYS 14 THE 'length' ATTRIBUTE for (int i = 0; i < temperature.length, i++) { // code for loop goes here } Array length is specified by the number in brackets when it is declared. Determines amount of memory allocated for array elements (values). Determines the maximum number of elements the array can hold. Storage is allocated whether or not the elements are assigned values.

ARRAYS 15 EXAMPLE: Array stores 7 temperatures and displays average temperature & whether each temperature is below, above or same as the average temperature. public class ArrayOfTemperatures { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) { double[] temperature = new double[7]; int index; double sum = 0.0, average; System.out.println(“Enter 7 temperature readings”); for (index = 0; index < 7; index++) { temperature[index] = Double.parseDouble(keyboard.readLine()); sum = sum + temperature[index]; }

ARRAYS 16 average = sum / 7; System.out.println(“The average temperature is “ + average); System.out.println(“The temperatures are : “); for (index = 0; index < 7; index++); { if (temperature[index] < average) System.out.println(temperature[index] + “ below average”); else if (temperature[index] > average) System.out.println(temperature[index] + “ above average”); else System.out.println(temperature[index] + “ is the average”); }

ARRAYS 17 INITIALIZING AN ARRAY (declaration) An initializer list can be used to instantiate and fill an array in one step The values are delimited by braces and separated by commas Examples: double [] temperature = {9, 11.5, 11, 8.5, 7, 9, 8.5}; char[] letterGrades = {'A', 'B', 'C', 'D', ’F'};

ARRAYS 18 Subscript range l Array subscripts use zero-numbering »the first element has subscript 0 »the second element has subscript 1 »etc. - the n th element has subscript n-1 »the last element has subscript length-1 l For example: int[] scores = {97, 86, 92, 71};

ARRAYS 19 Subscript out of range error Using a subscript larger than length-1 causes a run time (not a compiler) error »an ArrayOutOfBoundsException is thrown –you do not need to catch it or declare it in a throws-clause –you need to fix the problem and recompile your code l Other programming languages, e.g. C and C++, do not even cause a run time error! »one of the most dangerous characteristics of these languages is that they allow out of bounds array indexes.

ARRAYS 20 Good programming practice l Using singular rather than plural names for arrays improves readability »although the array contains many elements the most common use of the name will be with a subscript, which references a single value l Do not count on default initial values for array elements »explicitly initialize elements in the declaration or in a loop

ARRAYS 21 Array size l FIXED-SIZE ARRAY DECLARATION int[] number = new int[100];  Problems?? oDon’t always know array size at compile time.

ARRAYS 22 Some operations on arrays: l Initialize l Input data l Output stored data l Find largest/smallest/sum/average of elements

ARRAYS 23 How To Specify Array Size During Program Execution int arraySize; // Declare array System.out.println("Enter size of the array: "); arraySize = Integer.parseInt(keyboard.readLine()); int[] list = new int[arraySize]; // Create array

ARRAYS 24 Code to Initialize Array to Specific Value (10.00) for(index = 0; index < sale.length; index++) sale[index] = 10.00;

ARRAYS 25 Code to Read Data into Array for(index = 0; index < sale.length; index++) sale[index] = Integer.parseInt(keyboard.readLine());

ARRAYS 26 Code to Print Array for(index = 0; index < sale.length; index++) System.out.print(sale[index] + " ");

ARRAYS 27 Code to Find Sum & Average of Array sum = 0; for(index = 0; index < sale.length; index++) sum = sum + sale[index]; if(sale.length != 0) average = sum / sale.length; else average = 0.0;

ARRAYS 28 Determining Largest Element in Array maxIndex = 0; for(index = 1; index < sale.length; index++) if(sale[maxIndex] < sale[index]) maxIndex = index; largestSale = sale[maxIndex];

ARRAYS 29 Determining Largest Element in Array