Results 1 to 8 of 8
  1. #1
    neno_os is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Help with recursion

    Can somebody please help me with this recursion. I'm trying to find minimum, maximum and average from intr[] array. The compiler sends me these errors for everytime time trying to call methods loopMin, loopMax or loopSum:

    ']' expected
    ';' expected
    illegal start of type
    <identifier> expected

    Here is the code:

    <code>
    import static java.lang.System.out;

    class rekurzion {
    public static void main(String args[]){
    int intr[]={1,2,3,4,5,6,7,8,9,10};
    int i=0, min=0, max=0, sum=0;
    double avg=0;
    min=intr[0];
    max=intr[0];
    max=loopMax(intr[i],max,i);
    i=0;
    min=loopMin(intr[i],min,i);
    i=0;
    sum=loopSum(intr[i],sum,i);
    avg=sum/10;
    out.println("Maximun is "+max);
    out.println("Minimum is "+min);
    out.println("Average is "+avg);
    }
    public int loopMax(int intr[i],int max,int i){
    if (intr[i]>max){
    max=intr[i];//find max number from intr[] array
    }
    i++;
    if (i<10){
    loopMax(intr[i],max,i);
    }
    return max;
    }
    public int loopMin (int intr[i],int min,int i){
    if (intr[i]<min){
    min=intr[i];//find min number from intr[] array
    }
    i++;
    if (i<10){
    loopMin(intr[i],min,i);
    }
    return min;
    }
    public int loopSum (int intr[i],int sum,int i){
    sum=sum+intr[i];//find sum from numbers in intr[] array
    i++;
    if (i<10){
    loopSum(intr[i],sum,i);
    }
    return sum;
    }
    }

    </code>
    Last edited by neno_os; 06-04-2010 at 02:34 PM.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    1) please learn how to pass an array as argument to a method. 2) if you want to call a method from main, this method must be defined as static. 3) after correcting a huge of errors like min=intr[0]???, the code runs correctly.

    Java Code:
    import static java.lang.System.out;
    
    class rekurzion {
    	public static void main(String args[]) {
    		int intr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    		int max = loopMax(intr);
    		int min = loopMin(intr);
    		int sum = loopSum(intr);
    		int avg = sum / intr.length;
    		out.println("Maximum is " + max);
    		out.println("Minimum is " + min);
    		out.println("Average is " + avg);
    	}
    
    	public static int loopMax(int[] intr) {
    		int max = intr[0];
    		for (int i = 1; i < intr.length; i++) {
    			if (intr[i] > max) {
    				max = intr[i];// find min number from intr[] array
    			}
    		}
    		return max;
    	}
    
    	public static int loopMin(int[] intr) {
    		int min = intr[0];
    		for (int i = 1; i < intr.length; i++) {
    			if (intr[i] < min) {
    				min = intr[i];
    			}
    		}
    		return min;
    	}
    
    	public static int loopSum(int[] intr) {
    		int sum = 0;
    		for (int i = 0; i < intr.length; i++) {
    			sum = sum + intr[i];// find sum from numbers in intr[] array
    		}
    		return sum;
    	}
    }
    Last edited by j2me64; 06-04-2010 at 02:19 PM.

  3. #3
    neno_os is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you, but I don't want to do it with any kind of loop (like for loop), I want to do it with recursion.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Technically, recursion is a loop. You can tweak each of those four loops to call the method it's inside instead of iterating over a base set. you'll need to have a base case and break statement in there so it knows when to stop though.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    compiler sends me these errors
    Please post the full text of the error messages. Your edited versions don't have enough info.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    forget about recursion, learn the basics of java, then hit recursion. You seem to me as a programmer who compiles and hopes rather than compiles and knows, strive for perfection in your understanding.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Norm View Post
    Please post the full text of the error messages. Your edited versions don't have enough info.

    are you really interested to see all 49 errors from the initial post?

  8. #8
    neno_os is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •