Results 1 to 10 of 10
Thread: SumofArray
- 12-17-2008, 04:05 AM #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.
-
You may want to first fix your current code and only post code that will compile. Best of luck.
- 12-17-2008, 04:15 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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;
- 12-17-2008, 04:31 AM #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?
-
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, 04:34 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you are going to do with recursion.
- 12-17-2008, 04:47 AM #7
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?
-
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, 03:02 PM #9
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-18-2008, 03:35 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks