Question

In: Computer Science

Java Programming Create a program that prompts the user for an integer number and searches for...

Java Programming

Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array?

Your program should print the number of comparisons required to find the number or determine that the number does not exist.

Try finding the first and last numbers stored in the array.

Run your program several times before computing the average.

Solutions

Expert Solution

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {//linear search is implemented here
   int n,c=0,flag=0;
   int a[]={1,2,3,4,5,6,7,8,9,10};//array of 10 elements
   Scanner inp=new Scanner(System.in);
       System.out.println("Enter a integer number");
       n=inp.nextInt();//read integer from user
       for(int i=0;i<10;i++){
       c++;//increment c value for each Comparison
       if(a[i]==n){
       flag=1;//set flag to 1 if number n is found in array
       break;
       }
       }
       if(flag==1)
       System.out.println("Comparisons:"+c);//print number of Comparisons
       else
       System.out.println("Number not found");
   }
}

Screenshots:

The screenshots are attached below for reference.

Please follow them for output.

Average number of comparisons:

The number of comparisons are as shown above in the outputs attached.

For search an element located at index 0, 1 comparison is needed.

For search an element located at index 9, 10 comparison is needed.

For search an element located at index 4, 5 comparison is needed.

For search an element located at index 8, 9 comparison is needed.

The average number of comparisons needed is (1+2+3+4+5+6+7+8+9+10)/10.

The result is floor(5.5) which is 5.


Related Solutions

Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
python programming. Write a program that prompts the user to enter an integer from 0 to...
python programming. Write a program that prompts the user to enter an integer from 0 to 9. The program will check if the input is a positive integer. It displays the correct answer and shows the guess is correct or not. The demos are shown as following.(Hint:1. Use a random math function 2. Use str.isdigit() to check the input)
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
Java Program that prompts the user and reads in an integer between 0 to 50, inclusively....
Java Program that prompts the user and reads in an integer between 0 to 50, inclusively. If the entered value is outside the range, program’s output will display the following message: “Error: the entered number is out of range”, and continually re-prompts the user to enter the integer again until the user enters a valid integer. • Once a valid integer has been entered, program prompts the user and reads in a second integer between -5 to 20, inclusively. If...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is...
Python A program which prompts the user to enter an number which is a integer n...
Python A program which prompts the user to enter an number which is a integer n and creates a tuple where the values are all numbers between 1 and n (both inclusive). Note: you can assume that the integer will always be > 1. Input Result 3 Enter an integer: 3 (1, 2, 3)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT