Define a class to accept values into an array of double data type of size 20. Accept a double value from user and search in the array using linear search method.
Advertisements
Question
Define a class to accept values into an array of double data type of size 20. Accept a double value from user and search in the array using linear search method. If value is found display message "Found" with its potiition where it is present in the array. Otherwise display message "not found".
Answer in Brief
Advertisements
Solution
import java.util.*;
class Lsearch
{
public static void main(String args[])
{
int c, n;
double array[], search;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array= new double[n];
System.out.println("Enter those " + n +" elements");
for (c = 0; c < n; c++)
array[c] = in.nextDouble();
System.out.println("Enter value to find");
search= in. nextDouble ();
for (c = 0; c < n; c++)
{
if (array[c] = search) /* Searching element is present*/
{
System.out.println(search + " is present at
location" + (c + 1) + ".");
break;
}
}
if (c = n) /* Element to search isn't present */
System.out.println(search + " isn't present in array.");
}
}shaalaa.com
Is there an error in this question or solution?
