Question
Read the method findMin() to understand its logic. (b) Fill in the method findMax() to find the largest number. It should look very similar to
Read the method findMin() to understand its logic.
(b) Fill in the method findMax() to find the largest number. It should look very similar to the find min() method. Test if it outputs the largest number in the array hard-coded in the program. 2
(c) The method findSecondMin() is supposed to find both the smallest and the second smallest numbers in the array. Fill in findSecondMin() the code to do so.
Test if it outputs the smallest and the second smallest numbers correctly. Compare your output of the method findMin().
Given Code
class COMP108W03 { // print the content of an array of size n static void printArray(int[] data, int n) { int i; for (i=0; i < n; i++) System.out.print(data[i] + " "); System.out.println(); } // data[] is an array, n is size of array, key is the number we want to find static void seqSearch(int[] data, int n, int key) { int i, count; boolean found; // start sequential search on the array called data[] found = false; // to indicate if the number is found i = 0; // an index variable to iterate through the array count = 0; // to count how many comparisons are made while (i // data[] is an array in ascending order, n is size of array, key is the number we want to find // You can assume that data[] is already sorted // refer to Lecture 5 static void binarySearch(int[] data, int n, int key) { } // print the smallest number in the array of size n static void findMin(int[] data, int n) { int i, min; min = data[0]; i = 1; while (i < n) { if (data[i] < min) min = data[i]; i++; } System.out.println("The smallest number is " + min + "."); } // print the largest number in the array of size n // refer to Lecture 7 static void findMax(int[] data, int n) { } // print the second smallest number in the array of size n // refer to Lecture 7 static void findSecondMin(int[] data, int n) { } // print the second largest number in the array of size n // refer to Lecture 7 static void findSecondMax(int[] data, int n) { } // print the smallest number and its position in an array of size n // Find the bug and fix it by altering ONE line of code static void bugOne(int[] data, int n) { int i, pos, min; pos = 0; min = 0; i = 1; while (i < n) { if (data[i] < min) { min = data[i]; pos = i; } i++; } System.out.println("The smallest number is " + min + " at position " + pos + "."); } } |
Step by Step Solution
3.41 Rating (145 Votes )
There are 3 Steps involved in it
Step: 1
All the methods which are necessary to run the application are implemented as follows BinarySearch data is an array in ascending order n is size of array key is the number we want to find You can assu...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started