|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
||||
|
||||
|
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 ![]()
__________________
hmmm... |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
||||
|
||||
|
Hehe, check out my corrected formula.
|
|
#7
|
||||
|
||||
|
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 |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
||||
|
||||
|
Doh! Im stuck on negatives..lol. BRB to try your method out.
|
|
#10
|
||||
|
||||
|
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. |
|
#11
|
||||
|
||||
|
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. |
|
#12
|
||||
|
||||
|
Thanks alot man. Take it easy.
-andy |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > summing #ers and differencing #ers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|