Write a program that gets creates sorts prints searches an a

Write a program that gets creates, sorts, prints, searches an array of random numbers.

Declare an array of size 10. (an unsorted array)

Fill each element in this array with random numbers from 1 to 20.

Make a copy of this array. (to be sorted)

Using the java API, sort the copy.

Print a table showing both the unsorted and sorted arrays as shown in the
sample below.

This is what I have so far, my problem is that I need my program to say Search value: not found when a user punches in number not in the array.

import java.util.*;

public class blahblahblah
{
public static void main(String args[])
{
int[] unsortedArray = new int[10]; // Array of 10 numbers
int i, Min = 1, Max = 20;
int index1 = 0, index2 = 0;

for(i=0; i<10; i++)
unsortedArray[i] = Min + (int)(Math.random() * ((Max - Min) + 1));

int[] sortedArray = Arrays.copyOf(unsortedArray, unsortedArray.length); // Copy of the array

Arrays.sort(sortedArray);

System.out.printf(\"%-20s %-20s \ \ \", \"Unsorted Array\", \"Sorted Array\");

for(i=0; i<10; i++)
System.out.printf(\" %-20d %-20d \ \", unsortedArray[i], sortedArray[i]);

Scanner sc = new Scanner(System.in);

System.out.print(\"\ \ Enter a number to search for: \"); // Number to search for

int search = sc.nextInt();

for(int val : unsortedArray) // For-each loop for the unsorted array
{
       index1++;
if(val == search)
{
System.out.println(\"\ Search Value: \" + val + \" found at location: \" + index1 + \" in the unsorted array\");
   }
}

for(int val : sortedArray) // For-each loop for the sorted array
{
       index2++;
if(val == search)
{
System.out.println(\"\ Search Value: \" + val + \" found at location: \" + index2 + \" in the sorted array\");
}
}

System.out.println();
} // end main
} // end class

Solution

Hi, Please find my code.

import java.util.*;

public class ArrayOperation

{

   public static void main(String args[])

   {

       // creating Random Object

       Random random = new Random();

      

       int[] unsortedArray = new int[10]; // Array of 10 numbers

       int i;

       int index1 = 0, index2 = 0;

       for(i=0; i<10; i++)

           unsortedArray[i] = random.nextInt(20)+1; // random number 1-20

       int[] sortedArray = Arrays.copyOf(unsortedArray, unsortedArray.length); // Copy of the array

      

       Arrays.sort(sortedArray);

      

       System.out.printf(\"%-20s %-20s \ \ \", \"Unsorted Array\", \"Sorted Array\");

       for(i=0; i<10; i++)

           System.out.printf(\" %-20d %-20d \ \", unsortedArray[i], sortedArray[i]);

      

       Scanner sc = new Scanner(System.in);

       System.out.print(\"\ \ Enter a number to search for: \"); // Number to search for

       int search = sc.nextInt();

       for(int val : unsortedArray) // For-each loop for the unsorted array

       {

           index1++;

           if(val == search)

           {

               System.out.println(\"\ Search Value: \" + val + \" found at location: \" + index1 + \" in the unsorted array\");

               break;

           }

       }

       // if search value is not in array

       if(index1 == 10){

           System.out.println(search+\" : not found \");

       }

       else{

           for(int val : sortedArray) // For-each loop for the sorted array

           {

               index2++;

               if(val == search)

               {

                   System.out.println(\"\ Search Value: \" + val + \" found at location: \" + index2 + \" in the sorted array\");

                   break;

               }

           }

       }

      

       System.out.println();

   } // end main

} // end class

/*

Sample run:

Unsorted Array Sorted Array

11 4

5 5

9 6

13 9

12 11

19 12

6 13

17 17

4 19

19 19

Enter a number to search for: 3

3 : not found

*/

Write a program that gets creates, sorts, prints, searches an array of random numbers. Declare an array of size 10. (an unsorted array) Fill each element in thi
Write a program that gets creates, sorts, prints, searches an array of random numbers. Declare an array of size 10. (an unsorted array) Fill each element in thi
Write a program that gets creates, sorts, prints, searches an array of random numbers. Declare an array of size 10. (an unsorted array) Fill each element in thi
Write a program that gets creates, sorts, prints, searches an array of random numbers. Declare an array of size 10. (an unsorted array) Fill each element in thi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site