Results 1 to 10 of 10

Thread: SumofArray

  1. #1
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Smile 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #4
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Default

    Oh, i see.. do know how to convert this code into a recursion?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Mika is offline Member
    Join Date
    Dec 2008
    Location
    Davao Oriental
    Posts
    29
    Rep Power
    0

    Default

    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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by CJSLMAN View Post

    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
  •