Help with sequential searching
I have the following code and I'm trying to add a counter in order to count and return the number of occurrences of parameterkey, but I'm not sure how to properly implement it, I know I have to instantiate an array counter, which I did, but I'm not sure where to go from there. Any help greatly appreciated.
/** Searches for key in integer array named arr
//arr is an instance variable of the class and has been
//instantiated and filled with random values.
// @param key value to search for
// @return if key is found, the index of the first element
//in array whose value is key; if key is not found,
//the method returns -1
*/
publicint sequentialSearch(int key )
{
int [] keyCount =newint [1];//Instantiate array counter for key
for(int i = 0; i < arr.length; i++ )
{
animate( i, 0 );
if( arr[i] == key )
return i;
keyCount ++;
return keyCount;
}
return -1;
}
You're saying two different things -- do you want to return the index of the keyin the array or the count of occurrences of the key in the array?
I need to return both. Do I need a seperate for loop in order to do that?
How can a function with return type int return 2 numbers?
I would implement two methods:* one returns the numbers of occurrences* the other, the index of the first occurrence, if any
I'm being asked to modify only this method to count the occurrences of key. I'm not supposed to add a second method. How can I apporach this problem without having to add a second method
Can you change the return type? An int is one number, not two, unless you are allowed to pack two shorts into an int?!
No, the specific question for this problem in my instruction book is as follows:The sequential search finds only the first occurrence of the parameter key. How would you modify the sequentialSearch method to count the occurrences of key?
If you return values in an array, you can do this:
public int[] sequentialSearch(int key) {
return new int[]{_search(key), _count(key)};
}
private int _search(int key) {
for(int i = 0; i < arr.length; i++) {
if (arr[i]==key)
return i;
}
return -1;
}
private int _count(int key) {
int keyCount = 0;
for(int x : arr) {
if (x==key)
keyCount++;
}
return keyCount;
}
Even better would be to return some type of pair object, rather than an array.
> The sequential search finds only the first
> occurrence of the parameter key. How would you
> modify the sequentialSearch method to count
> the occurrences of key?
I would read as "return the count of occurrences of key instead of the index of the first occurrence",
but then my first language is Hungarian.
I'm thinking maybe it just wants me to change the method completely to only return the number of occurrences, but my instructor had mentioned that this particular method should perform the search, return the index where it finds a specific value and also the number of times that value occurs.
private boolean returnSearch = false;
public int sequentialSearch(int key) {
returnSearch = !returnSearch;
return returnSearch ? _search(key) : _count(key);
}
Now calling sequentialSearch alternates initial offset, key count, initial offset, key count...
This is the smelliest code I've written all day, by the way.
Hehe, no doubt, the help is greatly appreaciated though. I am learning new things everyday in this class. I gave it a shot, but still nothing. I'm getting compiler errors as follows:
2 errors found:
File: C:\Documents and Settings\Administrator.HARR\My Documents\My Classes\COMP150\Labs\A08_07\ArrayPractice2.java [line: 83]
Error: cannot find symbol
symbol : method _search(int)
location: class ArrayPractice2
File: C:\Documents and Settings\Administrator.HARR\My Documents\My Classes\COMP150\Labs\A08_07\ArrayPractice2.java [line: 83]
Error: cannot find symbol
symbol : method _count(int)
location: class ArrayPractice2
I have the following code written:
public int sequentialSearch( int key )
{
// Note: To animate the algorithm, put this method call as the
// first statement in your for loop
// animate( i, 0 );
// where i is the index of the current array element
boolean returnSearch = false;
for( int i = 0; i < arr.length; i++ )
{
animate( i, 0 );
if( arr[i] == key )
{
returnSearch = !returnSearch;
return returnSearch ? _search(key) : _count(key);
return i;
}
}
return -1; // replace this statement with your return statement
} // end of sequentialSearch
It should be a lot simpler, we have not encountered the following form of code:return returnSearch ? _search(key) : _count(key);I'll try to continue working with it, thanks again.
Cannot find symbol means you forgot to define those methods.
> return returnSearch ? _search(key) : _count(key);That is sometimes referred to as the ternary operator: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html
I suggest you check with your instructor to find out exactly what is required.I'm sure your function should only return the count.