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: Write a nested loop program creating an array list of three positions.  The...
Write a java program: Write a nested loop program creating an array list of three positions.  The first loop terminates with a sentinel value of which user is prompted for input to continue of quit.   Inside loop to enter in each position a prompted input of person's first name.  After the inside loop, edit the second array location making its value "Boston". Print the array's contents.
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 java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.
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 ≤...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT