Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 6th, 2007, 9:20 PM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
If you want help on an error message, it's a good idea to give us the same information your compiler gave you (eg the text of the error message, the line number it highlightd as a problem).

In this case, the problem is when you're calling divisor_count.
          divisors = divisor_count(int x);
I've highlighted the int keyword here because it is the source of your problem. Remove it.
grumpy is offline   Reply With Quote
Old Jan 6th, 2007, 10:15 PM   #12
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 646
Rep Power: 4 Jessehk is on a distinguished road
You are also going to have to update the maximum number of divisors so far. In your current code, you are only updating the number with the maximum divisors.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jan 7th, 2007, 8:53 AM   #13
gamerfelipe
Newbie
 
Join Date: Jan 2007
Location: Hershey, the sweetest place on earth
Posts: 19
Rep Power: 0 gamerfelipe is on a distinguished road
Thanks Guys! Here we go:

#include <iostream.h>

int divisor_count (int n);

int main()
{    
     int x;
     int divisors;
     int max_divs = 0;
     int max_num = 0;
     int last_num = 1000;

      for (x = 1; x <= last_num; x++)
      { 
          divisors = divisor_count(x);
          if (divisors > max_divs)
          {
             max_num = x;
             max_divs = divisors;
          }
      }
      cout << "The number with the most divisors is " << max_num << endl;
      return 0;
} 

int divisor_count (int n)
{
    int count = 1;

    for ( int i = 2; i <= n; i++ )
    {
        if ( n % i == 0 )
            ++count;
    }
    return count;
}
gamerfelipe is offline   Reply With Quote
Old Jan 7th, 2007, 9:38 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Review post #6 regarding iostream.h. May as well get off the non-standard stuff now.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 7th, 2007, 7:34 PM   #15
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 882
Rep Power: 4 The Dark is on a distinguished road
    for ( int i = 2; i <= n; i++ )
You should use n/2 for the upper limit (as you did in your first post). Anything bigger than n/2 is not going to be a divisor of n.
The Dark is offline   Reply With Quote
Old Jan 7th, 2007, 8:22 PM   #16
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 646
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by The Dark View Post
    for ( int i = 2; i <= n; i++ )
You should use n/2 for the upper limit (as you did in your first post). Anything bigger than n/2 is not going to be a divisor of n.

Oops. :o
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jan 11th, 2007, 4:05 AM   #17
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
Quote:
You are also going to have to update the maximum number of divisors so far. In your current code, you are only updating the number with the maximum divisors.
Quote:
For one of my exercises I'm required to find the number between 1 - 1000 with the most divisors.
?????????????????????
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Jan 11th, 2007, 12:41 PM   #18
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 882
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by bl00dninja View Post
?????????????????????
Whats does that mean bl00dninja? If you look at the code Jessehk was referring to, it is trying to find the number with the most divisors by checking the number of divisors of this number with the maximum number found so far (max_divs). Jessehk was pointing out that the code did not update the max_divs variable value inside the loop, so it would not work (it would always return 1000).
The Dark is offline   Reply With Quote
Old Jan 12th, 2007, 2:07 AM   #19
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
ahhhhhhhhhhhhhhh...

my bad.

__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding Random Min and Max imagikricei C++ 18 Jun 12th, 2006 1:29 PM
Sum of Divisors titaniumdecoy Software Design and Algorithms 20 May 14th, 2006 4:24 PM
Finding Patterns jobobshishkabob C++ 24 Feb 26th, 2006 3:49 AM
finding the number of spaces in a String squishiful Java 6 Jul 6th, 2005 6:43 PM
Help with finding windows programming books some1 C++ 11 Feb 9th, 2005 10:19 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC