1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Maurinko [17]
3 years ago
7

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
Computers and Technology
1 answer:
Gala2k [10]3 years ago
4 0

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

<em>  if(arrlength <2){return;}</em>

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

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

<em>      intArray[i] = input.nextInt(); </em>

<em>  }</em>

This calls the minGap method  

minGap(intArray, arrlength);  

}

You might be interested in
list the authours who explained communication, pros and cos, importance of communication and the types of of communication and t
atroni [7]

Answer: the answer is A.

Explanation: hope this helps!

3 0
2 years ago
What needs to be done before depositing a check using a mobile app?
galben [10]

Answer:

The check needs to be signed in the back by you or whoever is depositing it.

7 0
2 years ago
Read 2 more answers
What would be the result of running the command chown :root file1.txt
REY [17]

Answer:

b. This would set the group ownership of file1 to root.

Explanation:

Linux allows user to have his own files and regulate the ability of other users to access them. The <em>chown</em> command allows you to use the appropriate utility to change the owner of a file or directory.

The basic command syntax is as follows:

# chown [options] <owner name: owner group name> <file or directory name>

For example, if you want to give a user <em>root</em> opportunity to use the <em>file1.txt</em> file as he wishes, you can use the following command:

# chown root file1.txt

In addition to changing the owner of a file, the group of its owners or both can be changed at the same time. Use a colon to separate the username and user group name (without the space character):

# chown user2:group2 file1.txt

As a result, the user with the name <em>user2</em> will become the owner of the <em>file1.txt</em> and its group will become <em>group2</em>.

In your case omitting username

# chown :root file1.txt

will change owner group only.

8 0
4 years ago
Consider the following static method, calculate.
vfiekz [6]

Answer:

return 8 * x

Explanation:

Given

The attached code segment

Required

Which single statement can replace the program body

From calculate(), we have:

x =  x + x;\\ x =  x + x;\\ x =  x + x;

The first line (x = x + x) implies that:

x=2x

So, on the next line; 2x will be substituted for x

i.e.

x = x + x becomes

x = 2x + 2x

x = 4x

So, on the third line; 4x will be substituted for x

i.e.

x = x + x becomes

x = 4x+ 4x

x = 8x

In programming: 8x = 8 * x:

This means that: return 8 * x can be used to replace the body

6 0
3 years ago
Each webpage is assigned a(n) ______, an address that identifies the location of the page on the Internet.
timofeeve [1]

Answer:

The answer is "URL".

Explanation:

The term URL is also known as the "Uniform-Resource-Locator", which is also known as the address of the website or internet file. To access this file we use to type its address in the web browser by using the "http:// or https://" protocol. It contains the domain name, with several other basic data to guide a visitor to a certain internet website called a web page.

5 0
3 years ago
Other questions:
  • based on the transcript, what did broadcasting the story through the medium of radio allow welles to do?
    8·1 answer
  • In tableData the column variable of the first column should be RowerNames, the second RowingEvent, the third RowerWeight and the
    6·1 answer
  • Using an LCD projector to show an online video to a group of people is an example of:
    14·1 answer
  • What is the advantage of postfix notation?
    6·1 answer
  • What are the differences betweenCONS, LIST, and APPEND?
    5·1 answer
  • What is this car first to awnser is the brianliest
    5·2 answers
  • 80billion +2 TRILLION . PLEASE AWNSER
    7·1 answer
  • 1. Our Systems are Microsoft Dynamics OLTP, we move data from the Source Systems through Staging to a Data Warehouse. Our report
    12·1 answer
  • Anyone on ps4 willing to trade me a fennec or McClaren on rocket league?
    6·2 answers
  • Python code for converting between minutes to seconds. For example 5 minutes= 300 seconds. please help me with this
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!