Question

In: Computer Science

[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and...

  1. [15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and stores them into an array, where n is a parameter of the method. The method returns all the numbers above the average of the n random numbers. Use loops wherever possible.

For example, if the 10 random numbers are

44 61 98 45 45 17 63 24 9 95

The method should returns an array which contains 61 98 63 95

In the main method, make up an array size, call the method, and display the numbers above the average

Solutions

Expert Solution

Program:

import java.util.*;

public class Main
{
    public static ArrayList<Integer> generateRandom(int n) {
        int array[] = new int[n]; // Initializing array with the size of n
        // Array list to store the elements which are greater than average of array elements
        ArrayList<Integer> output_array = new ArrayList<Integer>();
        int sum = 0, average;
        // Loop iterates n number of times to generate random numbers
        for (int i=0; i<n; i++) {
            Random rand = new Random(); 
            // Generate random integers in range 0 to 100 and insert into array 
            array[i] = rand.nextInt(101);
            // sum of array elements
            sum += array[i];
        }
        // average of array elements
        average = sum/n;
        // Loop will find elements which are greater than average
        // and add it to ArrayList
        for (int i=0;i<n;i++) {
            if (array[i] > average) {
                output_array.add(array[i]);
            }
        }
        // Printing randomly generated array
        System.out.println("Random generated array : ");
        for (int i = 0; i < n; i++) 
            System.out.print(array[i] + " ");
        System.out.println();
        // Returns array with elements greater than average
        return(output_array);
    }
    
        public static void main(String[] args) {
            int n = 10; // Size of an array
            // Will have the array elements which are greater than average of array elements
            ArrayList<Integer> final_array = generateRandom(n);
                // Printing output
                System.out.println("Output Array : ");
                for (int i = 0; i < final_array.size(); i++) 
            System.out.print(final_array.get(i) + " ");
        }
}

Output:


Related Solutions

Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The method finds and returns an array which contains the positive numbers. For example, if the method receives an array with the following elements: 0 3 -1 2 5 1 -5 2 -2 0 the method should return an array with the following elements: 3 2 5 1 2 In the main method, randomly generate an array of 10 random integers between -5 and 5,...
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences of each with ascending order. input: line1:number of figures line2:number Sample Input 5 -3 100 -1 -2 -1 Sample Output -3 1 -2 1 -1 2 100 1
Write a java program that inserts 25 random integers from 0 to 100 in order into...
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Write a Python program which generates a random number between 0 and 24 inclusive and that...
Write a Python program which generates a random number between 0 and 24 inclusive and that print out: • the double of the random number if the random number is greater than 12, • the triple of the random number if it is equal to 12 • and the quadruple of the random number if it is less than 12
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write...
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write a program that finds the average value of the rows and the average value of the columns. Display the averages. 2. Create an array of randomly generated numbers in any range. Write a function that takes the array as an argument and returns an array that consists of only the even numbers in the original array. Use the function in a program. 3. Create...
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
Find the number of integers between 100 and 1000 that are
Find the number of integers between 100 and 1000 that are (i) divisible by 7  (ii) not divisible by 7      
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT