Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted...

14
Array Processing - 2

Transcript of Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted...

Page 1: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Array Processing - 2

Page 2: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Objectives

Demonstrate a swap. Demonstrate a linear search of an unsorted

array Demonstrate how to search an array for a

high value.

Page 3: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Swapping two elements in an array. Often have to swap the values in two cells in an array. The following code attempts to swap the third and fourth

element in the array (remember arrays are 0-based). IT DOESN’T WORK properly. Why? What are the resulting values in the array?

int[] anArray = { 24, 36, 64, 48 };anArray[2] = anArray[3];anArray[3] = anArray[2];

Page 4: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Demo – Swap two array elements.

Demo ArraySwap.java

Page 5: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Linear Search for a Value.

Commonly need to search arrays to see if they contain a value.

Technique involves : Iterate from first through last element of the

array. For each iteration, compare value at current

index with searchValue. If it is equal you can stop your search otherwise continue.

Stop searching at the end of the array.

Page 6: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Demo: Linear Search

public class LinearSearch1 { public static void main() { boolean found = false; int searchValue = 36; int[] anArray = { 24, 36, 64, 48 }; for( int i = 0 ; i < anArray.length && found != true ; i++ ){ if( anArray[i] == searchValue ){ found = true; } } System.out.println(“Our search for " + searchValue + “ returned “ + found ); }}

Page 7: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Finding a high value.

Commonly need to find the largest or smallest element in an array.

For a high value search: Create a simple variable, highValue, to store

our highest value. Set it to the first element of the array.

Iterate from second through last element of the array.

For each iteration, compare value at current index with stored highValue. If it is greater it is the new high value, so reset highValue.

Page 8: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Demo: Find High Value

public class HighValueSearch { public static void main() { int highValue = 0; int[] anArray = { 24, 36, 64, 48 }; highValue = anArray[0]; for( int i = 1 ; i < anArray.length; i++ ){ if( anArray[i] > highValue ){ highValue = anArray[i]; } } System.out.println("High Value is " + highValue ); }}

Page 9: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Your Turn!

Page 10: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Task:SimpleStats.java

We want to write some code to generate simple statistics from our temperature array. Declare and initialize an array with the values 21.2, 22.0, 24.5, 27.2 , 26.4. Write the code to detect the maximum value, the minimum value, the range (highest value minus lowest value) and the average temperature.

Page 11: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Passing and Returning Arrays.

You will often want to pass an array to method or return an array from a method.

Page 12: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Array Parameters.

you can use arrays as parameters.

public static void printArray(double[] numbers) { for( double number: numbers ){ System.out.println( number); }}

....main double[] anArray = { 1.0,2.0,3.0 }; printArray( anArray );

...end main.

Page 13: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Array Parameters.

you can use arrays as parameters.

public static int[] getMonthLengths() { int[] monthLengths = {31,28,31,30,31,30,31,31,30,31,30,31}; return monthLengths; }}

....main int[] anArray = getMonthLengths();

...end main.

Page 14: Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.

Your Turn

Your previous in-class assignment was supposed to generate simple statistics (min,max,range,and mean). Now refactor this code this code so that the stats are calculate in a getStats method. getStats receives an array of double and returns an array of stats representing the min [0],max [1], range [2] and mean [3]. Use the method by calling it from main() and printing the output.

public static double[] getStats( double[] temp );