Java Write a program to get create sort print and searches a
///Java: Write a program to get create, sort, print, and searches an array containing random numbers 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.
? Enter a number to search for.
? Utilizing a “for each loop”:
? Search for the search string in both the unsorted and sorted arrays.
o Output the search value and location found as shown in the example or output the search value was not found
//Sample output
////out put that I get
It\'s not supposed to look like that and Im unsure where I went wrong. Also the part where Im suppose to get the result for inputting a search number not included in the array is wrong as well am i\'m puzzled.
////Code
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class ArrayWork
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int[] array1 = new int[10]; // array size of 10
int[] array2 = new int[array1.length]; // copy of array1
int search; // user input number to search for
int ele1 = 0; // element in array1
int ele2 = 0; // element in array2
int temp = 0; // temporary holder
for (int i = 0; i {
array1[i] = rand.nextInt(20)+1; // random number of 1-20
} // end loop
array2 = Arrays.copyOf(array1, array1.length); // copy of array1 into array2
for (int i=0; i < array2.length; i++) // sorting array2
{
for (int j = i + 1; j < array2.length; j++)
{
if (array2[i] > array2[j])
{
temp = array2[i];
array2[i] = array2[j];
array2[j] = temp;
}
}
} // close
System.out.printf(\"%-20s%-20s \ \ \", \"Unsorted Array\", \"Sorted Array\"); // printing heading for both arrays
for (int i = 0; i < 10; i++)
{
System.out.printf(\"%-20d%-20d \ \", array1[i],array2[i]);
} // close
System.out.print(\"Please enter number to search for:\");
search = scan.nextInt();
int j = 0;
for (int i : array1) // for-each loop
{
ele1++;
if (i == search) // if the searched number does appear in the unsorted array
{ // prints the location of where it was found
System.out.println(\"Search value:\" +i+ \"found at location \" +ele1+ \" in the unsorted array.\");
j++;
} // close
if (j==0)
{
System.out.print(\"Search value:\" +i+ \"was not found\");
}
} // close
for (int i : array2) // for-each loop
{
ele2++;
if (i == search) // if the number is found in the sorted array
{
System.out.println(\"Search value:\" +i+ \"found at location \" +ele2+ \" in the sorted array.\");
} // close
} // close
} // end main
} // end class
Solution
//Modifications are highlighted in bold letters
//ArrayWork.java
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class ArrayWork
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int[] array1 = new int[10]; // array size of 10
int[] array2 = new int[array1.length]; // copy of array1
int search; // user input number to search for
int ele1 = 0; // element in array1
int ele2 = 0; // element in array2
int temp = 0; // temporary holder
//Note:
//Set a boolean value found to false
boolean found=false;
for (int i = 0; i<array1.length; i++) // fill array 1 with random numbers from 1 to 20
{
array1[i] = rand.nextInt(20)+1; // random number of 1-20
} // end loop
array2 = Arrays.copyOf(array1, array1.length); // copy of array1 into array2
for (int i=0; i < array2.length; i++) // sorting array2
{
for (int j = i + 1; j < array2.length; j++)
{
if (array2[i] > array2[j])
{
temp = array2[i];
array2[i] = array2[j];
array2[j] = temp;
}
}
} // close
System.out.printf(\"%-20s%-20s \ \ \", \"Unsorted Array\", \"Sorted Array\"); // printing heading for both arrays
for (int i = 0; i < 10; i++)
{
System.out.printf(\"%-20d%-20d \ \", array1[i],array2[i]);
} // close
System.out.print(\"Please enter number to search for:\");
search = scan.nextInt();
int j = 0;
for (int i : array1) // for-each loop
{
ele1++;
if (i == search) // if the searched number does appear in the unsorted array
{
// prints the location of where it was found
System.out.println(\"Search value: \" +i+ \" found at location \" +ele1+ \" in the unsorted array.\");
j++;
//Set found true
found=true;
} // close
} // close
for (int i : array2) // for-each loop
{
ele2++;
if (i == search) // if the number is found in the sorted array
{
//Set found true
found=true;
System.out.println(\"Search value: \" +i+ \" found at location \" +ele2+ \" in the sorted array.\");
} // close
} // close
//Note :
//Check if found boolean is false then print a message
//Theat search value is not found
if(!found)
{
System.out.println(\"Search value: \"+search+\" not found.\");
}
} // end main
} // end class
Sample output:
Unsorted Array Sorted Array
20 2
2 4
17 5
16 6
4 8
14 14
8 14
14 16
6 17
5 20
Please enter number to search for:20
Search value: 20 found at location 1 in the unsorted array.
Search value: 20 found at location 10 in the sorted array.
sample run2:
Unsorted Array Sorted Array
3 1
12 3
9 5
5 5
1 7
7 7
10 9
7 10
5 12
14 14
Please enter number to search for:85
Search value: 85 not found.



