Question

In: Computer Science

In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...

In Java Please:

* posOfLargestElementLtOeT returns the position of the largest element in the array that is

* less than or equal to the limit parameter

* if all values are greater than theVal, return -1;

*

* Precondition: the array is nonempty and all elements are unique.

* Your solution must go through the array exactly once.

*

* <pre>

* 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0

* 5 == posOfLargestElementLtOeT(3, new double[] { 11, -4, -7, 7, 8, 1 }), // value:1 is in pos 5

* -1 == posOfLargestElementLtOeT(-7, new double[] { 1, -4, -5, 7, 8, 11 }), // all elements are > -7

*

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

* labeled TODO with code that achieves the above specification

* </pre>

*/

public static int posOfLargestElementLtOeT( double limit, double[] list)

// Solve here

Solutions

Expert Solution

public class PosLargest {

    /**
     * posOfLargestElementLtOeT returns the position of the largest element in the array that is
     * <p>
     * less than or equal to the limit parameter
     * <p>
     * if all values are greater than theVal, return -1;
     * <p>
     * <p>
     * <p>
     * Precondition: the array is nonempty and all elements are unique.
     * <p>
     * Your solution must go through the array exactly once.
     *
     *
     *
     * <pre>
     *
     * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0
     *
     * 5 == posOfLargestElementLtOeT(3, new double[] { 11, -4, -7, 7, 8, 1 }), // value:1 is in pos 5
     *
     * -1 == posOfLargestElementLtOeT(-7, new double[] { 1, -4, -5, 7, 8, 11 }), // all elements are > -7
     *
     *
     *
     * The code below is a stub version, you should replace the line of code
     *
     * labeled TODO with code that achieves the above specification
     *
     * </pre>
     */
    public static int posOfLargestElementLtOeT(double limit, double[] list) {
        int maxIndex = -1;
        for (int i = 0; i < list.length; i++) {
            if (list[i] <= limit) {
                if (maxIndex == -1 || list[i] > list[maxIndex]) {
                    maxIndex = i;
                }
            }
        }
        return maxIndex;
    }

    public static void main(String[] args) {
        System.out.println(posOfLargestElementLtOeT(3, new double[]{-7}));
        System.out.println(posOfLargestElementLtOeT(3, new double[]{11, -4, -7, 7, 8, 1}));
        System.out.println(posOfLargestElementLtOeT(-7, new double[]{1, -4, -5, 7, 8, 11}));
    }
}


Related Solutions

JAVA /**    * posOfLargestElementLtOeT returns the position of the largest element in the    *...
JAVA /**    * posOfLargestElementLtOeT returns the position of the largest element in the    * array that is less than or equal to the limit parameter if all values are    * greater than limit, return -1;    *    * Precondition: the array is nonempty and all elements are unique. Your    * solution must go through the array exactly once.    *    * <pre>    * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
Question 6 Which of the following for loops will find the largest element in the array...
Question 6 Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values? Question 6 options: largest = None for i in range(len(numbers)): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(numbers): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(len(numbers)): if largest is None or numbers[i]...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
JAVA /**    * dataAtPosition returns the int data at the described position.    * if...
JAVA /**    * dataAtPosition returns the int data at the described position.    * if the position is an invalid position it throws an Exception.    *    * Examples:    * pos : 0 and LinkedList : 2 --> 3 --> null ==> return 2    * pos : 1 and LinkedList : 1 --> -3 --> null ==> return -3    * pos : 2 and LinkedList : -2 --> 3 --> -2 --> null ==> return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT