# SumofArray

• 12-17-2008, 05:05 AM
Mika
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.
• 12-17-2008, 05:14 AM
Fubarable
You may want to first fix your current code and only post code that will compile. Best of luck.
• 12-17-2008, 05:15 AM
Eranga
You cannot use the same variable name sum twice in the same method as a local method.

This is not valid.
Code:

``` int sum, x; double sum=0.0;```
• 12-17-2008, 05:31 AM
Mika
Oh, i see.. do know how to convert this code into a recursion?
• 12-17-2008, 05:34 AM
Fubarable
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?
• 12-17-2008, 05:34 AM
Eranga
What you are going to do with recursion.
• 12-17-2008, 05:47 AM
Mika
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?
• 12-17-2008, 05:49 AM
Fubarable
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.
• 12-17-2008, 04:02 PM
CJSLMAN
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?:
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
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
• 12-18-2008, 04:35 AM
Eranga
Quote:

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.