CurrentSum Program

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
May 2005
Location
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...
*
* @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 08:53 AM.

2. Senior Member
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. Registered User
Join Date
May 2005
Location
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.
*
*
*
* @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. Registered User
Join Date
May 2005
Location
Posts
173
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. Senior Member
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];

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

6. Registered User
Join Date
May 2005
Location
Posts
173

## I missled you!

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.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise