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
Consider the classes below:
jeka57 [31]

Answer:

Hi!

The correct answer is d. Neither (a) nor (b) is true.

Explanation:

First, you have to know what the scripts intend to do, and they are counting from 1 to 10 in both cases.

Table of results of class TestA at end of each iteration:

[j = 0,  counter =  1]

[j = 10,  counter =  2]

[j = 20,  counter =  3]

[j = 30,  counter =  4]

[j = 40,  counter =  5]

[j = 50,  counter =  6]

[j = 60,  counter =  7]

[j = 70,  counter =  8]

[j = 80,  counter =  9]

[j = 90,  counter =  10]

Table of results of class TestB at end of each iteration:

[j = 10,  counter =  1]

[j = 9,  counter =  2]

[j = 8,  counter =  3]

[j = 7,  counter =  4]

[j = 6,  counter =  5]

[j = 5,  counter =  6]

[j = 4,  counter =  7]

[j = 3,  counter =  8]

[j = 2,  counter =  9]

[j = 1,  counter =  10]

That's why a) and b) are false.

Maybe you want to know why using pre increments on class TestB doesn't affect the loop. The reason is that when enters in a loop, first checks the test condition and takes the decision if true or false, and last executes the incrementation step.

7 0
3 years ago
Parts are described by partnum, color, and price. Parts can be subassemblies as well and this means that they contain other part
Degger [83]

Answer:

The answer is attached below

Explanation:

8 0
3 years ago
The while loop is a pre-test loop? TRUE OR FALSE
mihalych1998 [28]
True

Hope I helped! 

Let me know if you need anything else!

~ Zoe
5 0
4 years ago
Read 2 more answers
Write a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. Fo
RoseWind [281]

Answer:

The solution code is written in Python

  1. def max(a, b):
  2.    if(a > b):
  3.        return a  
  4.    else:
  5.        return b
  6. num1 = int(input("Please input the first number: "))
  7. num2 = int(input("Please input the second number: "))
  8. print(max(num1, num2))

Explanation:

Firstly create a function max that accepts two input, a and b, as required by question (Line 1). In the function, create if statement to check if a greater than b return a and vice versa (Line 2 -5).

In the main program (Line 7 - 10), prompt user to input two numbers (Line 7 - 8). At last call the function by passing num1 and num2 as arguments and print the result (Line 10).

6 0
3 years ago
Write a recursive, int -valued function, len, that accepts a string and returns the number of characters in the string .
vodka [1.7K]
Without knowing the language, it's pretty imposible to write it.  Once you start thinking recursively, it's not a difficult problem.

While you're recursing, pass the string without the first character.
The base case would be a null string which would return 0.
The unwind would just add one and return that.
7 0
3 years ago
Other questions:
  • A small company has developed a specialized software product that it configures specially for each customer. New customers usual
    14·1 answer
  • Is it necessary that every autonomous system use the same intra-AS routing algorithm? Why or why not?
    10·1 answer
  • As Alexa types a message, Outlook autosaves the message at various points. In which folder is the message located if Alexa wants
    15·2 answers
  • Exit windows entirely and turns off the computers power
    8·1 answer
  • ____ is scientifically seeking and discovering facts.
    9·1 answer
  • A device that connects to a network without the use of cables is said to be?​
    13·1 answer
  • Provide five signs of cyber bullying​
    15·2 answers
  • What is the best way of farming exotics in destiny?
    12·2 answers
  • How do i automate survey monkey results to my email
    11·1 answer
  • Which of the following responses is the best example of a problem that can be solved using a computer program?
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!