Arrays and Algorithms Chapter 7 day 2 Sentinel



![public static boolean arr. Equal(int[] arr 1, int[] arr 2) { if(arr 1. length public static boolean arr. Equal(int[] arr 1, int[] arr 2) { if(arr 1. length](https://slidetodoc.com/presentation_image/d8e3cc41fd1b57045a29f8690b9623f2/image-4.jpg)
![public static boolean arr. Equal(int[] arr 1, int[] arr 2) { boolean equals = public static boolean arr. Equal(int[] arr 1, int[] arr 2) { boolean equals =](https://slidetodoc.com/presentation_image/d8e3cc41fd1b57045a29f8690b9623f2/image-5.jpg)

![public static int index. Of(int[] arr, int target) { for(int i = 0; i public static int index. Of(int[] arr, int target) { for(int i = 0; i](https://slidetodoc.com/presentation_image/d8e3cc41fd1b57045a29f8690b9623f2/image-7.jpg)
![public static int index. Of(int[] arr, int target) { int index = -1; for(int public static int index. Of(int[] arr, int target) { int index = -1; for(int](https://slidetodoc.com/presentation_image/d8e3cc41fd1b57045a29f8690b9623f2/image-8.jpg)

![public static int max. Value(int[]arr) { int max = arr[0]; for(int i = 1; public static int max. Value(int[]arr) { int max = arr[0]; for(int i = 1;](https://slidetodoc.com/presentation_image/d8e3cc41fd1b57045a29f8690b9623f2/image-10.jpg)

- Slides: 11
Arrays and Algorithms Chapter 7 day 2
Sentinel § A sentinel is a variable that will store a specific value until some condition is met.
§ If I have two arrays, arr 1 and arr 2, why can’t I check to see if the are the same using arr 1 == arr 2 Are two arrays equal? § Because the variables are storing references, so you are not checking the contents, but rather you are checking to see if they are stored at the same memory address. § You must check each element individually.
public static boolean arr. Equal(int[] arr 1, int[] arr 2) { if(arr 1. length != arr 2. length) return false; for(int i = 0; i < arr 1. length; i++) arr 1 equal to arr 2? { if(arr 1[i] != arr 2[i]) return false; } return true; }
public static boolean arr. Equal(int[] arr 1, int[] arr 2) { boolean equals = true; if(arr 1. length != arr 2. length) equals = false; for(int i = 0; i < arr 1. length && equals; i++) With sentinel { if(arr 1[i] != arr 2[i]) equals = false; } return equals; }
§ Work through the elements in the array one Find an element in an array at a time until you find what you are looking for. § Once found, return the index where it was found § If you don’t find it, return -1 typically
public static int index. Of(int[] arr, int target) { for(int i = 0; i < arr. length; i++) { index. Of if(arr[i] == target) return i; } return -1; }
public static int index. Of(int[] arr, int target) { int index = -1; for(int i = 0; i < arr. length; i++) { With sentinel if(arr[i] == target) index = i; } return index; }
§ To find the largest or smallest value in an array, we take the following steps: § Create a local variable for max (or min) and set it equal to Min/max algorithm the first element § Loop through each element of the array § If an element is larger than max(or smaller than min) than that is the new max (or min) § Once you get through the list, the value in the variable is what you are looking for
public static int max. Value(int[]arr) { int max = arr[0]; for(int i = 1; i < arr. length; i++) { max if (arr[i] > max) max = arr[i]; } return max; }
§ Write a class called Driver. Exam which has: § a field that is a char array called correct. Answers. § One constructor which takes a char array as a parameter and initializes the field with an array containing the correct answers to the exam § Methods: § passed: takes a char array called student. Answers as a parameter Homework (contains a students answers) and returns true if the student passed the exam (75%) and false if they did not § total. Correct: takes a char array called student. Answers and returns the number correct § total. Incorrect: takes a char array called student. Answers as a parameter and returns the number incorrect § questions. Missed: takes a char array called student Answers as a parameter and returns an int array with the question numbers missed. Hint: every time you add a question you need to make a new array because size can’t be changed. Also question numbers will be one more than the index of the missed question.