C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old March 25th, 2003, 07:31 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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...

Reply With Quote
  #2  
Old March 25th, 2003, 08:00 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
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.

Reply With Quote
  #3  
Old March 25th, 2003, 08:08 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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

Reply With Quote
  #4  
Old March 25th, 2003, 08:10 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
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.

Reply With Quote
  #5  
Old March 25th, 2003, 08:22 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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.

Reply With Quote
  #6  
Old March 25th, 2003, 08:24 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
Hehe, check out my corrected formula.

Reply With Quote
  #7  
Old March 25th, 2003, 08:26 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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

Reply With Quote
  #8  
Old March 25th, 2003, 08:28 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
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.

Reply With Quote
  #9  
Old March 25th, 2003, 08:38 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
Doh! Im stuck on negatives..lol. BRB to try your method out.

Reply With Quote
  #10  
Old March 25th, 2003, 08:41 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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.

Reply With Quote
  #11  
Old March 25th, 2003, 09:51 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
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.

Reply With Quote
  #12  
Old March 26th, 2003, 06:42 AM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
Thanks alot man. Take it easy.

-andy

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > summing #ers and differencing #ers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump