Results 1 to 10 of 10
Thread: Adding numbers in an array?
- 01-21-2009, 06:56 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
Adding numbers in an array?
Hello. I have this problem. Here is the code first
class SalesAnalysis
{
private String [] months = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
private int [] sales2005 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
private int [] sales2006 = {0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
public void getTotal (int[])
{
}
Question: This method [getTotal (int[]) ]will total all the values in an array. A parameter to this method is a reference to an array of integers.
The method will return the total of the values in the referenced array.
I think the code may be something like this:
for (int i = 0; i < sales2005.length; i++)
System.out.print(sales2005[i])
Any ideas? I am stumped. I have an exam this week.
-
if you must total the ints in the array, you need an int variable declared before your loop, and then add the array item's values to this variable within the loop as you loop through your array. When the for loop is done, there's your total. Make sense?
- 01-21-2009, 07:43 PM #3
Somehting like this?
the call to getTotal would be something like this:
int 2005Total=0;
2005Total = getTotal(sales2005);
The pseudo code for the getTotal method would look something like:
Did this help?Java Code:public int getTotal (int[]) { loop through the elements of the array, total = total + int[i] end loop return total }
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
The only thing I'd add to CJSL's excellent post is to be sure to declare total before the loop.
- 01-21-2009, 07:56 PM #5
yeah...
Yeah.... I thought about that, but it's pseudo code... I was hoping the OP would figure that out... don't want to give everything away :)
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-21-2009, 08:33 PM #6
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
arrrrghhh. my brain is fried. i cant get it. =(
- 01-21-2009, 11:47 PM #7
uh.... that's hard to believe...
If you came up with the code in your first post, then you surely can figure out what Fubarable and myself suggested.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-22-2009, 12:00 AM #8
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
Arrays and adding: (Some progress)
public class TestSalesa
{
public static void main (String [] args)
{
int i = 0;
int sum = 0;
String [] months = {"", "Jan", "Feb"};
int[] sales2005 = {0, 20, 30};
int[] sales2006 = {0, 40, 50};
getTotal(sales2005);
}
static void getTotal(int [] sales2005)
{
int sum = 0;
for (int i = 0; i < sales2005.length; i++)
sum = sum + sales2005[i];
System.out.print(sum);
}
}
I worked this out. but i cant write a driver to call the getTotal. arrgh it wont work.
- 01-22-2009, 12:05 AM #9
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
i dont understand your int 2005Total = 0;
The second line helped me with the code for the single file java programme. but the example on the exam paper is written like as if you have to write a driver to call the getTotal. trying to get the objects to cooperate is frustrating.
the exam question asks you just to write the method in the SalesAnalysis file. but like i want to practice oop. i want to have a crack at writing the driver for practice. but i cant get it. god i will never make a programmer. lol.
- 01-22-2009, 03:50 AM #10
It works fine for me....
Your "driver" is a method. You call a method. Now what is the problem? It works perfectly for me. I didn't change a thing. it prints 50 on the command line.
Do you get an error?
What did you expect it to do?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
finding the largest k numbers in an array without sorting the whole list?
By jmmjm in forum Advanced JavaReplies: 5Last Post: 02-07-2009, 07:48 AM -
Adding numbers in a 2 dimensional array
By j0shizabeast in forum New To JavaReplies: 2Last Post: 11-27-2007, 04:31 AM -
Adding numbers in array
By Shaolin in forum New To JavaReplies: 1Last Post: 11-15-2007, 06:30 PM -
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08-14-2007, 03:44 AM -
Adding graphics to array
By romina in forum Java 2DReplies: 1Last Post: 08-01-2007, 01:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks