Download presentation
Presentation is loading. Please wait.
Published byElwin Owen Modified over 9 years ago
1
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
2
Recap of Arrays
3
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
4
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. 4 0 1 2 3 4 5 N-2 N-1 a[0] a[1] a[2] 5 6 10 Addresses
5
Examples of Programs that Use Arrays 5
6
Shuffling a Deck of Cards
7
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
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
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].
10
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 44 55 66 22 33 10 JJ Value88 99 23450189Array index67 99 22 random integer = 7
11
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 44 55 66 99 33 10 JJ Value88 22 23450189Array index67 55 33 random integer = 3
12
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 44 33 66 99 55 10 JJ Value88 22 23450189Array index67 JJ 44 random integer = 9
13
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 JJ 44 88 33 99 55 66 22 Value10 77 23450189Array index67
14
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
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
16
Coupon Collector
17
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
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
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.