Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write a Java program that does the following: Declares an array o

ID: 674210 • Letter: I

Question

I'm trying to write a Java program that does the following:

Declares an array of size 10.

Fills each element of the array with random numbers 1 to 20.

Copies the array for sorting and sorts the copy using the java API.

Prints a table showing both the unsorted and sorted arrays.

Allows a user to search for a number.

Utilizes a "for-each loop" and searches for the search string in both the unsorted and sorted arrays.

Unsorted Array Sorted Array 2 4 6 8 10 12 14 14 15 12 15 14 4 14 10 Please enter number to search for: 6 Search Ualue 6 found at location: 1 in the unsorted array Search Ualue: 6 found at location 4 in the sorted array in the unsorted array

Explanation / Answer

package chegg;

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class ComputeN {

  

   public static void main(String[] args) {

       int[] unsorted = new int[20];

       int[] sorted = new int[20];

       Random generator = new Random();

      

       for(int i=0; i<20; i++) {

           unsorted[i] = generator.nextInt(20) + 1;

       }

      

       for(int i=0; i<20; i++) {

           sorted[i] = unsorted[i];

       }

       Arrays.sort(sorted);

      

       System.out.println("Unsorted Array Sorted Array");

       for(int i=0; i<20; i++) {

           System.out.println(unsorted[i]+" "+sorted[i]);

       }

      

       System.out.print(" Please enter a number to search for: ");

       Scanner sc = new Scanner(System.in);

       int num = sc.nextInt();

       int loc1 = -1;

       int loc2 = -1;

       for (int i = 0; i < 20; i++) {

           if(unsorted[i] == num) {

               loc1=i;

           }

           if(sorted[i] == num) {

               loc2=i;

           }

       }

      

       if(loc1==-1) {

           System.out.println(" Number not in array!!! ");

       } else {

           System.out.println(" Search value "+num+" is found at location "+(loc1+1)+" in the unsorted array");

           System.out.println("Search value "+num+" is found at location "+(loc2+1)+" in the sorted array");

       }

   }

}