Thread: help with lists

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    Question help with lists

    Below is a program ive written to test if a number entered is a perfect number. However im having problems with making another function that will lists the perfect numbers from 1 to 1000. Any help or hints will be appreciated. Thanks

    Code:
    #include <stdio.h>
    
    int factors( int );
    int perfect( int );
    
    int main()
    {
    	int number;
    
    	printf("Enter Number: " );
    	scanf( "%d", &number );
    
    	printf( "%d\n", perfect( number ) );
    
    	return 0;
    }
    
    int factors( number )
    {
    	int counter = 1, counter2 = 0, newNumber;
    
    	while ( counter <= number ) {
    		if ( ( number % counter == 0 ) && ( number != counter ) ) {
    			newNumber = counter;
    			counter2 += newNumber;
    		}
    		counter++;
    	}
    
    	return counter2;
    }
    
    
    int perfect( number )
    {
    	if ( number == factors( number ))
    		return 1;
    	else
    		return 0;
    }
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well you're going to have to use a loop. It will probably look something like this:

    Code:
    /* Headers and forward declarations */
    #define MAX_NUM 1000
    
    int main()
    {
      int i;
    
      for( i = 0; i < MAX_NUM; ++i )
      {
        if( perfect( i ) )
        {
          printf( "%d\n", i );
        }
      }
    
      return 0;
    }
    I think something like that will probably do it. I think you should be able to tell what is going on in the loop as it is fairly straight-forward. If you need help understanding it let me know. Basically we will execute the commands within the for loop braces until the middle condition ( i < MAX_NUM ) is false.
    Last edited by MrWizard; 01-28-2003 at 03:55 AM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    Ive implemented what you have said and understand what is going on but i think im still missing something, you have declared int number, but it's not doing anything..
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by kurz7
    Ive implemented what you have said and understand what is going on but i think im still missing something, you have declared int number, but it's not doing anything..
    Yeah you're right, I just copied and pasted your code for a starting point. You used number but I didn't. Take it out, you'll be fine. It should still work fine?

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    hmm not sure why its not working.
    Just thinking about the code, will the counter always restart to 0 when the new number is called? Might keep incrementing so it will probably do 1 % 1, then 2 % 2 ...

    i cuold be wrong
    there are only 10 people in the world, those who know binary and those who dont

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I didn't look at your perfect number algorithm at all. I was just showing you how to setup a for loop and use it. The loop index i should not be the problem. If you can't fix it by tomorrow let me know.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    ive fixed the code to generate perfect numbers from 1 to 1000 but it still doesnt seem to be working.. not sure what the problem is, could be the counters.

    Code:
    #include <stdio.h>
    #define MAX_NUM 1000
    
    int factors( int );
    int perfect( int );
    
    int main()
    {
      int i;
    
      for( i = 0; i < MAX_NUM; ++i )
      {
        if( perfect( i ) )
        {
          printf( "%d\n", i );
        }
      }
    
      return 0;
    }
    
    int factors( number )
    {
            int counter = 1, counter2 = 0, newNumber;
    
            while ( counter <= number ) {
                    if ( ( number % counter == 0 ) && ( number != counter ) ) {
                            newNumber = counter;
                            counter2 += newNumber;
                    }
                    counter++;
            }
    
            return counter2;
    }
    
    
    int perfect( number )
    {
            if ( number == factors( number ))
                    return 1;
            else
                    return 0;
    }
    there are only 10 people in the world, those who know binary and those who dont

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Program works fine for me. Start the loop counter at 1 instead of 0 because it reports 0 as a perfect number when in fact it is not. Again, program works for me.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    hmm still doesnt seem to work, there should be 3 perfect numbers between 1 and 1000, did u get all 3? 6, 28, and another one ive forgotten..

    it doesnt output anything
    there are only 10 people in the world, those who know binary and those who dont

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Output:
    0
    6
    28
    496
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    still doesnt work
    is it a compiler problem? im on mandrake 8.2 and just using the gcc commands.
    there are only 10 people in the world, those who know binary and those who dont

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by kurz7
    still doesnt work
    is it a compiler problem? im on mandrake 8.2 and just using the gcc commands.
    Put some extra printf() statements in there to help you debug it. Also, try fflush(stdout); at the end of the program (shouldn't be necessary but you never know )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM
  2. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM