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

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    Can't get the correct output...please help.


    Here is what my output should be:

    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


    Here is my code.....can anyone tell me what i'm doing incorrectly? I've been racking my brain and have no idea how to get this correct. I feel like I did all this for nothing. Please Help. Thanks.

    Code:
    import java.util.*;
    
    public class program1 { 
      
      public static void printArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
          System.out.println(a[i]);
        }
        System.out.println();
        System.out.println();
      }
      static Scanner kb = new Scanner(System.in);
      public static void main(String []args) {
      
        int[] bArray = new int[1000];
        int[] sArray = new int[1000];
        int num ;
        int integer = 0;
        
        for (num = 0; num < bArray.length;num++) {
       
          int randNumber = (int) (Math.random() * 1000 +1);
          bArray[num] = randNumber;
          sArray[num] = randNumber;
        }
        Arrays.sort(bArray);
        System.out.println("1000 random numbers 0 to 999 have been generated"
                   +"\n\t (Very grudgingly might I add!)"
                   +"\n");
        System.out.print("Enter an integer to search the arrays (EOF to stop):");
        while ( kb.hasNext() ) {
          do {
            try {
              valid = true;
              integer = kb.nextInt();
            }
            catch(Exception e){
              valid = false;
              
              integer = kb.nextInt();
            }
          } while (!valid);
        }
        int bIndex = bin_search(bArray, integer);
        
        if(bIndex < 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 " + bIndex + " with " + BIN_NUM + 
                             " items scanned.");
        }
        int sIndex = seq_search(sArray, integer);
        
        if (sIndex < 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 " + sIndex + " with " + SEQ_NUM + 
                             " items scanned.");
        }
        System.out.print("\nEnter an interger to search the arrays (EOF to stop):");
      }
    }
    Here is my output....

    9 errors found:
    File: C:\Documents and Settings\Drjava\program1.java [line: 34]
    Error: cannot find symbol
    symbol: variable valid
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 38]
    Error: cannot find symbol
    symbol: variable valid
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 42]
    Error: cannot find symbol
    symbol: variable valid
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 44]
    Error: cannot find symbol
    symbol: method bin_search(int[],int)
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 47]
    Error: cannot find symbol
    symbol: variable BIN_NUM
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 50]
    Error: cannot find symbol
    symbol: variable BIN_NUM
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 53]
    Error: cannot find symbol
    symbol: method seq_search(int[],int)
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 56]
    Error: cannot find symbol
    symbol: variable SEQ_NUM
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 59]
    Error: cannot find symbol
    symbol: variable SEQ_NUM
    location: class program1

    What do I need to change to make this correct?? Thanks.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,718
    Rep Power
    348
    Error: cannot find symbol
    The compiler can not find a definition for many variables whose names are given in the error messages.
    Make sure the code defines all the variables that it uses.

    Please edit the post and wrap the code in code tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    Am I even close to getting this working correctly? I am new to the programming world and have been at this for 2 weeks. I did not know where to turn. Thanks again.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,718
    Rep Power
    348
    Am I even close to getting this working correctly?
    Did you fix the errors? If not copy and paste the full text of the error messages here.

    If there are no errors, what happens when you execute the program?
    What output does it create?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    I'm still stuck getting....

    9 errors found:
    File: C:\Documents and Settings\Drjava\program1.java [line: 34]
    Error: cannot find symbol
    symbol: variable valid
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 38]
    Error: cannot find symbol
    symbol: variable valid
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 42]
    Error: cannot find symbol
    symbol: variable valid
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 44]
    Error: cannot find symbol
    symbol: method bin_search(int[],int)
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 47]
    Error: cannot find symbol
    symbol: variable BIN_NUM
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 50]
    Error: cannot find symbol
    symbol: variable BIN_NUM
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 53]
    Error: cannot find symbol
    symbol: method seq_search(int[],int)
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 56]
    Error: cannot find symbol
    symbol: variable SEQ_NUM
    location: class program1
    File: C:\Documents and Settings\Drjava\program1.java [line: 59]
    Error: cannot find symbol
    symbol: variable SEQ_NUM
    location: class program1

    I know I need to define the variables...but i'm not sure how to do that to get the correct output I need. Thanks for everything.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,718
    Rep Power
    348
    how to do that to get the correct output
    The program should NOT execute and make any output until you fix the compiler errors.

    Fix the errors so the program can execute. Then you can work on what it outputs when it executes.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    I can't fix the errors, i'm not quite sure what i'm doing incorrectly. I read all the material and i'm stumped. I have not been able to work on this for I had a death in the family. I'm screwed. It's do at the end of the week. Thanks for all the help anyways guys.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,718
    Rep Power
    348
    The variables named in the cannot find symbol error messages need to be defined.
    Add definition statements for all the variable in those type of error messages.

    Something like the following:

    int varOne;

    String varTwo;

    The data type first followed by the name.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    So....Boolean valid;?
    int BIN_NUM;
    int SEQ_NUM;

    Is that the right direction? It cleared up some errors. So sorry, my head is not where it needs to be but I have to get this finished. Thanks so much.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,718
    Rep Power
    348
    Did that fix the compiler errors?
    Now what happens when the code is executed?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    I now have two errors..

    2 errors found:
    File: C:\Documents and Settings\Drjava\p10.java [line: 45]
    Error: cannot find symbol
    symbol: method bin_search(int[],int)
    location: class p10
    File: C:\Documents and Settings\Drjava\p10.java [line: 56]
    Error: cannot find symbol
    symbol: method seq_search(int[],int)
    location: class p10
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,718
    Rep Power
    348
    The compiler can not find definitions for the two methods whose names are in the error messages.
    The code needs to have definitions for those methods.
    Start by just defining the method headers followed by empty {}s so that the compiler is happy.

    Then work on the logic for what those methods should do. Make a list of the specific steps the code in the method must take to do its task before trying to write any code.
    When you've worked out the logic, then try writing the code.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    I tried to define the method headers and got an illegal start of expression which is usually a curly bracket issue and I can't find it. Time to give up. Thanks.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    So here's what I have...

    Code:
    public class Program1Class
    {
      public static int SEQ_NUM;
      public static int BIN_NUM;
       
      public static int seq_search(int[] data, int key)
      {
        boolean found = false;
        int i = 0;
         
        SEQ_NUM = 1000;
         
        while ( !found && (i < data.length) )
        {
          if ( data[i] == key )
            found = true;
          else
            i++;
           
          SEQ_NUM++;
        }
         
        if ( found )
          return i;
        else
          return -1;
      }
       
      public static int bin_search(int[] data, int key)
      {
        boolean found = false;
        int midpoint = 0, first = 0, last;
         
        BIN_NUM = 10;
        last = data.length - 1;
         
        while ( ( first <= last ) && !found )
        {
          midpoint = (first + last) / 2;
          if ( data[midpoint] == key )
            found = true;
          else if ( data[midpoint] > key )
            last = midpoint - 1;
          else
            first = midpoint + 1;
           
          BIN_NUM++;
        }
         
        if ( found )
          return midpoint;
        else
          return -1;
      }
    }
    Code:
    import java.util.*;
    
    public class program1 extends Program1Class { 
    
      public static void printArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
          System.out.println(a[i]);
        }
        System.out.println();
        System.out.println();
      }
      static Scanner kb = new Scanner(System.in);
      public static void main(String []args) {
    
        int[] bArray = new int[1000];
        int[] sArray = new int[1000];
        int num;
        int integer = 0;
        int BIN_NUM = 1000;
        int SEQ_NUM = 1;
        boolean valid;
     
        for (num = 0; num < bArray.length;num++) {
       
          int ranNumber = (int) (Math.random() * 1000 +1);
          bArray[num] = ranNumber;
          sArray[num] = ranNumber;
        }
        Arrays.sort(bArray);
        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() ) {
          do {
            try {
              valid = true;
              integer = kb.nextInt();
            }
            catch(Exception e) {
              valid = false;
              integer = kb.nextInt();
            }
          } while (!valid);
        }
       int bIndex = bin_search(bArray, integer);
       
       if(bIndex < 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 " + bIndex + 
                             " with " + BIN_NUM + " items scanned.");
        }
        int sIndex = seq_search(sArray, integer);
        
        if (sIndex < 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 " + sIndex + 
                             " with " + SEQ_NUM + " items scanned.");
        }
        System.out.print("\nEnter an interger to search the arrays (EOF to stop):");
      }
    }
    It compiles, but when I type in a number to search, it does nothing. What am I missing?? Help??
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,718
    Rep Power
    348
    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo