Question
Write a static method named minGap that takes an integer array as a parameter and that returns the minimum gap between adjacent values in the
Write a static method named minGap that takes an integer array as a parameter and that returns the minimum gap between adjacent values in the array. The gap between two adjacent values in a list is defined as the second value minus the first value. For example, suppose a variable called "list" is an array of integers that stores the following sequence of values:
{1, 3, 6, 7, 12}
The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the call minGap (list) should return 1 because that is the smallest gap in the list. Notice that the minimum gap could be a negative number. For example, if list stores the following sequence of values
{3, 5, 11, 4, 8} then the gaps would be computed as 2 (5 - 3), 6 (11 - 5), -7 (4 - 11), and 4 (8 - 4). Of these values, -7 is the smallest, so it would be returned.
By the way, this gap information can be helpful for determining other properties of the list. For example, if the minimum gap is greater than or equal to 0, then you know the list is in sorted (nondecreasing) order. (Do you understand why?) If the gap is greater than 0, then you know the list is both sorted and contains unique values (i.e., it is strictly increasing).
If your method is passed an array with fewer than 2 elements, minGap should return 0. But sure to convincingly test your method out on a variety of arrays using a main program of your own design.
Write a static method named minGap that takes an integer array as a parameter and that returns the minimum gap between adjacent values in the array. The gap between two adjacent values in a list is defined as the second value minus the first value. For example, suppose a variable called "list" is an array of integers that stores the following sequence of values: {1,3,6,7,12} The first gap is 2(31), the second gap is 3(63), the third gap is 1(76) and the fourth gap is 5(127). Thus, the call minGap (list) should return 1 because that is the smallest gap in the list. Notice that the minimum gap could be a negative number. For example, if list stores the following sequence of values {3,5,11,4,8} then the gaps would be computed as 2(53),6(115),7(411), and 4(84). Of these values, -7 is the smallest, so it would be returned. By the way, this gap information can be helpful for determining other properties of the list. For example, if the minimum gap is greater than or equal to 0 , then you know the list is in sorted (nondecreasing) order. (Do you understand why?) If the gap is greater than 0 , then you know the list is both sorted and contains unique values (i.e., it is strictly increasing). If your method is passed an array with fewer than 2 elements, minGap should return 0 . But sure to convincingly test your method out on a variety of arrays using a main program of your own designStep by Step Solution
There are 3 Steps involved in it
Step: 1
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