# Help with rekurzion

• 06-04-2010, 12:22 PM
neno_os
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>
• 06-04-2010, 01:12 PM
j2me64
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.

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;         } }```
• 06-04-2010, 01:33 PM
neno_os
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.
• 06-04-2010, 10:03 PM
xcallmejudasx
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.
• 06-04-2010, 11:33 PM
Norm
Quote:

compiler sends me these errors
Please post the full text of the error messages. Your edited versions don't have enough info.
• 06-05-2010, 08:02 AM
al_Marshy_1981
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.
• 06-05-2010, 08:59 AM
j2me64
Quote:

Originally Posted by Norm
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?
• 06-05-2010, 11:24 AM
neno_os
Thank you all for your help. I solved my problem.