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

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...
(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.
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)
Write a MIPS assembly program that prompts the user for some number of cents (integer) and...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and read the user input. Then translate that number of into a number of quarters, dimes, nickels and pennies (all integers) equal to that amount and outputs the result. The output should adequately tell the user what is being output (not just the numeric results).
Write a MIPS assembly program that prompts the user for some number of cents (integer) and...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and read the user input. Then translate that number of into a number of quarters, dimes, nickels and pennies (all integers) equal to that amount and outputs the result. The output should adequately tell the user what is being output (not just the numeric results). (Make sure you use comments next to each line to describe what actions you are taking in your code. )...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT