tium
asked on
return doesn't return
i have a recursive method that give me an error.
no return!! blah...
look:
public int recursiveMinimum(int[] arr, int size){
if (size == 1)
return arr[size];
else {
if (arr[size] < (recursiveMinimum(arr, size-1)))
return arr[size];
}
}
this method accept an integer array and an integer size of array. the purpose of this method is to find the minimum value of an element in the array and return to its name. can somebody tell me why it gives me an error
no return statement.
which i think my pseudocode is clear enough for me to understand.
no return!! blah...
look:
public int recursiveMinimum(int[] arr, int size){
if (size == 1)
return arr[size];
else {
if (arr[size] < (recursiveMinimum(arr, size-1)))
return arr[size];
}
}
this method accept an integer array and an integer size of array. the purpose of this method is to find the minimum value of an element in the array and return to its name. can somebody tell me why it gives me an error
no return statement.
which i think my pseudocode is clear enough for me to understand.
the procedure sould be:
if (size == 1)
return arr[size];
else {
// not sure whether the array elements are int
int min = recursiveMinimum(arr, size-1);
if (arr[size] < min)
return arr[size];
else
return min;
}
}
if (size == 1)
return arr[size];
else {
// not sure whether the array elements are int
int min = recursiveMinimum(arr, size-1);
if (arr[size] < min)
return arr[size];
else
return min;
}
}
Listening...
There should be return statement for if none of your two conditions are achieved
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But anyway, why do you want to recurse an array, when you can do'it in the clasic way? For bigger arrays this aproach will be slower since is known that recursion needs method context saved/changed at every recursive call.
About your method, see the comments :
public int recursiveMinimum(int[] arr, int size){
if (size == 1) // what is happening with element 0 of the array?
return arr[size]; //are you sure that is the minimum? How, you have no history of a previously detected minimum value ...
else {
if (arr[size] < (recursiveMinimum(arr, size-1)))
return arr[size];
// else what's happend if not lower ?
}
About your method, see the comments :
public int recursiveMinimum(int[] arr, int size){
if (size == 1) // what is happening with element 0 of the array?
return arr[size]; //are you sure that is the minimum? How, you have no history of a previously detected minimum value ...
else {
if (arr[size] < (recursiveMinimum(arr, size-1)))
return arr[size];
// else what's happend if not lower ?
}
Be kinder to your vm :-)
public class ArraySmallest {
final static int[] numbers = { 1,3,9,20,6,0,20,2,6,7 };
public static void main(String[] args){
System.out.println(getIxSm allest(num bers));
}
public static int getIxSmallest(int[] numbers){
int n = Integer.MAX_VALUE;
int ix = -1;
for(int i= 0;i < numbers.length;i++){
if (numbers[i] < n){
n = numbers[i];
ix = i;
}
}
return ix;
}
}
public class ArraySmallest {
final static int[] numbers = { 1,3,9,20,6,0,20,2,6,7 };
public static void main(String[] args){
System.out.println(getIxSm
}
public static int getIxSmallest(int[] numbers){
int n = Integer.MAX_VALUE;
int ix = -1;
for(int i= 0;i < numbers.length;i++){
if (numbers[i] < n){
n = numbers[i];
ix = i;
}
}
return ix;
}
}
ASKER
thanks for all of your comment.
but i already made the same solution what Venci75 did. but it gave me array out of bound error.
but i already made the same solution what Venci75 did. but it gave me array out of bound error.
ASKER
thanks for all of your comment.
but i already made the same solution what Venci75 did. but it gave me array out of bound error.
but i already made the same solution what Venci75 did. but it gave me array out of bound error.
That is because the array indexes start from 0
if (size == 1)
return arr[size-1];
else {
int min = recursiveMinimum(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
if (size == 1)
return arr[size-1];
else {
int min = recursiveMinimum(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
ASKER
you mean to say, that i'll do this code
int minimunValue = recursiveMin(anArray,anArr ay.length- 1);
System.out.println("minimu nValue : " + minimunValue);
subtracting 1 to anArray.length. but take the comment of OVI.
// what is happening with element 0 of the array?
//are you sure that is the minimum? How, you have no history of a previously detected minimum value ...
and if i initialize the array like this.
int[] anArray = { 1000 );
an array with one element w/c have one index of 0.
it will give stackoverflow error.
but recursive method should return 1000 because the size is 1.
right?
int minimunValue = recursiveMin(anArray,anArr
System.out.println("minimu
subtracting 1 to anArray.length. but take the comment of OVI.
// what is happening with element 0 of the array?
//are you sure that is the minimum? How, you have no history of a previously detected minimum value ...
and if i initialize the array like this.
int[] anArray = { 1000 );
an array with one element w/c have one index of 0.
it will give stackoverflow error.
but recursive method should return 1000 because the size is 1.
right?
int[] anArray = {};
have
size=1
one element with index 0
have
size=1
one element with index 0
sorry - I made a mistake - shoud be:
int[] anArray = {1000};
have
size=1
one element with index 0
int[] anArray = {1000};
have
size=1
one element with index 0
I believe you will never encounter this problem with my solution.
ASKER
hey Ovi, i got it. look what i got when not using your method. i use the method of Venci75. it doesn't return any error at all.
if (size == 0) <<< change it to zero
return arr[size-1];
else {
int min = recursiveMinimum(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
if (size == 0) <<< change it to zero
return arr[size-1];
else {
int min = recursiveMinimum(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
if (size == 0) <<< change it to zero
return arr[size-1];
this will throw a ArrayIndexOutOfBounds exception for 0 size array. Try this :
public class Min1 {
static {
int[] a = new int[0];
recurseMin(a, a.length);
}
public static int recurseMin(int[] arr, int size) {
if (size == 0) //<<< change it to zero
return arr[size-1];
else {
int min = recurseMin(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
}
return arr[size-1];
this will throw a ArrayIndexOutOfBounds exception for 0 size array. Try this :
public class Min1 {
static {
int[] a = new int[0];
recurseMin(a, a.length);
}
public static int recurseMin(int[] arr, int size) {
if (size == 0) //<<< change it to zero
return arr[size-1];
else {
int min = recurseMin(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
}
Even my method is not prepared for that ...
Should be changed to:
public static int getMin(int[] arr) {
if(arr == null || arr.length == 0)
throw new IllegalArgumentException(" Array cannot be null or 0 length");
return(recurseMin(arr, arr.length - 1, Integer.MAX_VALUE));
}
Should be changed to:
public static int getMin(int[] arr) {
if(arr == null || arr.length == 0)
throw new IllegalArgumentException("
return(recurseMin(arr, arr.length - 1, Integer.MAX_VALUE));
}
ASKER
but ovi it works, try it it, i'll give the same example you made.
[pre]
class RecursiveMethods { // begin RecursiveMethods
public int recursiveMin(int[] arr, int size) { // begin recursiveMin
if (size == 0)
return arr[size];
else { // begin else
int min = recursiveMin(arr, size-1);
return (arr[size] < min) ? arr[size] : min;
} // end else
} //end recursiveMin
public int recursiveMax(int[] arr, int size) { // begin recursiveMax
if (size == 0)
return arr[size];
else { // begin else
int max = recursiveMin(arr, size-1);
return (arr[size] > max) ? arr[size] : max;
} // end else
} //end recursiveMax
} // end class RecursiveMethods
public class Recursive { // begin class
public static void main(String[] args){ // begin main
int[] anArray = new int[] {1100, 230, 346, 234, 1256};
RecursiveMethods recur = new RecursiveMethods();
System.out.println("Ex 1 min: " + recur.recursiveMin(anArray , anArray.length -1 ));
System.out.println("Ex 1 max: " + recur.recursiveMax(anArray , anArray.length -1 ));
anArray = new int[] {453, -100000, -256, 45, 78, -100, 200, 1, 3, 5, 7, 1000};
System.out.println("Ex 2 min: " + recur.recursiveMin(anArray , anArray.length -1 ));
System.out.println("Ex 2 max: " + recur.recursiveMax(anArray , anArray.length -1 ));
anArray = new int[]{453};
System.out.println("Ex 3 min: " + recur.recursiveMin(anArray , anArray.length -1 ));
System.out.println("Ex 3 max: " + recur.recursiveMax(anArray , anArray.length -1 ));
} // end main
} // end class
[/pre]
i did some addition if it really works, i added recursiveMax, with the same technique.
p.s.
i'm not saying that your code is bad, actually it's excelent, i just experimenting! :)
[pre]
class RecursiveMethods { // begin RecursiveMethods
public int recursiveMin(int[] arr, int size) { // begin recursiveMin
if (size == 0)
return arr[size];
else { // begin else
int min = recursiveMin(arr, size-1);
return (arr[size] < min) ? arr[size] : min;
} // end else
} //end recursiveMin
public int recursiveMax(int[] arr, int size) { // begin recursiveMax
if (size == 0)
return arr[size];
else { // begin else
int max = recursiveMin(arr, size-1);
return (arr[size] > max) ? arr[size] : max;
} // end else
} //end recursiveMax
} // end class RecursiveMethods
public class Recursive { // begin class
public static void main(String[] args){ // begin main
int[] anArray = new int[] {1100, 230, 346, 234, 1256};
RecursiveMethods recur = new RecursiveMethods();
System.out.println("Ex 1 min: " + recur.recursiveMin(anArray
System.out.println("Ex 1 max: " + recur.recursiveMax(anArray
anArray = new int[] {453, -100000, -256, 45, 78, -100, 200, 1, 3, 5, 7, 1000};
System.out.println("Ex 2 min: " + recur.recursiveMin(anArray
System.out.println("Ex 2 max: " + recur.recursiveMax(anArray
anArray = new int[]{453};
System.out.println("Ex 3 min: " + recur.recursiveMin(anArray
System.out.println("Ex 3 max: " + recur.recursiveMax(anArray
} // end main
} // end class
[/pre]
i did some addition if it really works, i added recursiveMax, with the same technique.
p.s.
i'm not saying that your code is bad, actually it's excelent, i just experimenting! :)
You have posted this few comments ago :
if (size == 0) <<< change it to zero
return arr[size-1]; // THIS WILL THROW EXCEPTION
else {
int min = recursiveMinimum(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
The code posted by you in your last comment is working fine because the syntax was cleared up, and instead of returning arr[size - 1] you return arr[size].
if (size == 0) <<< change it to zero
return arr[size-1]; // THIS WILL THROW EXCEPTION
else {
int min = recursiveMinimum(arr, size-1);
if (arr[size-1] < min)
return arr[size-1];
else
return min;
}
}
The code posted by you in your last comment is working fine because the syntax was cleared up, and instead of returning arr[size - 1] you return arr[size].
ASKER
oh, i'am sorry, i just cut and paste the sample code with out using the class type. but have you tried the complete code i posted. actually i notice your technique
if(pos == 0) { <<< this one
i remember that an array has intial index of 0, so i change that. how stupid i'am. very well, thanks a lot. for your patience.
teng
p.s.
do you know any site pertaining to Swing, i don't like the Swing tutorials of java sun. it's to general, i want to learn it by details. i have download some swing tutorials but still i can't grasp the idea. thanks again. master!
if(pos == 0) { <<< this one
i remember that an array has intial index of 0, so i change that. how stupid i'am. very well, thanks a lot. for your patience.
teng
p.s.
do you know any site pertaining to Swing, i don't like the Swing tutorials of java sun. it's to general, i want to learn it by details. i have download some swing tutorials but still i can't grasp the idea. thanks again. master!
ASKER
this book what i pertaining for
>>i have download some swing tutorials but still i can't grasp the idea
thanks any way.
>>i have download some swing tutorials but still i can't grasp the idea
thanks any way.
if (size == 1)
return arr[size];
else {
if (arr[size] < (recursiveMinimum(arr, size-1)))
return arr[size];
// else - does not return ???
}
}