Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-16-2008, 07:18 AM
Member
 
Join Date: Feb 2008
Posts: 3
NatNat is on a distinguished road
Recursive Method ==> find how many times a value is repeated in an array
Hi, I need to use a recursive method to find the number of times a certain value is repeated within a specific array of integers. I'm really lost on this one. I know how to do this iteratively, but if someone can hint me towards the base case or more of the logic than that, it would be greatly appreciated.

I am aware that this can be done without using recursion, however, I need to do this recursively. Any help you can give would be greatly appreciated. Thanks in advance! My code (or what I have of code) is posted below:

Code:
import.java.io.*; public class Main( public static int getNumberEqual(int[] data, int first, int last, int val) { if(first==val) ++count; else return getNumberEqual(data, first+1, last, val); if(last==val) ++count; } public static void main(String[] args) { int firstIndex, lastIndex; int[] ints = new int[10]; ints[0] = 20; ints[1] = 18; ints[2] = 40; ints[3] = 34; ints[4] = 50; ints[5] = 40; ints[6] = 20; ints[7] = 13; ints[8] = 98; ints[9] = 1; int f = 4; int val = 20; firstIndex = 0; lastIndex = ints.length - 1; System.out.println("GetNumberEqual for val " + val + " is "+ getNumberEqual(ints, firstIndex, lastIndex, val)); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-16-2008, 07:59 AM
Member
 
Join Date: Feb 2008
Posts: 3
NatNat is on a distinguished road
Follow Up
I've actually altered my code already, but it still does not work. I changed the static method 'getNumberEqual()' to this:

public static int getNumberEqual(int[] data, int first, int last, int val) {
if(first==last)
return count;
if(first==val)
{
count++;
return getNumberEqual(data, first+1, last, val);
}
else
return getNumberEqual(data, first+1, last, val);
return count;
}

Thanks again
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-16-2008, 10:52 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
hey is on a distinguished road
Code:
//import java.io.*; public class temp1class{ public static int getNumberEqual(int[] data, int curr, int val) { if (curr == 0){ if (val == data[curr]){ return 1; } else { return 0; } } else { if (val == data[curr]){ curr--; return getNumberEqual(data, curr, val)+1; } else { curr--; return getNumberEqual(data, curr, val); } } } public static void main(String[] args) { int[] ints = new int[10]; ints[0] = 20; ints[1] = 18; ints[2] = 40; ints[3] = 34; ints[4] = 50; ints[5] = 40; ints[6] = 20; ints[7] = 20; ints[8] = 98; ints[9] = 1; int f = 4; int val = 20; System.out.println("GetNumberEqual for val " + val + " is "+ getNumberEqual(ints, ints.length-1, val)); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
exercise of recursive method amexudo New To Java 2 03-09-2008 07:55 PM
Recursive Method ==> find minimum value from array NatNat New To Java 1 02-16-2008 11:10 PM
Recursive Method bluegreen7hi New To Java 5 11-29-2007 06:45 AM
problems to find the main method christina Eclipse 2 08-06-2007 09:51 PM
Problems with Find method in EJB Nick15 Enterprise JavaBeans 0 05-14-2007 03:29 PM


All times are GMT +3. The time now is 07:32 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org