///Java: Write a program to get create, sort, print, and searches an array conta
ID: 3782147 • Letter: #
Question
///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.
//output sample.
///////////////////////////////////////////////////////////////////////////
Code I have come up with but does not seem to work.
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 = scan.nextInt; // 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.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
int[] 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.println("%-20d%-20d " array1[i], array2[i]);
} // close
System.out.print("Please enter number to search for:");
search = scan.nextInt;
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.");
} // close
else
{
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
Explanation / Answer
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.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++;
} // close
} // close
if(j == 0)
{
System.out.print("Search value: " +search+ " was not found");
}
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.