Question

In: Computer Science

Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers....

Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers.

Randomly pick a number between one and ten and then sequentially search the array for that number.

Once the number is found, place that number at the top of the list.

Print the list. Perform #3 thru #5 ten times.

Solutions

Expert Solution

SOURCE CODE:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
           Scanner sc = new Scanner(System.in);
           int arr[] = {1,2,3,4,5,6,7,8,9,10}; //array that stores 1-10 numbers
           int t,i,j,r,search_element,iterations;
           System.out.println("Intial Array: ");
           for(i = 0; i < 10; i++)
           {
               System.out.print(arr[i]+" "); //printing array values
           }
           Random rn = new Random();
           for (i=(arr.length-1); i> 0; i--)
            {
                r=rn.nextInt(i + 1); //selecting random integer
                t= arr[r]; //rando
                arr[r] = arr[i];
                arr[i] = t;
            }
            System.out.print("\nShuffled array: ");
            System.out.println();
            for(i = 0; i < 10; i++)
           {
               System.out.print(arr[i]+" "); //printing array values
           }
           System.out.print("\nEnter number of iterations(3 or 5):");
           iterations=sc.nextInt();
           ArrayList<Integer> Arrlist = new ArrayList<Integer>(iterations); // if 5 iterationsn then set list size 5
           i=iterations; // 5 or 3 iteration
           while(i>0)
           {
                search_element=arr[rn.nextInt(10)]; //selecting random search_element in the array
                System.out.println("Iteration :-"+(iterations-i));
                System.out.println("Search element is:"+ search_element);
                for(j=0;j<arr.length;j++)
                {
                    if(arr[j]==search_element)
                    {
                        System.out.println("Found element :"+search_element+" At index: "+j);
                        Arrlist.add(search_element);
                        break;
                    }
                }
                System.out.println("Array list is: "+Arrlist);
                i--;
                System.out.println();
           }
    }
}

CODE SCREENSHOT:

OUTPUT-1:

OUTPUT-2:

OUTPUT-3:


Related Solutions

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...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
Write a program in Easy68K: a) Define an array of numbers in the memory.
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array. 
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 ≤...
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,
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried...
In C an array Numbers is declared float Numbers[10][10]. Due to a bug, the program tried to reference Numbers[11][-13]. Which element of Numbers will actually be accessed?
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT