CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
 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.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
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.
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.
Loops – While, Do, For Repetition Statements Introduction to 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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 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.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Pointers *, &, array similarities, functions, sizeof.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
BIT115: Introduction to Programming
COMP Loop Statements Yi Hong May 21, 2015.
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];
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Introduction to Arrays
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
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.
Arrays in Java What, why and how Copyright Curt Hill.
Arrays.
Arrays in Java.
Introduction to Arrays
Presentation transcript:

CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.

CS100Lecture 102 Today’s Topics Review Arrays –Allocation/Deallocation –Subscripting –Use of arrays to store large collections of data –Several examples

CS100Lecture 103 Review of iteration For loops –for(j = 0; j < NUM; j++) System.out.println(j); –What do you think for(;;) does, for example? Invariants break and continue statements

CS100Lecture 104 Hypothetical Problem Input: zero or more grades from 0 to 100 preceded by the number of grades Task: read grades and compile information about them –print them in reverse order –print them in increasing order –print a histogram So, need to read in all the values before processing, need as many variables as grades... How to do this using the Java we know so far?

CS100Lecture 105 Use arrays instead! An array is an ordered list of values. Each value is stored at a position in the array The number referring to the position is called an index In Java, array indices begin at g

CS100Lecture 106 Deconstruction of this array g[0] is 90 g[1] is 85 g[2] is 40 g[3] is 89 g[4] is 12 g.length is the number of elements in the array, g.length is 5 In “g[4]”, 4 is the index or subscript

CS100Lecture 107 Another example h.length is the number of array elements in array h -- here, h.length is h

CS100Lecture 108 Some notation segment number of values in it empty when g[h..i-1] i-h h = i g[i..j] j+1-ij = i-1 g[j+1..k-1] k-(j+1) j = k h i j k g

CS100Lecture 109 Declaring an array variable int[] g; g float[] averages; averages Employee[] employees; employees Declaring a variable does not “allocate” or create the array of elements; it only declares a variable that can contain a reference to an array of elements. An array is much like a class in that an array variable contains a reference to an array. null

CS100Lecture 1010 How to get the space? g = new int[5]; g Employee[] e = new Employee[206]; e null null null null null... null null …

CS100Lecture 1011 More on declaration and accessing What happens in the statement e[3] = new Employee(“Millett”, 1999); Given allocated array e, we can reference e[0], e[1] …., but we can also use expressions as array indices: e[2*b], e[i], etc.

CS100Lecture 1012 Using array elements Suppose g is an array of integers Then, g[i] where i is in range can be used just as an integer variable –g[i] = 3; a = g[i]*2; c.setX(g[i]); –System.out.println(“value is ” + g[i]); Note that arrays are objects -- therefore have the same ‘call by value’ conventions Java does bounds checking -- throws exception if out of bounds

CS100Lecture 1013 Example program with arrays // Read in a list of integer grades, preceded by the // number of grades, and print in them in reverse order int n= Integer.parseInt(stdin.readLine()); // number of grades int[ ] g= new int [n];// g[0..n-1] are the grades // Read in the grades int i= 0; while (i != g.length) { g[i]= Integer.parseInt(stdin.readLine()); i= i+1; } // Print grades in reverse order int k= n; while (k > 0) { System.out.println(g[k - 1]); k = k-1; } }

CS100Lecture 1014 Rewrite previous with for loops int n= Integer.parseInt(stdin.readLine()); // number of grades int[ ] g= new int [n];// g[0..n-1] are the grades // Read in the grades for(int i = 0; i < g.length; i++) { g[i]= Integer.parseInt(stdin.readLine());} // Print grades in reverse order for (int k = n; k > 0; k--) { System.out.println(g[k - 1]); }

CS100Lecture 1015 Histogram example -- explanation Program scheme to print “histogram” of grades // Read in a list of grades in the range , preceded // by the number of grades, and // print out how many times each grade appears int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will be the no. of times grade i appears // Initialize frequencies f[0..100] to 0. // Read in the grades and make up array f. // Print the grades and their frequencies (print only the grades in // that appeared at least once in the input) }

CS100Lecture 1016 // Read in a list of grades in the range , preceded by the number // of grades, and print out how many times each grade appears int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will be the no. of times grade i appears // Initialize frequencies f[k] to 0. int k= 0; while (k != f.length) {f[k]= 0; k= k+1;}

CS100Lecture 1017 // Read in the grades and make up array f int i= 0; // Inv: i grades have been read in and // each f[k], for 0<=k<=100, contains // the no. of times grade k was read in while (i != f.length) { int grade= Integer.parseInt(stdin.readLine() ); f[grade]= f[grade] + 1; i= i+1; }

CS100Lecture 1018 // Print the grades and their frequency (print only the // grades in that appeared at least once in the // input) int i= 0; // Inv: the grades 0..i-1 that occurred at least // once have been printed while (i != f.length) { if (f[i] != 0) System.out.println(“grade: ” + i + “ frequency: ” + f[i]); i= i+1; }

CS100Lecture 1019 Palindromes A palindrome is a word that reads the same backwards and forwards. –The empty string –A –AA –ABBA –NOON The following are palindromes if blanks and punctuation are ignored –able was I ere I saw elba –a man a plan a canal panama

CS100Lecture 1020 Is a given array a palindrome? // Return the value of the statement “array b is a palindrome” static public bool isPalindrome(char[ ] b) { int i= 0; int j= b.length; // Invariant: b is a palindrome iff b[i..j-1] is. In other // words, b[j..length-1] is the reverse of b[0..i-1] while ( j - i >1) { j= j-1; if (b[i] != b[j]) return false; i= i+1; } // {b[i..j-1] has 0 or 1 elements, so it’s a palindrome} return true; }

CS100Lecture 1021 Odd Syntax Thing int[] grades; is equivalent to int grades[]; Be careful however: int a, b, c; int[] x, y, z; int r[], s, t[]; Best to associate array brackets with the type not the variable name

CS100Lecture 1022 Initializer Lists You can initialize the elements of an array just as you can initialize a variable Example int[] grades = {88, 92, 65, 77, 33}; grades is now an array of 5 integers with values as shown Such an initializer list can only be used when the array is first declared Note curly braces

CS100Lecture 1023 Arrays of objects Remember: Java allows arrays of more than ints Suppose you would like an array of strings: String[] words = new String[25]; We could declare an array of employees, as we saw previously: Employee[] workers = new Employee[25]; workers[3] = new Employee(“Millett”, 1999); workers[3].setSalary(50000);

CS100Lecture 1024 Arrays as parameters Similar to when passing objects as parameters Declaration: z(int[] list) Call: z(a) where a is declared int[] z can change elements of the array, but not array reference itself Can also pass elements of the array as parameters Declaration: q(int x) Call: q(a[3])