Question

In: Computer Science

Write a program to produce an array of integer random numbers. Your program should find out...

Write a program to produce an array of integer random numbers. Your program should find out from the user how many numbers to store. It should then generate and store that many random integers (the random numbers must be between 1 and 999 inclusive). The program should then determine the smallest number, the largest number, and the average of all the numbers stored in the array. Finally, it should print out all the numbers on the screen, five numbers to a line with spaces in between. Once the contents of the array have been printed to screen, display the smallest number, largest number, and average determined previously. You should ensure that your program design in modular.

The Random class of Java library (java.util.Random) implements a random number generator. To generate random numbers, you construct an object of q the class Random, and then use the method nextInt(n) which returns a number between 0 (inclusive) and n (exclusive).

E.g.

import java.util.Random;

.....

Random generator = new Random();

.....

//generate a random number between 0 and 99 (inclusive)

int nextRand = generator.nextInt(100);

Alternatively use Math.random(), which returns a random value in the range 0.0 to 1.0 (then adjust to the correct range)

Solutions

Expert Solution

Hi,

Please find the java program below:

Program

package random;

import java.util.Random;
import java.util.Scanner;
///////////////////////////////////////
//ArrayDemo.java
// Program design in modular to fill array
// with random numbers 1 - 999 inclusive
///////////////////////////////////////
public class ArrayDemo {
   //array and size variables
   static int[] array;
   static int size=0;
   public static void main(String[] args) {
       //variables used
       Scanner console = new Scanner(System.in);
       int smallest;
       int largest;
       double average;
       try {
           //prompt the user for size
           System.out.print("How many numbers to store?=");
           size=Integer.parseInt(console.nextLine());
           array= new int[size];
           for(int i=0;i<size;i++) {
               array[i]=getRandonNumber();
           }
           smallest=getSmallest(array);
           largest=getLargest(array);
           average=getAverage(array);
           printContents(array);
           System.out.println("\nSmallest="+ smallest);
           System.out.println("\nLargest="+ largest);
           System.out.println("\nAverage="+ average);
       }catch(NumberFormatException nfe) {
           System.out.println("Error: Invalid Size.");
       }
   }

   //Method to get random number
   public static int getRandonNumber() {
       Random generator = new Random();
       return generator.nextInt(999)+ 1;
   }

   //Method to get smallest number
   public static int getSmallest(int[] a) {
       int small= Integer.MAX_VALUE;
       for(int i=0;i<a.length;i++) {
           if(a[i] < small)
               small=a[i];
       }
       return small;
   }

   //Method to get largest number
   public static int getLargest(int[] a) {
       int large= Integer.MIN_VALUE;
       for(int i=0;i<size;i++) {
           if(a[i] > large)
               large=a[i];
       }
       return large;
   }

   //Method to get average
   public static double getAverage(int[] a) {
       int sum= 0;
       for(int i=0;i<size;i++) {
           sum=sum+a[i];
       }
       return sum/size;
   }

   //Method to get average
   public static void printContents(int[] a) {
       int row= 5;
       System.out.println("Array Contents:");
       for(int i=0;i<size;i++) {
           if(i%row ==0) {
               System.out.println();
               System.out.print(a[i] + " ");
           }
           else
               System.out.print(a[i] + " ");
       }
   }
}


Screenshot


Output:

How many numbers to store?=20
Array Contents:

279 362 333 53 131
880 549 582 823 223
995 653 840 934 288
595 663 604 499 121
Smallest=53

Largest=995

Average=520.0

------------------------------------------
Hope this helps.
Let me know if you need more help with this.
Kindly, like the solution if you find it useful
Thanks.


Related Solutions

Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
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 a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
Write a program in Easy68K: a) Define an array of numbers in the memory.
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array. 
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language.
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT