Sunday, February 24, 2013

Test Seq Search



import java.util.Scanner;
public class testSeqSearch {

    static Scanner console = new Scanner(System.in);
    static final int Array_Size=10;
   
    public static void main(String[] args) {
       
        int[] intList = new int[Array_Size];
        int number;
        int index;
       
        System.out.println("L 4:Enter "+Array_Size+" integers.");
       
        for(index=0; index<Array_Size; index++)
            intList[index]=console.nextInt();
       
        System.out.println();
       
        System.out.println("L 8:Enter the number to be searched:");
        number=console.nextInt();
        System.out.println();
       
        index = seqSearch(intList, Array_Size, number);
       
        if (index !=-1)
            System.out.println("L 13:"+number+"is found at position "
                    +index);
       
        else
            System.out.println("L 15:"+number+"is not in the list.");
       

    }
   
    public static int seqSearch(int[] list, int listLength, int searchItem){
       
        int loc;
       
        for(loc=0; loc<listLength; loc++)
            if(list[loc]== searchItem)
                return loc;
       
        return -1;
       
       
       
       
    }

}

No comments:

Post a Comment