A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.

Slides:



Advertisements
Similar presentations
New features in JDK 1.5 Can these new and complex features simplify Java development?
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Shuffle an Array Shuffle a deck of cards. n In i th iteration, choose a random element from remainder of deck and put at index i. – choose random integer.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Coupon collector’s problem CS658 Po-Ching Liu. Question A person trying to collect each of b different coupons must acquire approximately x randomly obtained.
Describing Probability
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Midterm 2 Practice CIS 113 Spring public class Card { public int rank; public String suit; Card(){rank = -1; suit = "-";} Card(Card card){rank =
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
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.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 Pearson Education, Inc. All rights reserved Arrays.
UNR, MATH/STAT 352, Spring Radar target detection How reliable is the signal on the screen? (Is it a target of a false alarm?)
VOCABULARY  Deck or pack  Suit  Hearts  Clubs  Diamonds  Spades  Dealer  Shuffle  Pick up  Rank  Draw  Set  Joker  Jack 
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
An Overview of JAVA From basic to slightly less basic Jonathan-Lee Jones.
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
EMB1006 JAVA Part 2 Arrays and onwards Jonathan-Lee Jones.
Chapter 6 Arrays and Array Lists.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
 2007 Pearson Education, Inc. All rights reserved C Pointers.
Two Way Tables Venn Diagrams Probability. Learning Targets 1. I can use a Venn diagram to model a chance process involving two events. 2. I can use the.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
15.3 Counting Methods: Combinations ©2002 by R. Villar All Rights Reserved.
1.4 Arrays "... By the way, we rank 10 th among the industrialized world in broadband technology and its availability. That’s not good enough for America.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
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.
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 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅.
CSE 373 Data Structures and Algorithms
Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.
Comparison-Based Sorting & Analysis Smt Genap
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Heads-Up – Programming Quiz This Week Announcement will be made via You will be provided with a login to the password-protected website You will.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Single Dimensional Arrays
Chapter 7 - Pointers Outline 7.1 Introduction
Chapter 7 - Pointers Outline 7.1 Introduction
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/6/11 12:33.
Stacks, Queues, ListNodes
Presentation transcript:

A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab

Recap of Arrays

n To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0. Compact alternative. 3 Arrays in Java Indexed sequence of values of the same type n To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0. int N = 10; double[] a; // declare the array a = new double[N]; // create the array for (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.0 int N = 10; double[] a = new double[N]; // declare, create, init

Memory Representation and Allocation of Arrays Arrays occupy consecutive addresses in the program address space: int[] a = {5, 6, 10}; Arrays need to be explicitly allocated memory. Arrays cannot grow and cannot shrink Name of the array indirectly references its starting address. int[] b = new int[3]; b=a; //b references the SAME data in memory N-2 N-1 a[0] a[1] a[2] Addresses

Examples of Programs that Use Arrays 5

Shuffling a Deck of Cards

7 Setting Array Values at Compile Time Ex. Print a random card. String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; int i = (int) (Math.random() * 13); // between 0 and 12 int j = (int) (Math.random() * 4); // between 0 and 3 System.out.println(rank[i] + " of " + suit[j]);

8 Setting Array Values at Run Time Ex. Create a deck of playing cards and print them out. String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] deck = new String[52]; for (int i = 0; i < 13; i++) for (int j = 0; j < 4; j++) deck[4*i + j] = rank[i] + " of " + suit[j]; for (int i = 0; i < 52; i++) System.out.println(deck[i]);

9 Shuffling Goal. Given an array, rearrange its elements in random order. Shuffling algorithm. In iteration i, pick random card from deck[i] through deck[N-1], with each card equally likely. Exchange it with deck[i].

Shuffle an Array Iteration i=0 Shuffle a deck of cards. n In i th iteration, choose a random element from remainder of deck and put at index i. – choose random integer r between i and N-1 – swap values in positions r and i 44 55 66  22 33 10  JJ Value88 99 Array index67 99 22 random integer = 7

Shuffle an Array Iteration i=1 Shuffle a deck of cards. n In i th iteration, choose a random element from remainder of deck and put at index i. – choose random integer r between i and N-1 – swap values in positions r and i 44 55 66  99 33 10  JJ Value88 22 Array index67 55 33 random integer = 3

Shuffle an Array Iteration i=2 Shuffle a deck of cards. n In i th iteration, choose a random element from remainder of deck and put at index i. – choose random integer r between i and N-1 – swap values in positions r and i 44 33 66  99 55 10  JJ Value88 22 Array index67 JJ 44 random integer = 9

After the Final Iteration Shuffle a deck of cards. n In i th iteration, choose a random element from remainder of deck and put at index i. – choose random integer r between i and N-1 – swap values in positions r and i JJ 44 88 33 99 55 66 22 Value10  77 Array index67

14 Shuffling In iteration i, pick random card from deck[i] through deck[N-1], with each card equally likely. Exchange it with deck[i]. The cards in the deck should be the same as those before the shuffle. Need to pick a random card from those not already chosen for the shuffle. int N = deck.length; for (int i = 0; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t; } swap idiom

15 Shuffling a Deck of Cards public class Deck { public static void main(String[] args) { String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; int SUITS = suit.length; int RANKS = rank.length; int N = SUITS * RANKS; String[] deck = new String[N]; for (int i = 0; i < RANKS; i++) for (int j = 0; j < SUITS; j++) deck[SUITS*i + j] = rank[i] + " of " + suit[j]; for (int i = 0; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); String t = deck[r]; deck[r] = deck[i]; deck[i] = t; } for (int i = 0; i < N; i++) System.out.println(deck[i]); } } build the deck shuffle print shuffled deck Dr. Java Demo

Coupon Collector

17 Coupon Collector Problem Coupon collector problem. Given a shuffled deck of cards and N different card types, how many do you have to collect before you have (at least) one of each type? (Assume each possibility is equally likely for each card you collect) Simulation algorithm. Repeatedly choose an integer i between 0 and N-1. Stop when we have at least one card of every type. Q. How to check if we've seen a card of type i ? A. Maintain a boolean array so that found[i] is true if we've already collected a card of type i.

18 Coupon Collector: Java Implementation public class CouponCollector { public static void main(String[] args) { int N = Integer.parseInt(args[0]); // value range int cardcnt = 0; // number of cards collected int valcnt = 0; // number of distinct cards // do simulation boolean[] found = new boolean[N]; while (valcnt < N) { int val = (int) (Math.random() * N); cardcnt++; if (!found[val]) valcnt++; found[val] = true; } System.out.println(cardcnt); } } type of next card (between 0 and N-1) Dr. Java Demo

19 Coupon Collector: Scientific Context Q. Given a sequence from nature, does it have same characteristics as a random sequence? A. No easy answer - many tests have been developed. Coupon collector test. Compare number of elements that need to be examined before all values are found against the corresponding answer for a random sequence.