04_array - 1 1.4 Arrays " . . . By the way, we...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 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. Tenth is 10 spots too low as far as I’m concerned. " - George W. Bush 2 Arrays This lecture. Store and manipulate huge quantities of data. Array. Indexed sequence of values of the same type. Examples. 52 playing cards in a deck. 14 thousand undergrads at UVa. 1 million characters in a book. 10 million audio samples in an MP3 file. 4 billion nucleotides in a DNA strand. 73 billion Google queries per year. 50 trillion cells in the human body. 6.02 × 10 23 particles in a mole. humper 0 sullivan 1 soffa 2 knuth 3 billg 4 rms 5 mst3k 6 alvin 7 index value 3 Many Variables of the Same Type Goal. 10 variables of the same type. // tedious and error-prone double 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 ; a9 = 0.0 ; a9 = 0.0 ; double x = a4 + a8 ; 4 Arrays in Java Java has special language support for arrays. To make an array: declare, create, and initialize it. To access element i of array named a , use a[i] . Array indices start at . int N = 10 ; double a ; // declare the array a = new double [ N ]; // create the array for ( int i = ; i < N ; i ++) // initialize the array a [ i ] = 0.0 ; // all to 0.0 2 5 Arrays in Java Java has special language support for arrays. To make an array: declare, create, and initialize it. To access element i of array named a , use a[i] . Array indices start at . Compact alternative. Declare, create, and initialize in one statement. Default initialization: all numbers automatically set to zero. int N = 10 ; double a ; // declare the array a = new double [ N ]; // create the array for ( int i = ; 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 6 Vector Dot Product Dot 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 } ; double sum = 0.0 ; for ( int i = ; i < N ; i ++) { sum += x [ i ]* y [ i ]; } 7 Array Processing Code Shuffling a Deck 2 5 Arrays in Java Java has special language support for arrays....
View Full Document

Page1 / 8

04_array - 1 1.4 Arrays " . . . By the way, we...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online