Thread: Help accumilative variables

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    83

    Help accumilative variables

    Ok I'm tryin to add numbers together but just add the total of negative and then the total of positive numbers.

    Code:
    value = -2.3;
      while (value <= 2.9) {
        cout << value << " ";
        value = value + .4;
    Now after this I'm tryin to just add the negative and then the positive but I am not sure. I will have the negative variable as neg and the positive variable as pos. I am not sure how to make it only read the neg/pos numbers. I know that greater or lesser but just don't know how to set it up.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    I know everytime thru the loop add the next value to the accumulator variable, switching to the other accumulator variable after you cross 0, but not sure how to write this?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    could someone help me?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This would seem to be what if-statements were born to do (if positive do this otherwise do that).

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Quote Originally Posted by tabstop View Post
    This would seem to be what if-statements were born to do (if positive do this otherwise do that).
    I know it is an if statement but don't know how to write it. Could you help me?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    And also I am very very new to this.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by walkonwater View Post
    I know it is an if statement but don't know how to write it. Could you help me?
    well you have the logic of the code down correct and you know what you need to do. simply write it out on paper in pseudo code first, post what you come up with and we'll take it from there. A little effort on your part goes a long way.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    the thing I don't know is if I have to create a new int.

  9. #9
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    You need to break it down into logical chunks of what needs to happen then put those chunks together using if statements.

    hint:
    you've assigned value as -2.3 so that will need to be looped until it hits 0 then you need to do the other loop to it.

    Refer to your source on using if statements. Your best bet is to write as much as you can and then post it, you'll get more help that way

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    Code:
    if (0 < value)
                cout << I am not sure how to add it right here
    I am not even sure if this is right. I would just think you do this.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by walkonwater View Post
    Code:
    if (0 < value)
                cout << I am not sure how to add it right here
    I am not even sure if this is right. I would just think you do this.
    The cout will happen if the statement in parentheses is true, i.e., if 0 < value. I leave it to you to determine (a) if 0 < value, whether value is positive or negative and then (b) how to use = and + in some order to make the addition happen.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    well I know this:

    Code:
    if (0 < value)
    cout << not sure what to put here.
    else
    cout << not sure what to put here

  13. #13
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Try putting your original code in there.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    83
    value = -2.3;
    while (value <= 2.9) {
    cout << value << " ";
    value = value + .4;
    if (0 < value)
    cout << not sure what to do
    else
    cout << not sure what to do
    I am not sure if above is right however

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you can add to value (as evidenced by the line "value = value + .4") why can't you add to pos and neg?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM