1. Member
Join Date
Jun 2010
Posts
3
Rep Power
0

## 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 01:34 PM.

2. 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 01:19 PM.

3. Member
Join Date
Jun 2010
Posts
3
Rep Power
0
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. 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.

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

6. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
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. 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?

8. Member
Join Date
Jun 2010
Posts
3
Rep Power
0
Thank you all for your help. I solved my problem.

#### Posting Permissions

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