linear search (an algorithm for finding a single value in an array)

PHOTO EMBED

Sun Apr 11 2021 00:22:56 GMT+0000 (Coordinated Universal Time)

Saved by @Tadu21 #java

/*
*  Unit 6 - Lesson 2 - Algorithms - Searching
*/
import java.io.*;
import static java.lang.System.*;

import java.util.Scanner;
import java.lang.Math;


class U6_L2_template{

  
     public static void main (String str[]) throws IOException {
          Scanner scan = new Scanner (System.in);
          
          double list [] =  {2.3 , 4.7 , 5.25 , 9.5 , 2.0 , 1.2 , 7.129 , 5.4 , 9.5 };
          
          System.out.println( "What are you looking for? ");
          double look = scan.nextDouble();
          
          //search for value in the array, print -1 if not found
          
          int where = -1;
          
          for (int i = 0; i < list.length; i++) {
            if (list[i] == look){
              where = i; 
              break;
            }
          }
          
          if (where == -1){
            System.out.println("Not Found");
          } else {
            System.out.println("Value at: " + where);
          }

     }

}



content_copyCOPY