Results 1 to 9 of 9

Thread: SumElements

  1. #1
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Unhappy 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

  2. #2
    bigbigbug is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    when do the evensum and odd sum, just add a if-else statement after you do the totalSum

  3. #3
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

    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

  4. #4
    bigbigbug is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    yep, see my profile and i've my msn on it

  5. #5
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

    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

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Default

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

    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

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

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Posting Permissions

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