Results 1 to 9 of 9
Thread: SumElements
- 03-05-2009, 05:13 PM #1
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
SumElements
This is my assignment:
Sum the Elements
Complete the following program, called SumElements, so that it computes the sum of all the elements of the array, the sum of the even elements, and the sum of the odd elements. Assume that all the numbers are zero or positive. Even integers can be determined by dividing each number by two. If the remainder is 0, then the integer must be even (eg. if number%2 = 0, it must be even).
I'm falling behind and need to catch up in my computer science online course, so this is my first program im stuck on i have no idea where to start, this is the outline that i was given to complete it with:
// The "SumElements" class.
import java.awt.*;
import hsa.Console;
public class SumElements
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
int[] sumData = {3, 2, 5, 7, 9, 12, 97, 24, 54};
//declare and initialize three sums
//compute the sums of all the elements
int totalSum = 0;
for (int index = 0 ; index < sumData.length ; index++)
{
totalSum += sumData [index];
c.println (totalSum);
//write out the three sums
}
} // main method
} // SumElements class
- 03-05-2009, 07:04 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
when do the evensum and odd sum, just add a if-else statement after you do the totalSum
- 03-06-2009, 02:07 AM #3
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
thanks, do you or anyone else have msn so you can help me correct my code for a few programs? it would save me alot a time and i wont mind learning a few things from someone with experience
- 03-06-2009, 02:45 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
yep, see my profile and i've my msn on it
- 03-06-2009, 02:56 AM #5
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
OK thanks i added you to msn.....here is what i now have but the adding of the even and odds are wrong but its close, im not sure how to change the code to say add all odd elements from sumData array in my if statements:
public static void main (String[] args)
{
c = new Console ();
int[] sumData = {3, 2, 5, 7, 9, 12, 97, 24, 54};
//declare and initialize three sums
int totalSum = 0;
int evenSums = 0;
int oddSum = 0;
for (int index = 0 ; index < sumData.length ; index++)
{
//compute the sums of all the elements
totalSum += sumData [index];
//compute the sums of all the even elements
evenSums = sumData [index];
oddSum = sumData [index];
if (evenSums % 2 == 0)
{
evenSums += sumData.length;
}
//compute the sums of all the odd elements
else
{
oddSum += sumData [index];
}
}
//write out the three sums
c.println ();
c.println ("The sum of all the numbers in the array is " + totalSum + ".");
c.println ("The sum of all the EVEN numbers in the array is " + evenSums + ".");
c.println ("The sum of all the ODD numbers in the array is " + oddSum + ".");
} // main method
- 03-06-2009, 02:59 AM #6
Bob... this isn't a "Code-R-Us" ... we can help you with specific questions, but please come here to dump your homework on us (this is the second one I've seen).
No it's not, it's the second one (at least).... so this is my first program im stuck on i have no idea where to start...
Do your program part by part. First figure out how to get the sum of all the elements of the array. To do this you have to spin thru the array with a "for" loop and add up the elements as you go:
Then figure out how to determine which numbers are even and which are odd (the instructions are given).Java Code:total = total + sumData[i]:
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-06-2009, 03:10 AM #7
Member
- Join Date
- Dec 2008
- Location
- North America
- Posts
- 29
- Rep Power
- 0
i understand and i meant for this semester its the first one im stuck on....heres my code now and i got the total sums to print out correct and now im using the if statement and else to get the even and odd which works but i can't seem to get the code right to add them up properly, also totalSum+=sumData[index] is the same as totalSum=totalSum+sumData[index] right? :
for (int index = 0 ; index < sumData.length ; index++)
{
//compute the sums of all the elements
totalSum = totalSum + sumData [index];
//compute the sums of all the even elements
evenSums = sumData [index];
oddSum = sumData [index];
if (evenSums % 2 == 0)
{
evenSums = evenSums + sumData [index];
}
//compute the sums of all the odd elements
else
{
oddSum = oddSum + sumData [index];
}
-
When posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 03-06-2009, 12:28 PM #9
OK... you're on your way...
Good start... some comments:
Yes, that is correctalso totalSum+=sumData[index] is the same as totalSum=totalSum+sumData[index] right? :
There is no need to do the above, just declare your odd and even variable before the loop.Java Code:evenSums = sumData [index]; oddSum = sumData [index];
Try your program with the above suggestions.Java Code:int evenSums = 0; int oddSum = 0;
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks