Question

package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment {        /**        * valRange returns...

package exampleassignment;

import java.util.Arrays;

import stdlib.*;

public class assignment {

       /**

       * valRange returns the difference between the maximum and minimum values in the

       * array; Max-Min. Precondition: the array is nonempty. Your solution must go

       * through the array at most once.

       *

       * Here are some examples (using "==" informally):

       *

       * <pre>

       *    0 == valRange (new double[] { -7 })

       *    10 == valRange (new double[] { 1, 7, 8, 11 })

       *    10 == valRange (new double[] { 11, 7, 8, 1 })

       *    18 == valRange (new double[] { 1, -4, -7, 7, 8, 11 })

       *    24 == valRange (new double[] { -13, -4, -7, 7, 8, 11 })

       *

       * you should replace the line of code

       * labeled TODO with code that achieves the above specification

       * </pre>

       */

       public static double valRange(double[] list) {

             return -1; // TODO 1: fix this code

       }

       /**

       * 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 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) {

             return -2; // TODO 2: fix this

       }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Arrays;

public class Assignment {
   public static double valRange(double[] list)
   {
       // if only one elementt in list
       if (list.length <= 1) {
           return 0;
       }
       else
       {
           // consider 1 element as both max and min
           double min = list[0];
           double max = list[0];

           for (int i = 1; i < list.length ; ++i )
           {
               // if new max found update max
               if(list[i] > max)
               {
                   max = list[i];
               }
               // if new min found update min
               else if (list[i] < min)
               {
                   min = list[i];
               }
           }
          
           return (max - min);
       }
   }

   public static int posOfLargestElementLtOeT(double limit, double[] list)
   {
       // consider initial index is -1
       int lastIndex = -1;
       for (int i = 0; i < list.length ; ++i)
       {
           // if less number found then update index
           if(list[i] < limit)
           {
               lastIndex = i;
           }
       }
       // return index
       return lastIndex;
   }

   public static void main(String[] args)
   {
       System.out.println("Value of valRange");
       System.out.println(valRange(new double[] { -7 }));
       System.out.println(valRange(new double[] { 1, 7, 8, 11 }));
       System.out.println(valRange(new double[] { 11, 7, 8, 1 }));
       System.out.println(valRange(new double[] { 1, -4, -7, 7, 8, 11 }));
       System.out.println(valRange(new double[] { -13, -4, -7, 7, 8, 11 }));

       System.out.println("\nValue of posOfLargestElementLtOeT");
       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 }));
   }
}

11 A en TUE ULL U -/Desktop/Assignment.java - Sublime Text (UNREGISTERED) Preferences Help Tools Find View File Project Goto11 A en TUE ULL U -/Desktop/Assignment.java - Sublime Text (UNREGISTERED) Project Preferences Help Edit File Selection Find V

Add a comment
Know the answer?
Add Answer to:
package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment {        /**        * valRange returns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT