Introduction to Programming Ms. Knudtzon Monday, Nov 15, C Period Week 11
Homework Check Abstract Lights Out Lab Homework 10 (Edward, Jesse) Grading (me) Week 11
The Incredibles Tuesday Class: Movie at 2:00 pm Talk about computer animation Movie at 2:00 pm Meet at Comp Lab at 1:25 Lunch? Box Lunches Chipotle? Bagels? Week 11
2D arrays Used to represent tables of values in rows and columns 1 2 3 1 2 3 4 5 6 7 8 On AB test only A test covers 1d arrays, AB test covers 2d arrays 1 2 Week 11
And remember: for loops and arrays go together! For a 2d array, we will need nested for loops: for(int row = 0; row < array.length; row++){ for(int col = 0; col < array[row].length; col++){ System.out.print(array[row][col] + “ “); } System.out.println(); Week 11
Example: DoubleArray.java Keeps track of rows of students and columns of grades Also has a second array that keeps track of the names that correspond to the rows of students Since arrays have to hold the same Data type in each element, we need the second array if we want to correspond the rows with actual student names. Week 11
Review of Pass-By Pass-by-Value Pass by reference When we pass an argument by value to a method, a copy of the argument’s value is made and passed to the called method Pass by reference When we pass by reference, the caller gives the called method the ability to access the caller’s data directly and possibly modify that data (it is not copied) Week 11
Trying it out… (Lab) Pass by value Pass by reference PassByExample.java Week 11
Special Computer Animation Lecture See Ms. K for the slides Tuesday, November 16 Special Computer Animation Lecture See Ms. K for the slides Week 11
Introduction to Programming Ms. Knudtzon Thursday, Nov 18, C Period Week 11
Problems on Homework 10 GradeBook homework Following specifications! Repeating code – SumArray did the work, AverageArray was supposed to call it Readable, easy to follow code Smallest and largest values in an array What should they be initialized to before you start searching? When do you print in a for loop/ when do you print when the loop has finished? Specifically for Summing elements or averaging elements Also for calculations, better not to repeat a calculation that isn’t needed (like computing average each time inside a loop) Week 11
Lights Out Lab What does “!” mean? It is used to take the opposite meaning in a boolean We’ve used != to check if something is not equal Can also be used to assign values In this lab, many of you wrote: if(lights[x] == true) { lights[x] = false; } But you could have just reversed the value without checking it: lights[x] = ! lights[x]; Week 11
Questions 1d arrays 2d arrays Pass by value Pass by reference Quiz Tomorrow Week 11
Sorting Arrays Sorting data is important for all sorts of computer applications Bubble sort Let smaller values slowly “bubble” their way to the top (of the array), and let larger values sink to the bottom Technique: use nested for loops to go through the array several times Each time through, compare successive pairs – if pair is in decreasing order, swap the values; if not, leave them as is D&D Section 7.7 Week 11
Bubble Sort public void bubbleSort (int array[]){ //loop to control the number of passes for(int pass = 1; pass < array.length; pass++){ //loop to control the number of comparisons for(int e = 0; e < array.length -1; e++){ //compare elements & swap if necessary if(array[e] > array[e + 1] ) swap(array, e , e+1); } Fig 7.10 D&D Week 11
swap public void swap(int array2[],int first,int second){ int holder; //temp holding area for swap holder = array2[first]; array2[first] = array2 [second]; array2[second] = holder; } Fig 7.10 D&D Week 11
How does this work? Run BlueJ code with the debugger to step through the program – Can use object inspector to examine the array at different points in the code’s execution Note: the array could also be a variable with class scope and then you wouldn’t have to pass it as a parameter Week 11
Searching If information is stored in arrays, it might be necessary at some point to see if an array contains a certain key value Linear Search Binary Search Week 11
Linear Search Step through every element in the array to see if the key value is in the array Works well for small arrays or for unsorted arrays For large arrays, linear searching is inefficient Week 11
Linear Search Method private int linearSearch(int array[], int key){ for(int i = 0; i < array.length; i++){ if(array[i] == key){ return i; } return -1; Week 11
Linear Search in Action Welcome to linear search First we create an array: Please enter the number of elements in the array > 10 Please enter element 1 > 3 Please enter element 2 > 36 Please enter element 3 > 347 Please enter element 4 > 235 Please enter element 5 > 346 Please enter element 6 > 46 Please enter element 7 > 23 Please enter element 8 > 63 Please enter element 9 > 36 Please enter element 10 > 36 Please enter the key value you want to find > 46 Now Searching the array.... Value was found stored in position: 6 Week 11
Note on searches Typically it isn’t done all in one program like this. In general, you would have things stored and due to a user inquiry, you would want to search for it. These examples just show the basics of how these algorithms work. Week 11
Binary Search For sorted arrays, binary search is a higher-speed search technique Eliminates half the elements in the array after each comparison by looking at the middle elements’ value - if value matches key returns that elements’ index, otherwise it can reduce the problem to one half of the array – and so on and so forth Week 11
Binary Search Method public int binarySearch(int array[], int key){ int low = 0; int high = array.length - 1; int middle; //loop until low index reaches high while (low <= high){ middle = (low + high) /2; if(key == array[middle] ) { return middle; //return the found location! } else if(key < array[middle]){ high = middle - 1; else{ low = middle + 1; return -1; //key not found while looping Week 11
Binary Search in Action Welcome to binary search!! First we create an array: Please enter the number of elements in the array > 5 Please enter element 1 > 35 Please enter element 2 > 3467 Please enter element 3 > 47 Please enter element 4 > 25 Please enter element 5 > 457 Please enter the key value you want to find > 35 Information: Array is being sorted Press any key to continue > Information: Array has been sorted Press any key to start searching the array for the keyValue: 35 > **** Value was found stored in position: 2 **** Press any key to see the sorted array. > Element 1 is 25 Element 2 is 35 Element 3 is 47 Element 4 is 457 Element 5 is 3467 Week 11