Create an array of type int having a length of 16. Populate the array with rando
ID: 660553 • Letter: C
Question
Create an array of type int having a length of 16.
Populate the array with random integers that are in the interval zero to 42 (inclusive).
Print contents of the the array to the standard output stream. The numbers should be separated by two spaces and at most five numbers should be printed per line.
Enter into a loop that prompts the user to enter a number between 0 and 42, inclusive. Terminate the loop if -1 is entered. Use a "linear search" algorithm to find the number in the array printing either a "number found" or "number not found" message.
Keep track of the total number of searches performed and the number that were successful. Print the following message prior to exiting the program
X of Y searches (nn.n%) were successful.
Where X is the number of successful searches,
Y is the total number of searches, and nn.n
is the percentage of successful searches.
Explanation / Answer
import java.util.*;
class Tower{
public static void main(String []args){
int[] l = new int[];
Random rn = new Random();
for (int i = 0; i < 16; i++){
l[i] = rn.nextInt() % 43;
}
for (int i = 1; i < 17; i++){
System.out.print(l[i-1]+" ");
if (i % 4 == 0)
System.out.print(" ");
}
Scanner sc = new Scanner(System.in);
int query = 0;
int find = 0;
while (True){
int n = sc.nextInt();
if (n == -1)
break;
query++;
for (int i = 0; i < 16; i++){
if (l[i] == n){
System.out.println(n + " is Found Succesfully");
find++;
break;
}
if (i == 15)
System.out.println(n +" is not there is array");
}
}
System.out.println("Total number of Search is "+query+" Total query that are Succesfully found are "+find);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.