TestSeqSearch

TestSeqSearch - System.out.println(); //Line 10 index =...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.*; i public class TestSeqSearch { static Scanner console = new Scanner(System.in); static final int arraySize = 10; public static void main(String[] args) { int[] intList = new int[arraySize]; //Line 1 int number; //Line 2 int index; //Line 3 System.out.println("Line 4: Enter " + arraySize + " integers."); //Line 4 for (index = 0; index < arraySize; index++) //Line 5 intList[index] = console.nextInt(); //Line 6 System.out.println(); //Line 7 System.out.print("Line 8: Enter the number " + "to be searched: "); //Line 8 number = console.nextInt(); //Line 9
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: System.out.println(); //Line 10 index = seqSearch(intList, arraySize, number); //Line 11 if (index != -1) //Line 12 System.out.println("Line 13: " + number + " is found at position " + index); //Line 13 else //Line 14 System.out.println("Line 15: " + number + " is not in the list."); //Line 15 } public static int seqSearch(int list, int listLength, int searchItem) { int loc; boolean found = false; for (loc = 0; loc < listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1; } }...
View Full Document

This note was uploaded on 06/17/2011 for the course COMP 2301 taught by Professor Staff during the Spring '10 term at Kennesaw.

Ask a homework question - tutors are online