November 20th, 2013, 06:27 AM
-
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.
November 20th, 2013, 07:43 AM
-
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.
November 20th, 2013, 10:22 AM
-
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.
November 20th, 2013, 10:25 AM
-
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?
November 20th, 2013, 08:28 PM
-
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.
November 20th, 2013, 08:39 PM
-
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.
November 25th, 2013, 03:04 PM
-
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.
November 25th, 2013, 03:12 PM
-
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.
November 25th, 2013, 08:28 PM
-
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.
November 25th, 2013, 08:30 PM
-
Did that fix the compiler errors?
Now what happens when the code is executed?
November 25th, 2013, 08:34 PM
-
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
November 25th, 2013, 08:43 PM
-
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.
November 25th, 2013, 09:24 PM
-
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.
December 8th, 2013, 09:58 PM
-
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??
December 9th, 2013, 05:52 AM
-
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.