Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    Sorry, I changed it.
  2. #17
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    it does nothing
    Not sure what that means.
    What happens when you execute the program?
    After you enter a number what happens?
    Does the program exit normally or does it continue executing and not exit?

    Try debugging the code by adding lots of println() statements that print out the values of variables as the code executes so you can see what the computer sees when it executes. Be sure to print the variables used to control looping.
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    so here's my new code that works. I just don't understand why when the program does not find the value that it only prints the sequential search, it should print sequential and binary and then print....Enter an integer to search the arrays (EOF to stop): CTRL-D again. Any help would be great.

    Code:
    import java.util.*;
    
    public class program1 extends Program1Class {
      
      static Scanner kb = new Scanner(System.in);
      
      public static void main(String []args) {
        
        int[] array1 = new int[1000];
        int[] array2 = new int[1000];
        int num;
        int integer = 0;
        boolean valid;
        
        for (num = 0; num < array1.length;num++)
        {
          int randNumber = (int) (Math.random() * 1000 + 1);
          array1[num] = randNumber;
          array2[num] = randNumber;
        }
        Arrays.sort(array1);
        System.out.println("1000 random numbers 0 to 999 have been generated"
                             +"\n");
        System.out.print("Enter an integer to search the arrays (EOF to stop):"); 
        while ( kb.hasNext() ) {
          integer = kb.nextInt();
        int seqIndex = seq_search(array2, integer);
            if (seqIndex < 0){
              System.out.println("sequntial search did not find the value with " + SEQ_NUM + " items scanned.");
            }
            else {
              System.out.println("sequential search found the value "+ integer +" in element " + seqIndex + 
                                 " with " + SEQ_NUM + " items scanned.");
          int binIndex = bin_search(array1, integer);
            if(binIndex < 0){
            System.out.println("binary    search did not find the value with " + BIN_NUM + " items scanned.");
          }
          else {
            System.out.println("binary     search found the value "+ integer +" in element " + binIndex + 
                               " with " + BIN_NUM + " items scanned."); }
            System.out.print("\nEnter an interger to search the arrays (EOF to stop):"); 
          }
        }
      }
    }
  6. #19
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Please copy the full text of what the program prints out and paste it here.

    The code is poorly formatted making it hard to see what code is included within a pairs of {}s.
    Please: Format the code to make it easier to see the logic.
    Last edited by NormR; December 14th, 2013 at 06:15 AM.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    Here is the output...sorry how it is formatted. I'm completely new to programming.

    1000 random numbers 0 to 999 have been generated

    Enter an integer to search the arrays (EOF to stop): 500
    sequential search found the value 500 in element 770 with 771 items scanned.
    binary search found the value 500 in element 486 with 10 items scanned.

    Enter an integer to search the arrays (EOF to stop): 34
    sequential search found the value 34 in element 637 with 638 items scanned.
    binary search found the value 34 in element 43 with 9 items scanned.

    Enter an integer to search the arrays (EOF to stop): 46
    sequential search did not find the value with 1000 items scanned.

    Under the last line, it should have also printed the binary search also and then printed a line to add another integer.
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    Like this...

    1000 random numbers from 0 to 999 have been generated

    Enter an integer to search the arrays (EOF to stop): 467
    sequential search found the value 467 in element 500 with 501 items scanned.
    binary search found the value 467 in element 461 with 9 items scanned.

    Enter an integer to search the arrays (EOF to stop): 999
    sequential search found the value 999 in element 475 with 476 items scanned.
    binary search found the value 999 in element 998 with 9 items scanned.

    Enter an integer to search the arrays (EOF to stop): 34
    sequential search did not find the value with 1000 items scanned.
    binary search did not find the value with 10 items scanned.

    Enter an integer to search the arrays (EOF to stop): 145
    sequential search found the value 145 in element 499 with 500 items scanned.
    binary search found the value 145 in element 141 with 9 items scanned.

    Enter an integer to search the arrays (EOF to stop): CTRL-D
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    If you fix the formatting it will be easier to see why the code does what it does when it is executed.

    The {}s should line up vertically and there should NOT be any }s hidden at the end of a statement.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo