Question

In: Computer Science

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 in the array, and the second smallest element of the array as well as its index in the array. For example, if the elements of the array were 5 2 4 1 3 then your program should output

The smallest element is 1 and its index is 3

The second smallest element is 2 and its index is 1

must be written in Java

Solutions

Expert Solution

/**
 * @fileName RandomNumber.java
 * @author 
 * @since 21/3/17
 */

import java.util.Arrays;
import java.util.Random;

public class RandomNumber {

    /**
     * @param arr
     * @param max
     * @param min
     */
    public static void generateRandomNumber(int[] arr, int max, int min) {

        Random rand = new Random();

        for (int i = 0; i < arr.length; i++) {
            arr[i] = rand.nextInt((max - min) + 1) + min;
        }
    }

    /**
     * @param arr
     * @return
     */
    public static double average(int[] arr) {
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }

        double avg = sum / arr.length;
        return avg;
    }

    /**
     * @param arr
     * @return
     */
    public static int getSmallestNumber(int[] arr) {
        int smallest = arr[0];

        for (int i = 1; i < arr.length; i++) {

            if (arr[i] < smallest) {
                smallest = arr[i];
            }

        }
        return smallest;
    }

    /**
     * @param arr
     * @return
     */
    public static int getSecondSmallestNumber(int[] arr) {

        int firstSmallest;
        int secondSmallest;

        firstSmallest = secondSmallest = Integer.MAX_VALUE;
        for (int i = 0; i < arr.length; i++) {
            /* If current element is smaller than first
              then update both first and second */
            if (arr[i] < firstSmallest) {
                secondSmallest = firstSmallest;
                firstSmallest = arr[i];
            }

            /* If arr[i] is in between first and second
               then update second  */
            else if (arr[i] < secondSmallest && arr[i] != firstSmallest)
                secondSmallest = arr[i];
        }
        return secondSmallest;
    }

    /**
     * @param arr
     * @param number
     * @return
     */
    public static int getIndex(int[] arr, int number) {
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number) {
                index = i;
                break;
            }
        }
        return index;

    }

    public static void main(String[] args) {
//        int arr[] = {5,2, 4, 1,3};
        int arr[] = new int[20];

        int max = 100, min = -100;
        generateRandomNumber(arr, max, min);
        System.out.println("Array : " + Arrays.toString(arr));
        System.out.println("Average :" + average(arr));
        int smallest = getSmallestNumber(arr);
        int secondSmallest = getSecondSmallestNumber(arr);
        System.out.println("The smallest element is " + smallest + " and its index is " + getIndex(arr, smallest));
        System.out.println("The second smallest element is " + secondSmallest + " and its index is " + getIndex(arr, secondSmallest));


    }
}

output:


Related Solutions

Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Problem Description: In C write a program that assigns random integers between 1 and 20 to...
Problem Description: In C write a program that assigns random integers between 1 and 20 to a 5 x 5 two-dimensional array then displays the array with average of each row in a table format and the total of all elements in the array. Also calculate the total of each diagonal, top left to bottom right and top right to bottom left. Display the largest of the diagonal’s totals. Example of the array: 6          10        3          19        20        1          17       ...
Write a program that uses a custom function to generate a specified number of random integers...
Write a program that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also report how many...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT