1. Member
Join Date
Dec 2008
Location
Davao Oriental
Posts
29
Rep Power
0

## SumofArray

I have a problem regarding my program. It is difficult for me to convert for loop into a recursion function. Can you help me? Heres my program I Have done.It is to solve the all elements within an array. We use java programming languge.

import java.util.Scanner;
public class SumofArray {
static Scanner scan=new Scanner(System.in);
public static void main(String[]args) throws Exception {

int []val=new int [20];
int sum, x;
double sum=0.0;

for(int x=0;x<val.length;x++) {
System.out.println("Enter a number:");
int i=scan.nextInt();
sum=sum+i;
}
System.out.println(sum);
}
}

Your help is very well appreciated. Thank You.

2. You may want to first fix your current code and only post code that will compile. Best of luck.

3. You cannot use the same variable name sum twice in the same method as a local method.

This is not valid.
Java Code:
```int sum, x;
double sum=0.0;```

4. Member
Join Date
Dec 2008
Location
Davao Oriental
Posts
29
Rep Power
0
Oh, i see.. do know how to convert this code into a recursion?

5. You need to make a method in order to have something be recursive. So along this line, what would this method take as a parameter?

6. Member
Join Date
Dec 2008
Location
Davao Oriental
Posts
29
Rep Power
0
i have here a algorithm..
public static int array(int A, int n) {
if n=1 then
return A[0]
else return Sum(A,n-1)+A[n-1]
}

Is it correct? What should i put in a main method to receive this sum. Can you help me?

7. You're using "A" as if it were an array, but you've declared it as if it were an int. You may want to fix that. What you need to do is try to implement your algorithms, compile your code, and read the compiler errors that are returned to you. You will progress much faster with your studies if you learn to do it this way. Best of luck.

8. ## Head hurts...

Java Code:
```public static int array(int A, int n) {
if n=1 then
return A[0]
else return Sum(A,n-1)+A[n-1]
}```
• What is the pupose/objective of the program? What does it have to do?
• What is the name of the above method? "array" (not sure it's legal) is not a good name. Should it be fillArray? SumArray? Sum?
• What does this method have to do (I know it has to recurse), but what should it return?
• the "if" statement is wrong. Do you mean?:
Java Code:
`if (n==1)`
• unless you're showing pseudo code, "then" doesn't exist as a commnad in Java. Also, I don't see any ";".
• Always use curly brackets "{}" with "if", "else" and "for" statements (even with one-liners)
• The int A vs A[0] has already been commented by Fubarable
Java Code:
`Sum(A,n-1)+A[n-1]`
• The parentesis is messed up in the above example. I really don't know what you're trying to return here.

You obviously don't know what your doing. You don't understand the basics of programmming/Java. May I suggest the following: study and do a lot of exercises. Here's a good link to start with:
The Java™ Tutorials

Luck,
CJSL

9. Originally Posted by CJSLMAN

You obviously don't know what your doing. You don't understand the basics of programmming/Java. May I suggest the following: study and do a lot of exercises. Here's a good link to start with:
The Java™ Tutorials

Luck,
CJSL

Exactly. Seems to me he's mess up with different things, arrays and recursion. He wants to find the sum of all elements of an array. But what's going to do with recursion.

#### Posting Permissions

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