-
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 08:53 AM.
-
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)
-
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) + "." );
}
}
-
 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,
-
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));
}
-
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
-
By divagoddess in forum C++
Replies: 5
Last Post: 08-14-2009, 03:12 PM
-
Replies: 1
Last Post: 02-18-2007, 07:43 PM
-
By sedricbenson@ho in forum C++
Replies: 2
Last Post: 11-07-2006, 07:58 AM
-
By Gordon Reichhardt in forum VB Classic
Replies: 2
Last Post: 01-08-2002, 10:06 AM
-
By W.Pierce in forum VB Classic
Replies: 1
Last Post: 12-11-2001, 08: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks