Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named minGap that accepts the array entered by the user as a parameter and returns the minimum 'gap' between adjacent values in the array. The main method should then print the value returned by the method. The gap

Answers

Answer 1

Answer:

In Java:

import java.util.*;

class Main {  

public static void minGap(int intArray[], int arrlength) {  

 if(arrlength <2){return;}

 int minm = Math.abs(intArray[1] - intArray[0]);  

 for (int i = 2; i < arrlength; i++)  

  minm = Math.min(minm, Math.abs(intArray[i] - intArray[i - 1]));  

 System.out.print("Minimum Gap = " + minm);  

}  

public static void main(String arg[]) {  

 Scanner input = new Scanner(System.in);

 int arrlength;

 System.out.print("Array Length: ");

 arrlength = input.nextInt();

 int[] intArray = new int[arrlength];

 for(int i = 0;i<arrlength;i++){

     intArray[i] = input.nextInt();

 }

 minGap(intArray, arrlength);  

} }

Explanation:

The minGap method begins here

public static void minGap(int intArray[], int arrlength) {

This checks if array length is 1 or 0. If yes, the program returns nothing

 if(arrlength <2){return;}

If otherwise, this initializes the minimum gap to the difference between the 0 and 1 indexed array elements

 int minm = Math.abs(intArray[1] - intArray[0]);  

This iterates through the array elements

 for (int i = 2; i < arrlength; i++)  

This checks for the minimum gap

  minm = Math.min(minm, Math.abs(intArray[i] - intArray[i - 1]));  

At the end of the iteration, the minimum gap is printed

 System.out.print("Minimum Gap = " + minm);  

}  

The main method begins here

public static void main(String arg[]) {  

 Scanner input = new Scanner(System.in);

This declares the length of the array

 int arrlength;

This prompts the user for array length

 System.out.print("Array Length: ");

This gets the user input

 arrlength = input.nextInt();

This declares the array

 int[] intArray = new int[arrlength];

The following iteration gets input for the array

 for(int i = 0;i<arrlength;i++){

     intArray[i] = input.nextInt();

 }

This calls the minGap method  

minGap(intArray, arrlength);  

}


Related Questions

Match the data type to the given data.
1. float
2. array
3. Boolean
4. character

false
с
c, o, m, p, u, t, er
26.2

Answers

Float: 26.2
Array: c, o, m, p, u, t, er
Boolean: false
Character: c
Other Questions
The distance you travel while hiking is a function of how fast you hike and how long you hike at this rate. You usually maintain a speed of three miles per hour while hiking. Wow a statement that describes how the distance that you travel is determined. Then identify the independent and dependent variables if this function. how many states didn't allow women to vote during womens suffrage???? plz help Prove that the sum of five consecutive natural numbers is even.pls explain briefly and pls help it has 14 pts help please lol yuhaaaaaas Match the number of neutrons to the correct isotope by studying the atomic number and mass number of each isotope. Drag the items on the left to the correct location on the right. Tessa is training for a marathon. She runs 13 km a day for 3 days. How many meters did Tessa run in all? What are the abiotic and biotic factors of a Beach Coastal? Help me on this plsss :((((((((((((( i need help I would like my answer double-checked. I think it should be restructured like this:Flying over the forest, we saw three fires.I think the answer is misplaced. In addition, it cannot be dangling because we know who is flying. a reflection of the pink triangle is shown. which transformation makes this reflection Help me its urgent What is Basquiat's impact on Black Art? * 17. (05.06 LC)A person drives her own personal vehicle to work two days of the week. Her commute to and from work is 42 miles a day, and her car averages 28 miles per gallon? How much gasolineis the person conserving a day if she takes the train instead? (5 points)O 1.5 gallons per dayO 3.0 gallons per dayO 4.5 gallons per day0 5.5 gallons per day DO NOT SIMPLIFY YOUR ANSWERSSam owns a canoe rental company with 120 boats. He has 55 wood boats, and the rest are fiberglass. What is the ratio of...fiberglass boats to wood boats?total boats to fiberglass boats?wood boats to other boats?wood boats to total boats?*answers are NOT TO BE SIMPLIFIED. YOU MUST use the colon key. . NO SPACESOulz saved Form a sequence that has two arithmetic means between 13 and 88.a.13, 38, 63, 88c.13, 25, 25, 88b.13, 88, 163, 238d.13, 50.5, 88 Pls help me find KL!!!!! (-2,-4) and (-1, -5) Help me please!eee!! Romes legal code provided the basis for many laws and principles we use today.two examples include...