Hi,
I am trying to use recursion and count to find the number of times the number 5 appears in an array. I have the following code:

public static int countfive(double[] array1, int beginarray, int endarray,int count)
	 {
		 int next;
		 
		 if(beginarray==endarray)
		 {
			 if(array1[startIndex]==5)
			 return 1;
			 else
			 return 0;
		 }
		 else 
		 {
			 if(array1[beginarray]==5)
			 {
				 count=count+1;
				 System.out.println("COUNT ONE"+count);
				 
				 next=countNegative(array1,beginarray+1,endarray,count)+startIndex;
				
			 }
			 else if(array1[beginarray]!=5)
			 {
				 
				 System.out.println("COUNT TWO"+count);
				 next=countNegative(array1,beginarray+1,endarray,count);
				 System.out.println("THIS IS BEGIN ACCESSED");
			 }
			 
		 }
		 System.out.println("VALUE"+count);
		return count;
		 
	}

I have tested this code on a set of numbers, and i get the following:
If i enter:
1
2
3
4
5
5
6
5

The following output occurs:

COUNT TWO0
COUNT TWO0
COUNT TWO0
COUNT TWO0
COUNT ONE1
COUNT ONE2
COUNT TWO2
COUNT ONE3
COUNT TWO3
THIS IS BEGIN ACCESSED
VALUE3
VALUE3
THIS IS BEGIN ACCESSED
VALUE2
VALUE2
VALUE1
THIS IS BEGIN ACCESSED
VALUE0
THIS IS BEGIN ACCESSED
VALUE0
THIS IS BEGIN ACCESSED
VALUE0
THIS IS BEGIN ACCESSED
VALUE0

I am not sure why the count value is being subtracted and how i can prevent that from happening so i can just return the correct value ?

Not sure what the countNegative function does or why you are calling it. I also don't see the purpose or the need for the next variable. What is startIndex ? There's also nothing recursive about this function. I don't see any call to countfive .

A recursive method calls itself in order to come up with the final answer. Your method is returning count, which does not do what I just said. Your method should have the following parameters: currentIndex (the index the array just checked), count, array (your array). A recursive method also needs a base case to tell it when to stop the recursion. In your case, since you want to go through the whole array, counting the number of times 5 appears, where do you want to stop the recursion? Clearly, you want to stop when you have gone through the entire array.

To help you out, here is the pseudocode, or at least part of it:

count(currentIndex, count, array){
if currentIndex is at the end of the array, return count.
else if the next index has a 5, return count(currentIndex+1, count+1, array)
else, the next index does not have a 5, so return count(currentIndex+1, count, array)
}

It is a little sloppy but that is the general idea. You have to call the method for it to be recursive, and you have to have some mechanism that stops it from calling itself. In this case, when it is at the end of the array, it should stop calling itself, because it has no more indexes to look at.

commented: Good explanation of recursive calls with example pseudocode +4

Thanks BestJewSinceJC that helped me fix the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.