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 =...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
[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 program in C which randomly generates two integers X and Y between 10 and...
Write a program in C which randomly generates two integers X and Y between 10 and 50, and then creates a dynamic array which can hold as many numbers as there are between those two random numbers. Fill the array with numbers between X and Y and print it on screen. Do not forget to free the allocated memory locations whenever they are no longer needed. Example: Randomly generated numbers are: 43 23 Expected Output : 23 24 25 26...
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).
In this programming question, n integers ranging from 0 to n-1 are stored randomly in A,...
In this programming question, n integers ranging from 0 to n-1 are stored randomly in A, an array of size n. In the class Sort.java, you are supposed to implement the following two sorting algorithms: Insertion-sort and Heap-sort. You have to work directly on the given starter code. Download the starter code from the course web site. Read the starter code and make sure you understand how it works before attempting to modify it. In the given class Sort.java, Selection-sort...
Write a program that randomly generates 100 dates and store them into a vector. Use the...
Write a program that randomly generates 100 dates and store them into a vector. Use the Date.h provided . The dates generated must be within 1000 days after 1/1/2000. and Sort the 100 dates generated in ascending order. date.h #ifndef DATE_H_ #define DATE_H_ #include #include using namespace std; class Date {    friend ostream &operator<<( ostream &, const Date & ); private:    int day;    int month;    int year; static const int days[]; // array of days per...
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 c++ program that randomly generates 100 dates, store them into four vectors of date...
Write a c++ program that randomly generates 100 dates, store them into four vectors of date objects according to their seasons.. The dates generated must be within 1000 days after 1/1/2000.
Question 1:Write a program that randomly generates 100 dates and store them into a vector. Use...
Question 1:Write a program that randomly generates 100 dates and store them into a vector. Use the Date class provided . The dates generated must be within 1000 days after 1/1/2000. Question 2:Sort the 100 dates generated in Question 1 in ascending order. This is Class data.h.(Please don't change the data.h file) data.h #ifndef DATE_H_ #define DATE_H_ #include #include using namespace std; class Date {    friend ostream &operator<<( ostream &, const Date & ); private:    int day;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT