Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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, 06:09 AM
Member
 
Join Date: Feb 2008
Posts: 3
NatNat is on a distinguished road
Recursive Method ==> find minimum value from array
Hello, this is my first post; sorry if my format is off, but I need to use a recursive method to find the minimum value of an array of integers. I feel like I'm almost there, but it is not working. I continuously get the value of zero returned. I may be referencing the position in the array, rather than getting the value, but I'm not sure.

I am aware that this can be done without using recursion, however, I need to do this recursively. If anyone can help, it would be greatly appreciated. Thanks in advance! My code is posted below:

Code:
import java.io.*; public class Main{ public static int minimum(int[] data, int first, int last) { int M2; int M1; int mid; int min; if(first==last) { min= data[0]; return min; } else { mid = data[(first + last)/2]; M2= minimum(data, (++mid), last); M1= minimum(data, (++first), mid); if(M2<M1) return M2; else return M1;} } 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("the minimum value from the ints array is " + minimum(ints, firstIndex, lastIndex)); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-16-2008, 10:10 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 Min(int[] data, int curr, int currMin) { if (curr == 0){ if (currMin > data[curr]){ return data[curr]; } else return currMin; } else { if (currMin > data[curr]) currMin = data[curr]; curr--; return Min(data, curr, currMin); } } 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; System.out.println("Min for currMin " + Min(ints, ints.length-1, ints[ints.length-1])); } }
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 06:55 PM
Recursive Method ==> find how many times a value is repeated in an array NatNat New To Java 2 02-16-2008 09:52 PM
Recursive Method bluegreen7hi New To Java 5 11-29-2007 05:45 AM
problems to find the main method christina Eclipse 2 08-06-2007 08:51 PM
Problems with Find method in EJB Nick15 Enterprise JavaBeans 0 05-14-2007 02:29 PM


All times are GMT +3. The time now is 07:12 PM.


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