Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Recursive_java_function

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Recursive_java_function

    Hello,

    I've written the following code to find out the number of occurrences of a number in a sorted array (example: [ 1 3 3 4 8] for 3, it prints 2 occurrences).

    The problem is that I'm using a recursive function, and the only solution I've found was to declare the counter as a global variable (public static int contor =0.

    I want to declare the counter INSIDE the recursive function countOccurrences. How do I do that without losing counter values?

    Thanks in advance. My code is below:

     
     
    public class teste_Iulia {
     
    	public static int contor =0; 
     
    	public static void main(String []args){
     
    		int v[]={1, 2, 4, 4, 10, 10, 20 };
    		int key = 4;
     
    		int contor = countOccurrences(v, key, 1, 7);
     
    		System.out.println(contor);
     
     
    	}
     
     
     
    	private static int countOccurrences(int[] v, int key, int lower, int upper) {
    		// TODO
    	    // Calculati recursiv numarul de aparitii ale lui key in v
    	    // intre pozitiile lower si upper. 
    	    // La calcularea pozitiei de mijloc folositi
    	    //         int m = lower + (upper - lower) / 2;
    	    // pentru a evita overflow la adunarea pe numere intregi.
     
     
    		if(lower>=upper) return contor;
     
    		else{
     
     
    		int m = lower+(upper-lower)/2;
    		System.out.println("m "+m);
     
    		if(v[m]==key) {
    			contor++;
    		}
     
    		if(v[m]>key)
    			return countOccurrences(v, key, lower, m);
     
    		else
    			return countOccurrences(v, key, m+1, upper);
     
    		}
     
     
    	}
     
    }

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,168
    My Mood
    Hungover
    Thanks
    141
    Thanked 598 Times in 513 Posts

    Default Re: Recursive_java_function

    Have a return value, and take a parameter.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Recursive_java_function

    Quote Originally Posted by radulescuiulia View Post
    ...The problem is that ...the only solution I've found...
    A bigger problem is that your function is bugous.

    I mean, if your binary search just happens to land on the first of two occurrences and there are exactly two occurrences, well, OK, but. a few runs with different points of occurrence shows some incorrect counts:

    for v = [8, 9, 10, 11, 12, 13, 14]:
    countOccurrences(7) = 0 <---> OK!

    for v = [7, 8, 9, 10, 11, 12, 13]:
    countOccurrences(7) = 0 <---> Huh? Should be 1

    for v = [1, 7, 9, 10, 11, 12, 13]:
    countOccurrences(7) = 1 <---> OK!

    for v = [1, 2, 7, 10, 11, 12, 13]:
    countOccurrences(7) = 1 <---> OK!

    for v = [1, 2, 3, 7, 11, 12, 13]:
    countOccurrences(7) = 1 <---> OK!

    for v = [1, 2, 3, 4, 7, 12, 13]:
    countOccurrences(7) = 1 <---> OK!

    for v = [1, 2, 3, 4, 6, 7, 13]:
    countOccurrences(7) = 1 <---> OK!

    for v = [1, 2, 3, 4, 5, 6, 7]:
    countOccurrences(7) = 1 <---> OK!

    for v = [7, 7, 9, 10, 11, 12, 13]:
    countOccurrences(7) = 1 <---> Huh? Should be 2

    for v = [1, 7, 7, 10, 11, 12, 13]:
    countOccurrences(7) = 1 <---> Huh? Should be 2

    for v = [1, 2, 7, 7, 11, 12, 13]:
    countOccurrences(7) = 2 <---> OK!

    for v = [1, 2, 3, 7, 7, 12, 13]:
    countOccurrences(7) = 1 <---> Huh? Should be 2

    for v = [1, 2, 3, 4, 7, 7, 13]:
    countOccurrences(7) = 2 <---> OK!

    for v = [1, 2, 3, 4, 6, 7, 7]:
    countOccurrences(7) = 1 <---> Huh? Should be 2

    for v = [7, 7, 7, 10, 11, 12, 13]:
    countOccurrences(7) = 1 <---> Huh? Should be 3

    for v = [1, 7, 7, 7, 11, 12, 13]:
    countOccurrences(7) = 2 <---> Huh? Should be 3
    .
    .
    .
    // Etc.
    .
    .
    .
    // With a seven-element array:
    // Your method can not get the correct count value for occurrences greater than two.
    .
    .
    .


    Cheers!

    Z