Question

assume that input is an int array. the array is already declared and initialized wnd every...

assume that input is an int array. the array is already declared and initialized wnd every element is assigned to an integer number. in addition assume that the array contains at least 1 element. write a code that contains one while loop abd no other loops to find both maximum and minimum numbers from input. then outout the maximum minus the minumum to the console window. (using java eclipse coding)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question, here is the code, where i have used only 1 while loop to iterate all the numbers in the array and find out the min and max number in the array. Once done, at the end have output the maximum and minimum numbers to the console window.

=====================================================================


public class MaxMin {

    public static void main(String[] args) {

        int array[] = {1, 2, 3, 4, 5, 6, 7, -352, 9, 26, 6, 6, -35, 7, 8, 535};

        int min = array[0];
        int max = array[0];
        int index = 0;
        while (index < array.length) {
            if (array[index] < min) min = array[index];
            if (array[index] > max) max = array[index];
            index+=1;
        }

        System.out.println("min = " + min);
        System.out.println("max = " + max);

    }
}

=====================================================================

Add a comment
Know the answer?
Add Answer to:
assume that input is an int array. the array is already declared and initialized wnd every...
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