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
Mingji is working to transfer photos from her digital camera to her computer. While
MAVERICK [17]

Answer:

digital camera with bluetooth

Explanation:

Bluetooth is defined as a wireless technology with the help of which data and information such any images, documents, files, etc can be send to other devices equipped with bluetooth technology over a short distance with the use of UHF radio waves.

Bluetooth technology help in exchanging data with out the use of a USB cable.

In the context, Minji is having a digital camera with bluetooth technology as she can transfer her photos from the camera to the computer with out the help of any USB chord.

4 0
3 years ago
Which address correctly represents one that is composed of two halves, one assigned to a network adapter manufacturer, and the o
Vlad1618 [11]
 Media access control address
5 0
3 years ago
Choosing a per_formatted presentation that already has a design and the slots is called choosing What​
Aloiza [94]
A template is a predesigned presentation you can use to create a new slide show quickly. Templates often include custom formatting and designs, so they can save you a lot of time and effort when starting a new project.
8 0
2 years ago
How can a user begin to work with a new sheet in excel?
Hitman42 [59]
I hope I'm answering this correctly I'm not too smart about it so I hope that sounds right if you arewanting to start with a new sheet in excel do one of the following click the new sheet button on the screen. now if you want to insert a new worksheet in front of its system worksheet and then click Home>insert> insert sheet. also Excel gives you three worksheets in a workbook, you can add much more worksheets and of course other types of sheets examples are micro sheet or dialog sheet. then you can rename them or delete them. so you can give any work sheet any name you want. sheets are displayed by default but if you don't see them click options>adcanced> display for options for the worksheet> show sheet tabs.
3 0
3 years ago
A system is being developed to help pet owners locate lost pets. Which of the following best describes a system that uses crowds
Slav-nsk [51]

Crowdsourcing  is used by mobile application that allows users to report the location of a pet that appears to be lost.

<h3>What is a mobile application?</h3>

A mobile application is known to be a kind of computer app or program that has been made to handle or run itself on a mobile device e.g. phone,  etc.

Conclusively, the option that best describes a system that uses crowdsourcing is the mobile application that allows users to report the location of a pet that appears to be lost and upload a photo that is made available to other users of the application and thus everyone can help to look for it.

Learn more about mobile application  from

brainly.com/question/9477543

#SPJ1

6 0
2 years ago
Other questions:
  • To arrange data in alphabetical order quickly, you can _____ the data.
    7·2 answers
  • Stealing passwords by using software code to run through various password schemes with numbers, symbols, capital letters, and ch
    6·1 answer
  • Can you make copies of classified documents provided you alert others?
    6·1 answer
  • Use the word bank to identify each description.
    6·1 answer
  • Which of the following is a true statement?
    12·2 answers
  • Computer software is regarded a bridge between the hardware and software.elaborate the statement outlining types and functions o
    10·1 answer
  • ________________, _______________ and ___________ are what you see when you open Excel
    15·1 answer
  • How does Shakespeare immediately introduce Tybalt as a menacing character?
    9·1 answer
  • The field names in a database are also known as__?
    12·2 answers
  • Zybook C++ 3.21 answer
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!