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,738 people online right now. Registration is fast and FREE... Join Now!




C++ Prgramming

 
Reply to this topicStart new topic

C++ Prgramming, Problems need HELP pls! thanx!

Vimrod
1 Apr, 2008 - 12:43 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;
}



I am not sure what i have done wrong. The program is meant to scan an interval [a, b] and return the smallest number that is the closest to its sum of divisors and it does not compile.
User is offlineProfile CardPM
+Quote Post

realNoName
RE: C++ Prgramming
1 Apr, 2008 - 01:33 AM
Post #2

D.I.C Regular
***

Joined: 4 Dec, 2006
Posts: 299



Thanked: 5 times
My Contributions
the 2 reasons its not compiling is because you are trying to return an int in your sum function but you have it prototyped as void and the other reason is you are trying to cout the min variable but its not defined anywhere
User is offlineProfile 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