Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,912 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,735 people online right now. Registration is fast and FREE... Join Now!




Need HELP!

 
Reply to this topicStart new topic

Need HELP!, C++ Programming

Vimrod
2 Apr, 2008 - 01:34 AM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2008
Posts: 20

[code]
#include <iostream>
using namespace std;

void sum( int n );

int main()
{
int a, b;
cout << "Enter 2 positive integers a <= b: " << flush;
cin >> a >> b;

int c = b;
int d = a;
int tmp;
for( int n = a; n <= b; n++ )
{
tmp = n - sum(n);
if( tmp < 0 )
tmp = -tmp;
if( tmp <= c )
tmp = c;
d = n;
}
cout << "The distance is minimal for " << d << " and is equal to " << min << endl;

system("pause");
return 0;
}

void sum( int &n )
{
int s = 1;
// if d is a divisor of n so is n/d avoid to search till n
for( int d = 2; d*d <= n; d++ )
if( n%d == 0 )
s += d + n/d;
return s;
}

[\code]
The program is meant to scan an interval [a, b] and return the smallest number that is the closest to its sum of divisors. I've been editing it for ages but still can't see where my mistakes are. Can someone help me please! thanx in advance!


User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Need HELP!
2 Apr, 2008 - 01:37 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,495



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You define
CODE

void sum( int n );


But yet, you use it
CODE

void sum( int &n )
{


Also, you declare it void, yet you return a value from it. If it's going to return a value it must be declared as such.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:01PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month