Here is the array you'll be woking with: 3 2 5 2 7 4 3 5 Enter target value: 3 Target 3 occurs in the list at position: 0 Target 3 occurs in the list at position: 6 Goodbye.or
Here is the array you'll be woking with: 3 2 5 2 7 4 3 5 Enter target value: 8 Goodbye.
Here is the array you'll be woking with: 3 2 5 2 7 4 3 5 Enter target value: 8 Target 8 is not in the list. Goodbye.Hint: One way to do it is to use a variable that counts the number of times the target is found. Another way is to use a boolean variable that keeps track whether the target has been found or not; maintain this variable through the search loop.
//this variable keeps track whether the target has been found or not boolean isFound; //initially we have not found it yet isFound = false; //search loop while (..) { //if we find target set isFound to true } //at the end of the loop isFound should be true if and only if //the target has been found at least once if (isFound == false) { //tell the user the target is not in the list }
Here is the array you'll be woking with: 3 2 5 2 7 4 3 5 Enter target value: 3 Target 3 occurs in the list at position: 0 6 Goodbye.or
Here is the array you'll be woking with: 3 2 5 2 7 4 3 5 Enter target value: 8 Target 8 is not in the list. Goodbye.Hint: use your boolean variable isFound from part (b). Based on it you can figure out if it is the first time you find the target, or not.
//this variable keeps track whether the target has been found or not boolean isFound; //initially we have not found it yet isFound = false; //search loop while (..) { //compare target to element i //if we find target then //if its the first time we find it then //print "The target occurs at position: " //set isFound to true //print position //increment loop counter } //at the end of the loop isFound should be true if and only if //the target has been found at least once if (isFound == false) { //tell the user the target is not in the list }
Here is the array you'll be woking with: 3 2 5 2 7 4 3 5 Do you want to search? (Press y for yes, n for n): y Enter target value: 3 Target 3 occurs in the list at position 0 6 Do you want to search? (Press y for yes, n for n): y Enter target value: 5 Target 5 occurs in the list at position 2 7 Do you want to search? (Press y for yes, n for n): y Enter target value: 9 Target 9 is not in the list Do you want to search? (Press y for yes, n for n): n Goodbye.
What size will your array be? 5 Enter element 0: 2 Enter element 1: 8 Enter element 2: 5 Enter element 3: 4 Enter element 4: -1 Thank you. The largest number is 8 at position 1. Goodbye.If the numbers in the list (array) are not unique, the largest number could occur more than once. Would your algorithm find the first occurence? The last? Explain.