Results 1 to 6 of 6
  1. #1
    lch
    lch is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default Simple question: Alternating sum

    How would I write a program which takes an integer n and produces the output of the sum of the alternating series. So if I were to enter 5 it would say 3 because 1 - 2 + 3 - 4 + 5 = 3.
    I'm not sure where to start

    Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Simple question: Alternating sum

    How do you do it without a computer, with a pencil and a piece of paper? Work through a few examples until you see a pattern.

    Pretend you have a really dumb friend who has no idea how to do this. Write instructions (in plain language, not code) that your friend could follow to achieve the task. Remember how dumb he is, so make the instructions really short and really simple. When you have that written out, you'll have an algorithm that you can start thinking about implementing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Simple question: Alternating sum

    Well, first you need to know how to sum up a series of integers. Then, once you have that, you need to think about how to alternate the sign of every other one, starting with the second.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,527
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple question: Alternating sum

    You can also think a bit before you start writing loops and sums ... look at your series: (1-2)+(3-4)+(5-6)+(7-8) ... == -1 + -1 + -1 + -1 ... if the largest number is even, the result is -n/2, otherwise the result is -n/2+n. We don't need no steenkin' loops ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Simple question: Alternating sum

    Quote Originally Posted by JosAH View Post
    We don't need no steenkin' loops
    Ah, another addition to my collection of European cognates. :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,527
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple question: Alternating sum

    Quote Originally Posted by jim829 View Post
    Ah, another addition to my collection of European cognates. :)
    We don't need no steenkin' cognates either; vague acquaintances at most ...

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Using recursion to print alternating squares?
    By cheezncrackers in forum New To Java
    Replies: 2
    Last Post: 03-09-2012, 09:26 AM
  2. alternating font color
    By droidus in forum New To Java
    Replies: 2
    Last Post: 12-14-2011, 04:46 PM
  3. alternating series sum java help
    By java157 in forum New To Java
    Replies: 18
    Last Post: 03-20-2011, 03:41 AM
  4. Alternating Sum
    By ScaryJello in forum New To Java
    Replies: 6
    Last Post: 10-13-2009, 09:18 AM
  5. strange alternating array
    By jarvis in forum New To Java
    Replies: 2
    Last Post: 04-23-2009, 09:42 AM

Posting Permissions

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