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 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 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 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.
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)
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT