Respuesta :
Answer:
Explanation:
import java.util.Random;
public class RandomGen {
private static Random rand = new Random();
public static void main(String args[]) {
int[] randomEight = new int[8];
for(int i=0; i<8; i++) {
/*
* Get a random number between 50 and 100
* */
randomEight[i] = rand.nextInt(51) + 50;
}
int[] extremeValues = sortArray(randomEight);
System.out.println("The lowest element is " + extremeValues[0]);
System.out.println("The highest element is " + extremeValues[1]);
int sum = 0;
int oddCount = 0;
int evenCount = 0;
System.out.println("\nThe contents of the sorted array are : ");
for (int i = 0; i < randomEight.length; i++) {
System.out.print(randomEight[i] + " ");
if (randomEight[i]%2==0) {
evenCount++;
}
else {
oddCount++;
}
sum += randomEight[i];
}
System.out.println("\nEven numbers count : " + evenCount);
System.out.println("\nOdd numbers count : " + oddCount);
System.out.println("\nSum of all elements : " + sum);
}
public static int[] sortArray(int[] input) {
int extremeVal[] = new int[2];
extremeVal[0] = input[0]; /* Minimum */
extremeVal[1] = input[1]; /* Maximum */
/*
* Bubble Sort Algorithm
* */
int n = input.length;
for (int i=0; i<n-1; i++) {
for (int j=0; j<n-i-1; j++) {
if (input[j] > input[j+1]) {
int temp = input[j];
input[j] = input[j+1];
input[j+1] = temp;
}
}
/* Finding minimum */
if (extremeVal[0] > input[i]) {
extremeVal[0] = input[i];
}
/* Finding maximum */
if (extremeVal[1] < input[i]) {
extremeVal[1] = input[i];
}
}
return extremeVal;
}
}
The lowest element is 58
The highest element is 71
The content of the sorted array are:
51 52 58 59 59 63 71 100
Even numbers count :3
Odd numbers count :5
Sum of all element :513