Question

5. Write Java code for a loop that simultaneously computes both the maximum and minimum of...

5. Write Java code for a loop that simultaneously computes both the maximum and minimum of an array.

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

Java Code:

/* Java program that finds the maximum and minimum values present in the array */

class ArrayMaxMin
{
   //Main method
   public static void main(String args[])
   {
       //Array to hold integer values
       int[] values = {23,6,18,4,85,92,38,2,143,64};
      
       //Finding max and min values
       findMinMax(values);
   }
  
   //Method that simultaneously computes both the maximum and minimum of an array
   public static void findMinMax(int[] values)
   {
       int i, min, max;
      
       //Initially assume that starting value is the minimum and maximum element
       min = max = values[0];
      
       //Iterating over array elements
       for(i=0; i<values.length; i++)
       {
           //Updating max element
           if(values[i] > max)
               max = values[i];
          
           //Updating min element
           if(values[i] < min)
               min = values[i];
       }
      
       //Printing max and min values
       System.out.println("\n\n Minimum Number: " + min + "\n\n Maximum Number: " + max + " \n\n");
   }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Add a comment
Know the answer?
Add Answer to:
5. Write Java code for a loop that simultaneously computes both the maximum and minimum of...
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
  • (Java) Write a code segment that uses a for loop and an in a statement to...

    (Java) Write a code segment that uses a for loop and an in a statement to determine whether the value 13 is stored in a previously declared dimensional array named sportsScores. If the value 13 is stored in the array, then store the value true in a boolean flag variable named found. Otherwise, store false in found. The array sportsScores has 4 columns and 5 rows.

  • Intro to JAVA see below: ----------------------------------------------- Rewrite the algorithm for computing the maximum using an enhanced...

    Intro to JAVA see below: ----------------------------------------------- Rewrite the algorithm for computing the maximum using an enhanced for loop. Complete the following code: CODE: import java.util.ArrayList; public class ArrayListUtil {    /**       Computes the maximum value of a nonempty array list.       @param values a non-empty array list       @return the largest value in the array list    */    public static double maximum(ArrayList<Double> values)    {       // Compute the largest value, using an enhanced for loop       ....

  • Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the...

    Write a recursive algorithm in a pseudo code, Min-Max, for finding both the minimum and the maximum elements in an array A of n elements. Your algorithm calls itself only once within the algorithm and should return a pair (a, b) where a is the minimum element and b is the maximum element.

  • please show the code in java,thanks 5. Perfect Numbers Version 2: Write a program which computes...

    please show the code in java,thanks 5. Perfect Numbers Version 2: Write a program which computes perfect numbers according the following algorithm: for each prime, p: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 if 2P 1 s prime (2P (2 is perfect: print both p and (2P 1)(2) Note: You will need to use the Biglnteger clas for thist

  • ** Write a Java Code ** 1. Use for and switch statements Write a for loop...

    ** Write a Java Code ** 1. Use for and switch statements Write a for loop that counts from 1 to 5, and in the for loop body, use a switch statement to display a letter in the alphabet that corresponds to the number (i.e., 1 is A, 2 is B, etc.).

  • Java Write code for the problem: The user enters exactly 5 scores. Use a for loop...

    Java Write code for the problem: The user enters exactly 5 scores. Use a for loop to input the scores and count the number of scores that are greater or equal to 80. Print the result after all 5 scores have been entered.

  • java pseudocode and source code help? Write a program that uses an array of high temperatures...

    java pseudocode and source code help? Write a program that uses an array of high temperatures for your hometown from last week (Sunday - Saturday). Write methods to calculate and return the lowest high temperature (minimum) and a method to calculate and return the highest high temperature (maximum) in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array...

  • java programming!!! Write the code fragment that will do the following: • Ask the user how...

    java programming!!! Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instantiate an array of doubles with as many elements as the number entered by the user • Write one 'for' loop to fill the array with data from the user • Write a second 'for' loop to calculate the sum of all of the numbers in the array • Print the calculated sum Note: Write...

  • in java.... write a segment of code that declares an array variable, create an array object...

    in java.... write a segment of code that declares an array variable, create an array object that can hold 20 integers assigns the array object to the array variable, and then uses a loop to fill the array with the following integers... 0,10,20,30,40,50,60,70,80,90,100

  • Need to write a MIPS assembly program that finds the minimum and maximum and sum of...

    Need to write a MIPS assembly program that finds the minimum and maximum and sum of a stored array. It also finds the locations of the minimum and maximum. The interaction between the main program and the function is solely through the stack. The function stores $ra immediately after being called and restores $ra before returning to main. The main program reserves a static C like array (index starts from 0) of 10 elements and initializes it. The maximum should...

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