944,192 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 14976
  • Java RSS
Nov 15th, 2004
0

Sequential search in java

Expand Post »
hi.i've written a program that loads #'s into an array and sorts them.The end user then inputs a number and the program is supposed to perform a sequential search for the input number and tell wether it was found or not found.I'm hoping someone canhelp me with my sequential search.binary gave me enough trouble.any idea?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
josirucu is offline Offline
2 posts
since Nov 2004
Nov 15th, 2004
0

Re: Sequential search in java

>binary gave me enough trouble.
Binary search is leaps and bounds more complex than sequential search. Start at the beginning, and go to the end. If you find a match, stop. What's so hard about that? You probably solved the first half of the problem when you printed the sorted array (ie. start at the beginning and go to the end).
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 17th, 2004
0

Re: Sequential search in java

Java Syntax (Toggle Plain Text)
  1. /**
  2.  * Requires j2se 5.0 (version 1.5). This uses class Scanner, static import,
  3.  * and some methods of class Arrays.
  4.  */
  5.  
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8. import static java.lang.Math.*;
  9.  
  10. public class Prog3c_1
  11. {
  12. public static void main(String... args)
  13. {
  14. Scanner sc = new Scanner(System.in);
  15.  
  16. int j, k, number, size;
  17. boolean found = false;
  18.  
  19. System.out.print("Enter the array size: ");
  20. size = sc.nextInt();
  21.  
  22. int[] intArray = new int[size];
  23.  
  24. for (j=0; j<size; j++)
  25. intArray[j] = (int) (random()*100);
  26.  
  27. System.out.println("A " + size + " element int array of random "
  28. + "ints between 0 and 99 has been loaded.");
  29. System.out.print("Guess one of the numbers: ");
  30. number = sc.nextInt();
  31.  
  32. k=0;
  33. while (!found && k<size)
  34. if (intArray[k]==number)
  35. found=true;
  36. else
  37. k +=1;
  38.  
  39. System.out.println();
  40. if (found)
  41. System.out.println("Found in position " + k);
  42. else
  43. System.out.println("The number is not found");
  44.  
  45. System.out.println("\nThe random array for verification is:\n");
  46. System.out.println(Arrays.toString(intArray));
  47. }
  48. }
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Nov 18th, 2004
0

Re: Sequential search in java

There's always some goofball who encourages lazy students by doing their homework for them.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 4th, 2010
0
Re: Sequential search in java
let me try dis one..
Last edited by foxpro21; Oct 4th, 2010 at 7:17 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
foxpro21 is offline Offline
1 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Creating a Second Window
Next Thread in Java Forum Timeline: setting the classpath for derby





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC