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  Reply With Quote

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  Reply With Quote

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  Reply With Quote

4. Member Join Date
Mar 2009
Posts
6
Rep Power
0 yep, see my profile and i've my msn on it  Reply With Quote

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  Reply With Quote

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).
... so this is my first program im stuck on i have no idea where to start...
No it's not, it's the second one (at least).

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:
Java Code:
total = total + sumData[i]:
Then figure out how to determine which numbers are even and which are odd (the instructions are given).

Luck,
CJSL  Reply With Quote

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];

}  Reply With Quote

8. 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 [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Java Code:
[cod&#101;]
// your code goes here
// notice how the top and bottom tags are different
[/cod&#101;]  Reply With Quote

9. OK... you're on your way...

Good start... some comments:
also totalSum+=sumData[index] is the same as totalSum=totalSum+sumData[index] right? :
Yes, that is correct
Java Code:
evenSums = sumData [index];
oddSum = sumData [index];
There is no need to do the above, just declare your odd and even variable before the loop.
Java Code:
int evenSums = 0;
int oddSum = 0;
Try your program with the above suggestions.

Luck,
CJSL  Reply With Quote Posting Permissions

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