1 / 46

Arrays

Arrays. Arrays Data structures Related data items of same type Remain same size once created Fixed-length entries. Arrays. Array Group of variables Have same type Reference type (objects). -45. 6. 0. 72. 1543. -89. 0. 62. -3. 1. 6453. 78. c[ 0 ].

smejia
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays • Arrays • Data structures • Related data items of same type • Remain same size once created • Fixed-length entries

  2. Arrays • Array • Group of variables • Have same type • Reference type (objects)

  3. -45 6 0 72 1543 -89 0 62 -3 1 6453 78 c[ 0 ] Name of array (note that all elements of this array have the same name, c) c[ 1 ] c[ 2 ] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] c[ 10 ] Index (or subscript) of the element in array c c[ 11 ] A 12-element array.

  4. Arrays (cont.) • Index • Also called subscript • Position number in square brackets • Must be positive integer or integer expression a = 5; b = 6; c[ a + b ] += 2; • Adds 2 to c[ 11 ]

  5. Arrays (cont.) • Examine array c • c is the array name • c.length accesses array c’s length • c has 12 elements ( c[0], c[1], … c[11] ) • The value of c[0] is –45

  6. Declaring and Creating Arrays • Declaring and Creating arrays • Arrays are objects that occupy memory • Created dynamically with keyword new int c[] = newint[ 12 ]; • Equivalent toint c[]; // declare array variable c = newint[ 12 ]; // create array • We can create arrays of objects too String b[] = new String[ 100 ];

  7. Examples Using Arrays • Declaring arrays • Creating arrays • Initializing arrays • Manipulating array elements

  8. Examples Using Arrays (Cont.) • Creating and initializing an array • Declare array • Create array • Initialize array elements

  9. Examples Using Arrays (Cont.) • Using an array initializer • Use initializer list • Items enclosed in braces ({}) • Items in list separated by commas int n[] = { 10, 20, 30, 40, 50 }; • Creates a five-element array • Index values of 0, 1, 2, 3, 4 • Do not need keyword new in this usage.

  10. Array with two dimensions int my_array1[][]; // Declaring 2-dimension // array int b1[][] = {{2, 7}, {5, 3}, {2, 1}}; int b2[][] = {{5, 1, 9}, {3, 4}, {4, 1, 2}};

  11. my_array1 = new int [3][4]; my_array2 = new int [3][]; my_array2[0] = new int [4]; my_array2[1] = new int [2]; my_array2[2] = new int [3];

  12. Use of length length variable is available with arrays - gives the number of cells int my_array[] = new int[10]; my_array.length has a value 10 int a[][] = new int[3][4]; a.length has a value 3 a[0].length has a value 4

  13. my_array2 = new int [3][]; my_array2[0] = new int [4]; my_array2[1] = new int [2]; my_array2[2] = new int [3]; What is the value of my_array2[1].length?

  14. Problem 1 We have a list of all integers between 2 and 1000. Our problem is to find all the prime numbers in the list. Idea : If a number is prime, all multiples of the number are not prime. 3 is prime. Therefore 6, 9, 12, …., 999 are not prime.

  15. An example Suppose we have a table containing numbers 0, 1, 2, … , 24.

  16. An example We ignore the first 2 elements. The next element is 2. This must be a prime. So 2 is a prime. Now we delete all numbers which are multiples of 2.

  17. An example The next undeleted element is 3. This must be a prime. So 3 is a prime. So we delete all numbers which are multiples of 3.

  18. The next undeleted element 5 must be a prime. We delete all numbers which are multiples of 5. Continuing in this way we are left with only prime numbers.

  19. Question - Why do we need to check what number is saved in which cell? We know that cell # n contains # n!

  20. Question - Why do we need to check what number is saved in which cell? We know that cell # n contains # n

  21. Idea : We start off an empty table and as soon as we determine that the index of a cell is not prime, we will put a X in that cell.

  22. Our algorithm for finding primes Step 1: Initialize a table so that no cell is marked with X Step 2: While we are not finished repeat steps 3- 5 Step 3 : Pick the next cell which is not marked X. Step 4 : n = the index of this cell. Step 5 : mark with X all cells with indices which are multiples of n. Question - How to mark a cell with X? Why don’t we put true in all cells. To denote that a cell is marked with X, we will put false in that that cell.

  23. Reminder • General structure of a simple program : import javax.swing.*; public class YourClassName {public static void main(String args[]) {Body of your program } } This will be replaced by your decomposition

  24. public static void main(String args[]) {Step 1: Initialize a table so that no cell is marked with X Step 2: While we are not finished repeat steps 3- 5 Step 3 : Pick the next cell which is not marked X. Step 4 : n = the index of this cell. Step 5 : mark with X all cells with indices which are multiples of n. }

  25. public static void main(String args[]) { boolean table[]; table = new boolean[1000]; int CurrentCellNumber = 2, index; for (index = 0; index < table.length;index++) table[index] = true; Step 2: While we are not finished repeat steps 3- 5 Step 3 : Pick the next cell which is not marked X. Step 4 : n = the index of this cell. Step 5 : mark with X all cells with indices which are multiples of n. }

  26. public static void main(String args[]) { boolean table[] = new boolean[1000]; int CurrentCellNumber = 2, index; for (index = 0; index < table.length;index++) table[index] = true; while (CurrentCellNumber < table.length ) { Step 3 : Pick the next cell which is not marked X. Step 4 : n = the index of this cell. Step 5 : mark with X all cells with indices which are multiples of n. } }

  27. public static void main(String args[]) { boolean table[] = new boolean[1000]; int CurrentCellNumber = 2, index; for (index = 0; index < table.length;index++) table[index] = true; while (CurrentCellNumber < table.length ) { while (!table[CurrentCellNumber]) CurrentCellNumber++; Step 4 : n = the index of this cell. Step 5 : mark with X all cells with indices which are multiples of n. } }

  28. public static void main(String args[]) { boolean table[] = new boolean[1000]; int CurrentCellNumber = 2, index, n; for (index = 0; index < table.length;index++) table[index] = true; while (CurrentCellNumber < table.length ) {if !table[CurrentCellNumber] CurrentCellNumber++; n = CurrentCellNumber; Step 5 : mark with X all cells with indices which are multiples of n. } }

  29. public static void main(String args[]) { boolean table[] = new boolean[1000]; int CurrentCellNumber = 2, index, n; for (index = 0; index < table.length;index++) table[index] = true; while (CurrentCellNumber < table.length ) {if !table[CurrentCellNumber] CurrentCellNumber++; n = CurrentCellNumber; for (index = 2*n; index <table.length; index += n) table[index] = false; } }

  30. Problem 2 Find the nth prime number. Hints : 1) The 1st and 2nd prime # are 2 and 3. 2) All other prime numbers must be expressible as 6n - 1 or 6n + 1. 3) If a number N is not divisible by all prime numbers less that or equal to sqrt(N), N must be prime.

  31. Decomposition of the problem Step 1) Read the value of N Step 2) Calculate PN, the Nth prime Step 3) Display the value of PN

  32. Some introspection How do I know whether I have the Nth prime? The only way is to keep generating successive prime numbers and stop when we get the Nth prime number. I already know that the first and the second prime numbers are 2 and 3.

  33. Step A) count = 2 Step B) while (count < N) repeat steps C-D Step C) count = count + 1; Step D) find Pcount, the next prime number Steps A, B and C are trivial. How to find Pcount?

  34. Technique I When generating the next number to be tested, why do we simply generate the next odd number? We know that prime numbers must be expressible as 6n - 1 or 6n + 1. Why waste time on other numbers?

  35. Technique II When testing whether a given number is prime, why do we simply try dividing it by successive odd numbers? For example, if a number is not divisible by 3, is there any point in dividing it by 9 or 15? In other words, if a number is not divisible by 3, 5 or 7, the next divisor should be 11 and the next one should be 13 and so on.

  36. Technique II This means that we only need to use prime numbers as trial divisors!! We also must have a table of all prime numbers we generated so far so that we can use them as trial divisors.

  37. An example to illustrate the process Suppose we have to find the 10th prime number. In this process, we will generate 9 prime numbers before we find the 10th prime. We create an array which can save 10 numbers and start filling the array with prime numbers. When we have filled the last cell, we are done.

  38. We know that the first 2 prime numbers are 2 and 3. Therefore the table looks like We are now trying to find the next prime #. What should be our trial number? If it is not expressible as 6n - 1 or 6n + 1, we need not bother.

  39. This means we try with 5. Is 5 a prime? 5 < 32 Therefore, we need not try dividing 5 by 3. 5 must be prime. We can save it in our table of prime numbers.

  40. Proceeding in this way, when the 9th cell in the table is filled up we have We now have to generate the next prime number and save it in the 10th cell.

  41. Step 1) While ( we are not done) Step 2) Generate the next candidate number Step 3) Check if it is prime

  42. Step 1) While ( we are not done) Step 2) Generate the next candidate number Step 3) Check if it is prime What is the next candidate number? 25. Is it prime? No. What is the next candidate number? 29. Is it prime? Yes.

  43. Calculate PN, the Nth prime Step A) Create a table of N int Step B) Fill the first 2 cells with 2 and 3 Step C) While (the last cell of the table is empty) Step D) Generate next candidate number X Step E) If the number X is prime, save it in the first empty cell.

  44. How to generate the next candidate number? Examples of candidate numbers n = 1, 6n - 1 is 5, 6n + 1 is 7 n = 2, 6n - 1 is 11, 6n + 1 is 13 n = 3, 6n - 1 is 17, 6n + 1 is 19 n = 4, 6n - 1 is 23, 6n + 1 is 25 n = 5, 6n - 1 is 29, 6n + 1 is 31

  45. How to generate the next candidate number? Step 1) If the last number was generated using the formula 6n - 1, for some n, use the formula 6n + 1. Step 2) If ( the last number was generated using the formula 6n + 1, for some n), a) increase the value of n b) use the formula 6n - 1.

More Related