#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    summing #ers and differencing #ers


    My code below basically does this:

    The FL Function:
    - User types in one number (lets say 2)
    - User types in second number (lets say 4)
    - The computer caclulates and outputs what 4+3+2 is.

    The FG Function:
    - User types in one number (lets say 4)
    - User types in second number (lets say 2)
    - The computer caclulates and outputs what 4-3-2 is.

    The FG function works fine until I type something in like:
    First number: 4
    Second number: 3

    If they are one apart they don't come out with the right answer. I have narrowed this down to the n/2 which makes in all situations (where the first and second number are one apart) n/2 equal to one. I need a revise my formula but can't think of a way. Here is my code:


    #include <iostream>
    using namespace std;
    float fg();
    float fl();

    float first;
    float second;

    int main()
    {

    cout << "Type in A1\n";
    cin >> first;
    cout << "Type in An\n";
    cin >> second;

    if (first < second)
    {
    fl();
    }
    else if (first > second)
    {
    fg();
    }


    return 0;
    }

    float fl()
    {
    float n;
    float final;

    n = ((second + 1) - first);
    final = n/2 * (first + second);
    cout << "The sum between " << first << " and " << second << " is, "<< final << "\n";
    return 0;
    }

    float fg()
    {
    float n;
    float final;

    n = ((first + 1) - second);
    final = n/2 * (first + second);
    cout << "The difference between " << first << " and " << second << " is, -"<< final << "\n";
    return 0;
    }


    P.S. I realize I could do this easier with arrays..But I want to try it with a custom formula :D
    hmmm...
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Your formulae are wrong. By using this well-known formula (was it Gauss who proposed it, or Euler? I forget who it was):
    Sum(1..n) = n(n+1)/2

    I derived the formulae where the first number is not 1, to be (assuming second is larger than first, of course):

    FL = (second + first) (second - first + 1) / 2
    FG = (2 * second + (first - second)(first + second - 1))/2

    This works for all cases, whether the numbers are one apart or not. A discussion of how I derived these formulae is probably best discussed in the algorithms forum.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Hmm..my first formula works in all cases. My second is flawed and I will use yours although I don't understand it 100% yet. Thanks alot though for your quick reply. Regards.

    -andy
    hmmm...
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    That's because your FL() formula is the same as my FL() formula (rearrange the symbols and you'll see). However, your FG() formula is not the same as mine.

    [edit]
    BTW my first try at FG was wrong too.
    FG() = second + ((first - second)*(first + second - 1)/2)
    [/edit]
    Last edited by Scorpions4ever; March 25th, 2003 at 08:22 PM.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    hmm..your formula:
    final = (2 * second + (first - second) * (first + second - 1))/2;

    doesn't seem to work. Here is my results for a practice run:

    Type in A1
    4
    Type in An
    2
    The difference between 4 and 2 is, -7

    It should be -9? 4-3-2 = -9

    EDIT: Nope..your new formula:
    final = second + ((first - second)*(first + second - 1)/2);

    Still results in -7
    Last edited by andy3109; March 25th, 2003 at 08:24 PM.
    hmmm...
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Hehe, check out my corrected formula.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    fixed it..it should be:


    final = (2 * second + (first - second) * (first + second + 1))/2;

    Thanks for pointing me in the right direction again scorp.

    -andy
    hmmm...
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    My formulae are correct for FG(), both first and second try are the same thing. BTW 4-3-2 = -1, not -9

    if you put second = 4 and first = 2 and try out my formula, it works correctly.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Doh! Im stuck on negatives..lol. BRB to try your method out.
    hmmm...
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    final = second + ((first - second)*(first + second - 1)/2);

    Output:

    Type in A1
    5
    Type in An
    3
    5-4-3 = -10

    hmm..
    Last edited by andy3109; March 25th, 2003 at 08:51 PM.
    hmmm...
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    If you check the first reply in my thread, I said second was assumed to be the greater number in my formula. If you put second = 5, first = 3, then you get

    final = 5 + ((3-5) * (3 + 5- 1)/2)
    = 5 + ((-2) * (7)/2)
    = 5 + (-7)
    = -2

    5-4-3 = -2

    Therefore my formula works correctly. The reason you're getting -10 is because you reversed the values of first and second.
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Thanks alot man. Take it easy.

    -andy
    hmmm...

IMN logo majestic logo threadwatch logo seochat tools logo