CurrentSum Program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: CurrentSum Program

  1. #1
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    CurrentSum Program

    Hello every one,
    Can anyone help me improve it?
    code:


    /**
    * Write a description of class CurrentSum here.
    * I am trying to get the sum of the sale of given number of months. Like the fist 2 months...
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    public class CurrentSumProgrma{
    static int CurrentSum(int[]A, int n){
    int CurrentSum = A[0];
    int k=5;
    if (k<2)
    return CurrentSum;
    for (int i=1; i<k; i++)
    CurrentSum += A[i];
    return CurrentSum;
    }
    public static void main (String args[]){
    int[] num={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n=num.length;
    System.out.println ( " The Sum of " + k + " element is" + CurrentSum + "." );
    }
    }
    /code
    Last edited by Kinda Electroni; 05-16-2007 at 09:53 AM.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Improve it or get it to output? Remember that your call to CurrentSum, whereever it occurs, must use parameters. ALSO, you cannot use the same name for both your static method and one of its fields (the compiler won't let you confuse it in this way). Your use of "k" in your System.out statement will return a "variable not defined" error - main doesn't know what "k" is.

    Are you trying to make this recursive? (exponential is an easy problem to implement using recursion)

  3. #3
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    No one would !!

    /**
    * Write a description of class CurrentSum here.
    * I am trying to get the sum of the sale of given number of months. Like the fist 2 months...
    *
    * Results:
    * The Sum of 1 element is 1.
    *The Sum of 2 element is 3.
    * The Sum of 3 element is 6.
    * The Sum of 4 element is 10.
    *The Sum of 5 element is 15.
    *The Sum of 5 element is 15.
    *
    *
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    public class CurrentSumProgrma{
    static int CurrentSum(int[]A, int n){
    int CurrentSum = A[0]+A[1];
    int k=5;
    if (k<2)
    //CurrentSum += A[1];
    return CurrentSum;
    for (int i=2; i<(k+1); i++)
    CurrentSum += A[i];
    return CurrentSum;
    }
    public static void main (String args[]){
    int[] num={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n=num.length;
    //System.out.println ( " The Sum of " + k + " element is " + CurrentSum(num,n) + "." );
    System.out.println ( " The Sum of " + 5 + " element is " + CurrentSum(num,n) + "." );
    }
    }

  4. #4
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Quote Originally Posted by nspils
    Improve it or get it to output? Remember that your call to CurrentSum, whereever it occurs, must use parameters. ALSO, you cannot use the same name for both your static method and one of its fields (the compiler won't let you confuse it in this way). Your use of "k" in your System.out statement will return a "variable not defined" error - main doesn't know what "k" is.

    Are you trying to make this recursive? (exponential is an easy problem to implement using recursion)
    Sorry, looks like we are posting at the same time.
    Yes, I would like to use a recursive. How?
    I want the user to input the number of the months that he wants me the sum. This is k?
    I do not know how to use it prob.
    Why you said:
    Remember that your call to CurrentSum, whereever it occurs, must use parameters.
    How can I?

    I did not get this one:
    you cannot use the same name for both your static method and one of its fields (the compiler won't let you confuse it in this way).
    Thanks,

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You have hard coded "k" in your CurrentSum method and never use the argument "n", which is what you've designed to be the "th" element of the array you are adding up to.

    your base case for this would be

    if (n == 1) return A[0];

    your recursive call is

    else
    {
    return (A[n-1] + CurrentSum(A, n-1));
    }

  6. #6
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    I missled you!

    Thank you for your respond,
    I have 12 months income:
    J = $1
    F = $2
    M = $3000
    ...
    the user will ask what is the sume of the income for J...M
    k=3 in this case.
    the sume will be 303..etc.

Similar Threads

  1. soft drink Vending machine program
    By divagoddess in forum C++
    Replies: 5
    Last Post: 08-14-2009, 04:12 PM
  2. help, dont know why program is too slow
    By bob2000 in forum Java
    Replies: 1
    Last Post: 02-18-2007, 08:43 PM
  3. Connecting to a Server Program HELP
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 08:58 AM
  4. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 11:06 AM
  5. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center