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
kykrilka [37]
3 years ago
7

Implement a java program to find the smallest distance between two neighbouring numbers in an array.

Computers and Technology
1 answer:
Alex17521 [72]3 years ago
6 0
<h2>Answer:</h2>

Here is the JAVA program to find smallest distance between 2 neighboring numbers in an array.

import java.lang.Math; // for importing Math class functions

import java.util.Scanner; // for importing Scanner class

public class CalSmallestDistance // class to calculate smallest distance

{public static void main(String[] args) {     // to enter java program

    Scanner s = new Scanner(System.in);  //creating scanner object

    int size; // size of the array

  System.out.print("Enter size of the array:"); //prompts to enter array size

       size = s.nextInt(); // reads input

       int arr[] = new int[size];  // array named arr[]

      //line below prompts to enter elements in the array             System.out.println("Enter numbers in the array:");

       for(int j = 0; j < size; j++)        //loops through the array

           {arr[j] = s.nextInt(); }     //reads the input elements

      int smallest_distance = Math.abs(arr[0]-arr[1]);  

       int position= 0; //index of the array

       for(int i=1; i<arr.length-1; i++){

           int distance= Math.abs(arr[i]-arr[i+1]);

           if(distance< smallest_distance){

           smallest_distance= distance;

           position = i;            }        }

  System.out.println("Smallest distance is :"+smallest_distance);

System.out.println("The numbers are  :"+arr[position]+ " and " +arr[position+1]);      } }

<h2>Explanation:</h2>

I have stated the minor explanation of some basic lines of code as comments in the code given above.

Now i will give the detailed explanation about the working of the main part of the code.

Lets start from here

      int smallest_distance = Math.abs(arr[0]-arr[1]);  

In this statement the array element at 0 index (1st position) and the array element at 1 index (2nd position) of the array are subtracted.

Then i used the math.abs() method here  which gives absolute value

Lets say the distance between 3 and 5 is -2 as 3-5 equals to -2. But the math.abs() method will return 2 instead of -2.

Now the subtraction of two array elements and absolute value (if subtraction result is negative) will be assigned to variable smallest_distance.

       for(int i=1; i<arr.length-1; i++)

This is for loop. Variable i is positioned to the 1 index of the array (arr) (it is pointing to the second element of the array). It will move through the array until the end of the array is reached i.e. the loop will continue till value of i remains less than the length of the array.

Now at the start of the loop body the following statement is encountered

           int distance= Math.abs(arr[i]-arr[i+1]);

This subtracts the array element at i th position and array element at i th +1 position (means one position ahead of array element at i th position). In simple words two neighboring numbers in an array are being subtracted and Math.abs() method is used to give absolute value. The result of this operation is assigned to distance variable.

           if(distance< smallest_distance)

This if statement checks if the value in distance variable is smallest than the value of smallest_distance variable which was previously calculated before calculating the value for distance variable.

If this condition is true then the following statements are executed:

             smallest_distance= distance;

if distance value is less than value in smallest_distance, then the value of distance is assigned to smallest_distance.

this means the smallest_distance will keep on storing the smallest distance between two neighboring numbers.

Next the value of variable i that is pointing to the 1st index of the array is now assigned to the position variable.

                                         position = i;

It will keep assigning the value of i to position variable so at the end of the program we can get the positions of the two neighboring numbers that have the smallest distance between them.

Then the value of i is incremented and moves one place ahead in the array.

Then the 2nd iteration takes place and again checks if i pointer variable has reached the end of the array. If not the loop body will continue to execute in which the distance between the two neighboring numbers is calculated and shortest distance is stored in smallest_distance.

When i reaches the end of the array the loop will break and the smallest distance between two neighboring numbers in the array have been stored in the smallest_distance variable.

Finally the statement System.out.println("Smallest distance is :"+smallest_distance); displays the shortest distance and the statement System.out.println("The numbers are  :"+arr[position]+ " and " +arr[position+1]); displays the array index positions at which the two neighboring numbers have the smallest distance.

You might be interested in
By generating and delivering timely and relevant information supported by networks, _____ creates new opportunities for conducti
castortr0y [4]

Answer:

The correct option to the following question is option (B). e-business.

Explanation:

E-business is stands for the Electronic business.

E-business is the business which is conduct by the uses of the internet, Web, extranet or intranet, etc.

E-business is also known as the online business where online transactions take place.

E-business facilitates our customers that the selling and the buying of the goods between the consumers and the merchants.

7 0
3 years ago
Select the correct answer.
Softa [21]

Answer:

D. smart phone

explanation;

a. what the heck is a radio going to do?

b. an HD television? is his job watching movies?

d. he already has a form of communication he uses, he needs something that can to both things at the same time

c. a smart phone can do everything he needs to do, likely faster than a landline phone or laptop.

5 0
2 years ago
You can use a(n) ______________to store large amounts of customer data for a website.
BaLLatris [955]

You can use a(n) <u>database server; Databases, like MySQL</u><u> and PostgresSQL, are widely used to store data.</u>to store large amounts of customer data for a website.

<h3>What is the difference between POP3 and IMAP coursera?</h3>

POP3 can only download email to one local device. IMAP can only download email to one local device. POP3 is used for sending emails. IMAP can download emails onto multiple devices.

<h3>When would you use IMAP?</h3>

IMAP and POP are two methods to access email. IMAP is the recommended method when you need to check your emails from several different devices, such as a phone, laptop, and tablet.

To learn more about database server, refer

brainly.com/question/23752341

#SPJ4

5 0
1 year ago
Which option is the default when creating appointments and meetings in a user’s own calendar as it relates to Free/Busy informat
Vlad1618 [11]

Answer:

C !

Explanation:

7 0
3 years ago
An online supermarket wants to update its item rates during the sales season. They want to show the earlier rates too but with a
cupoosta [38]
Cheaper elements gives a strike through appearence
7 0
3 years ago
Read 2 more answers
Other questions:
  • Which pattern is produced by the following code? for (int i = 1; i &lt;= 6; i++) { for (int j = 6; j &gt;= 1; j--) System.out.pr
    6·1 answer
  • There are a multitude of items that Cyber Security professionals view as attack vectors but none are more prevalent and exploita
    13·1 answer
  • Can an iphone user see when an android user is typing
    10·2 answers
  • You're setting up some VMs to test an application you're considering making available to employees of the small company you work
    11·1 answer
  • How can you assess a website for currency?
    14·2 answers
  • How does abstraction help us write programs
    11·1 answer
  • QUESTION 10
    10·2 answers
  • a publication usually issued daily,weekly or other regular times that provides news,views,features and other information of publ
    8·1 answer
  • In a finite state machine, state transitions happen only: a. When the reset causes a clock pulse on the D outputs of the flip-fl
    11·1 answer
  • A. In I/O interation between OS and devices, describe how hybrid mechanism works?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!