March 25th, 2003, 08:31 PM

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 432 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...
March 25th, 2003, 09:00 PM

Your formulae are wrong. By using this wellknown 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.
March 25th, 2003, 09:08 PM

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...
March 25th, 2003, 09:10 PM

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 09:22 PM.
March 25th, 2003, 09:22 PM

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? 432 = 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 09:24 PM.
hmmm...
March 25th, 2003, 09:24 PM

Hehe, check out my corrected formula.
March 25th, 2003, 09:26 PM

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...
March 25th, 2003, 09:28 PM

My formulae are correct for FG(), both first and second try are the same thing. BTW 432 = 1, not 9
if you put second = 4 and first = 2 and try out my formula, it works correctly.
March 25th, 2003, 09:38 PM

Doh! Im stuck on negatives..lol. BRB to try your method out.
hmmm...
March 25th, 2003, 09:41 PM

final = second + ((first  second)*(first + second  1)/2);
Output:
Type in A1
5
Type in An
3
543 = 10
hmm..
Last edited by andy3109; March 25th, 2003 at 09:51 PM.
hmmm...
March 25th, 2003, 10:51 PM

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 + ((35) * (3 + 5 1)/2)
= 5 + ((2) * (7)/2)
= 5 + (7)
= 2
543 = 2
Therefore my formula works correctly. The reason you're getting 10 is because you reversed the values of first and second.
March 26th, 2003, 07:42 AM

Thanks alot man. Take it easy.
andy
hmmm...