posted 8 months ago

Hello Jave'ers,

I am currently working on a program that is to be able to calculate the smallest int from an int[] array, but with a few conditions: that my method is static, that I do not use static variables, that I do not use any loops within the method, & that I use recursion in the method in order to find the smallest number. I have been dreaded with this for 3 days now and am going a little bit mad tbh. I keep trying, deleting, trying and deleting but cannot solve this. I am feeling pretty incompetent by now. I am wondering if anyone can lead me i the right direction on how to do this. I just need a different point of view because mine's is clearly not the right one. Thanks in advance! Below is the code:

I am currently working on a program that is to be able to calculate the smallest int from an int[] array, but with a few conditions: that my method is static, that I do not use static variables, that I do not use any loops within the method, & that I use recursion in the method in order to find the smallest number. I have been dreaded with this for 3 days now and am going a little bit mad tbh. I keep trying, deleting, trying and deleting but cannot solve this. I am feeling pretty incompetent by now. I am wondering if anyone can lead me i the right direction on how to do this. I just need a different point of view because mine's is clearly not the right one. Thanks in advance! Below is the code:

posted 8 months ago

You've got a lot of code there but it isn't going to give you the right answer.

Basically you want to be comparing the first element in the range (start-index to end-index) to the minimum of the rest of the elements in the range. That's how the recursive part comes in. I'll leave you to work out how to stop the recursion, it's similar to some things you already have in your post.

Basically you want to be comparing the first element in the range (start-index to end-index) to the minimum of the rest of the elements in the range. That's how the recursive part comes in. I'll leave you to work out how to stop the recursion, it's similar to some things you already have in your post.

posted 8 months ago

There's no need to pass in the endIndex it can be determined by the length of the numbers array. Then, instead of startIndex it would be currentIndex which would be set to zero in the first call and incremented by one for each recursive call.

I Think that you are overthinking the plumbing. you should be able to implement the method with about 5 lines of code in the body.

There's no need to pass in the endIndex it can be determined by the length of the numbers array. Then, instead of startIndex it would be currentIndex which would be set to zero in the first call and incremented by one for each recursive call.

I Think that you are overthinking the plumbing. you should be able to implement the method with about 5 lines of code in the body.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.