DOC PREVIEW
WUSTL CSE 131 - sp14_3

This preview shows page 1-2-3-27-28-29 out of 29 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 29 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1A Foundation for ProgrammingArraysMany Variables of the Same TypeMany Variables of the Same TypeMany Variables of the Same TypeArrays in JavaArrays in JavaVector Dot ProductArray-Processing ExamplesSlide 11Setting Array Values at Compile TimeSetting Array Values at Run TimeSlide 14War Story (PlanetPoker.com)ShufflingShuffling a Deck of Cards: Putting Everything TogetherCoupon Collector: Scientific ContextCoupon Collector ProblemCoupon Collector: Mathematical ContextCoupon Collector: Java ImplementationSlide 22Two-Dimensional ArraysTwo-Dimensional Arrays in JavaSetting 2D Array Values at Compile TimeMatrix AdditionMatrix MultiplicationArray ChallengeSummaryModule 3: Arrays (Chapter 1.4)Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 1/14/19 06:26:24 AM2A Foundation for Programmingobjectsfunctions and modulesgraphics, sound, and image I/Oarraysany program you might want to writeconditionals and loopsMath text I/Oassignment statementsprimitive data typesstore and manipulatehuge quantities of data3ArraysStore and manipulate huge quantities of data. Examples.52 playing cards in a deck.All the students in CSE 1311 million characters in a book.10 million audio samples in an MP3 file.Example question: 1) valentine’s chocolate problem2) Output a sorted set of cards3) Output a shuffled set of cards4Many Variables of the Same TypeGoal. 10 variables of the same type.// tedious and error-pronedouble a0, a1, a2, a3, a4, a5, a6, a7, a8, a9; a0 = 0.0;a1 = 0.0;a2 = 0.0;a3 = 0.0;a4 = 0.0;a5 = 0.0;a6 = 0.0;a7 = 0.0;a8 = 0.0;a9 = 0.0;…a4 = 3.0;…a8 = 8.0;…double x = a4 + a8;5Many Variables of the Same TypeGoal. 10 variables of the same type.double[] a; // declarationa = new double[10]; // creation…a[4] = 3.0;…a[8] = 8.0;…double x = a[4] + a[8];6Many Variables of the Same TypeGoal. 1 million variables of the same type.// scales to handle large arraysdouble[] a = new double[1000000];…a[123456] = 3.0;…a[987654] = 8.0;…double x = a[123456] + a[987654];declares, creates, and initializesin a same sentence7Arrays in JavaJava has special language support for arrays.To make an array: declare, create, and initialize it.To access entry i of array named a, use a[i]. Array indices start at 0.Size of an array a: a.lengthint N = 10; // size of arraydouble[] a; // declare the arraya = new double[N]; // create the arrayfor (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.08Arrays in JavaJava has special language support for arrays.To make an array: declare, create, and initialize it.To access entry i of array named a, use a[i]. Array indices start at 0.Compact alternative. Declare, create, and initialize in one statement.Default initialization: all numbers automatically set to zero.int N = 10; // size of arraydouble[] a = new double[N]; // declare, create, initint N = 10; // size of arraydouble[] a; // declare the arraya = new double[N]; // create the arrayfor (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.09Vector Dot ProductDot product. Given two vectors x[] and y[] of length N, their dot product is the sum of the products of their corresponding components.double[] x = { 0.3, 0.6, 0.1 }; double[] y = { 0.5, 0.1, 0.4 }; int N = x.length;double sum = 0.0; for (int i = 0; i < N; i++) { sum = sum + x[i]*y[i];}This syntax initializes an array with the listed values.10Array-Processing ExamplesIf you didn’t know about Double.NEGATIVE_INFINITY, can you think of another way to initialize max?Output a Deck12Setting Array Values at Compile TimeEx. 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 12int j = (int) (Math.random() * 4); // between 0 and 3System.out.println(rank[i] + " of " + suit[j]);Safer, and easier, to say: rank.length instead of 1313Setting Array Values at Run TimeEx. Create a deck of playing cards and print them out.Q. In what order does it output them?A. B.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 k = 0; k < 52; k++) System.out.println(deck[k]);typical array-processing code changes valuesat runtimetwo of clubstwo of diamondstwo of heartstwo of spadesthree of clubs...two of clubsthree of clubsfour of clubsfive of clubssix of clubs...Shuffling a Deck15War Story (PlanetPoker.com)Texas hold 'em poker. Software must shuffle electronic deck of cards.How we learned to cheat at online poker: a study in software securityhttp://itmanagement.earthweb.com/entdev/article.php/61622116ShufflingGoal. 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].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;}between i and N-1swapidiom17Shuffling a Deck of Cards: Putting Everything Togetherpublic 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]); }}avoid "hardwired" constantsbuild the deckshuffleprint shuffled deck18Coupon Collector: Scientific ContextQ. Given a sequence from nature, does it have same characteristicsas 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


View Full Document

WUSTL CSE 131 - sp14_3

Documents in this Course
sp14_4

sp14_4

28 pages

sp14_2

sp14_2

43 pages

sp14_10

sp14_10

19 pages

sp14_9

sp14_9

16 pages

sp14_8

sp14_8

22 pages

sp14_7

sp14_7

33 pages

sp14_6

sp14_6

27 pages

sp14_5

sp14_5

55 pages

lecture1

lecture1

33 pages

lab0

lab0

6 pages

Load more
Download sp14_3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view sp14_3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view sp14_3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?