# SumElements

• 03-05-2009, 06:13 PM
bobmasta5
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, 08:04 PM
bigbigbug
when do the evensum and odd sum, just add a if-else statement after you do the totalSum
• 03-06-2009, 03:07 AM
bobmasta5
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, 03:45 AM
bigbigbug
yep, see my profile and i've my msn on it
• 03-06-2009, 03:56 AM
bobmasta5
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, 03:59 AM
CJSLMAN
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).
Quote:

... 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:
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
• 03-06-2009, 04:10 AM
bobmasta5
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];

}
• 03-06-2009, 04:22 AM
Fubarable
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:

Code:

```[cod&#101;]   // your code goes here   // notice how the top and bottom tags are different [/cod&#101;]```
• 03-06-2009, 01:28 PM
CJSLMAN
Quote:

also totalSum+=sumData[index] is the same as totalSum=totalSum+sumData[index] right? :
Yes, that is correct
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.
Code:

```int evenSums = 0; int oddSum = 0;```
Try your program with the above suggestions.

Luck,
CJSL