Question: All code should be in Java. /** * posOfElementClosestTo returns the position of the element in the array that is * closest to the theVal

All code should be in Java.

/**

* posOfElementClosestTo returns the position of the element in the array that is

* closest to the theVal parameter, in the absolute value sense.

* In the event of a tie, return the position of the first value found

* (starting from 0)

*

* You can assume the array is nonempty and all values are unique. Your solution

* must go through the array exactly once and use Math.abs. Here are some examples (using "==" informally):

*

*

* 0 == posOfElementClosestTo(3, new double[] { -7 }) // -7 is closest to 3, it's in pos 0

* 5 == posOfElementClosestTo(3, new double[] { 11, -4, -7, 7, 8, 1 }), // 1 is closest to 3, it's in pos 5

* 2 == posOfElementClosestTo(-6, new double[] { 1, -4, -7, 7, 8, 11 }), // -7 is closest to -6, it's in pos 2

*

* The code below is a stub version, you should replace the line of code

* labeled TODO with code that achieves the above specification

*

*/

public static int posOfElementClosestTo(double theVal, double[] list) {

//TODO 3: fix this

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!