Question

In: Computer Science

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 49 26 24 26 70 42 2 22 20 87 42 73 33 21 87 37 84 66]

Random Array in Reverse Order: [66 84 37 87 21 33 73 42 73 42 87 20 22 2 42 70 26 24 26 49 67 17]

Max Value of the Array: 87

Min Value of the Array: 2

Solutions

Expert Solution

Solution :

Following is the Java code for the above problem with output :

import java.util.Random;
public class Program {
   public static void printArray(int arr[])
   {    System.out.println("\n");
       for(int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
   }
   }
   public static void printArrayReverse(int arr[])
    {   System.out.println("\n");
        for(int i = arr.length-1; i >=0; i--) {
            System.out.print(arr[i]+" ");
    }
    }
   public static int searchMax(int arr[])
    {   
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
         if (arr[i] > max)
            max = arr[i];
      }
      return max;
    }
     public static int searchMin(int arr[])
    {
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
         if (arr[i] < min)
            min = arr[i];
      }
      return min;
    }
   public static void main(String[] args) {
      Random rd = new Random(); // creating Random object
      int[] arr = new int[20];
      for (int i = 0; i < arr.length; i++) {
         arr[i] = rd.nextInt(100); // storing random integers in an array
      }
      printArray(arr);
      printArrayReverse(arr);
      System.out.println("\n");
      System.out.println(searchMax(arr));
      System.out.println("\n");
      System.out.println(searchMin(arr));
   }
}

Code demo :

Output :


Related Solutions

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...
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...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them to a text file on separate lines. Then the program should read the numbers from the file, calculate the average of the numbers, and display this to the screen.
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...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }.  For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this assignment,...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT