looking for a function:

This is a discussion on looking for a function: within the C Programming forums, part of the General Programming Boards category; Anyone know where i might find a Perfect number function I could add to my program that basically will take ...

  1. #1
    Unregistered
    Guest

    Question looking for a function:

    Anyone know where i might find a Perfect number function I could add to my program that basically will take an integer an return true or false?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Hmm...lemme see:

    You're a programmer - right?

    You write programs - right?

    So how about just "writing" one instead of "looking" for one...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Unregistered
    Guest

    Question well...

    I wouldn't call myself a programmer. I more of a piece together type. I never claimed to be a programmer. just looking for a function to put into another program i am editing. I'm still in the early stages of reading code and getting things to work.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: looking for a function:

    Originally posted by Unregistered
    Anyone know where i might find a Perfect number function I could add to my program that basically will take an integer an return true or false?
    Code:
    #define TRUE 1
    #define FALSE 0
    
    int MyFunc(int i)
    {
        return (TRUE);  /* or FALSE, if you want */
    }
    Well, thats what you asked for, but it doesn't do very much
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well start with what you are trying to do.

    Write out, in plain english, how to determine if a number if perfect or not.

    Then try and abstract that into pseudocode (google that if you need to know what it is)

    Once you have made a legitimate attempt to do that, and come up against a wall, post what you have and your quandry.




    If you are just looking for an algorithm, instead of the knowledge of how to make that algorithm, then you are on the wrong board. Try sourceforge.

  6. #6
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    nice code hammer i just love what it does i'm pretty sure he means something more like this:


    Code:
    #define true   1
    #define false  0
    int myfunc (number)
    {
     if(scanf("%d",&number)==1)
       return true;
     else
       return false;
    }
    well it at least takes in a number

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    A number is perfect if it is equal to the sum of its divisors, like so:

    6 is perfect: 6 = 1 + 2 + 3.
    28 is perfect: 28 = 1 + 2 + 4 + 7 + 14.

    How do you find a perfect number? Simply find all of its divisors and add them together, if the two numbers match then it's perfect. This sounds like homework and is simple when you understand the problem, so I'll leave the coding part up to you.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    I don't mean to bump this unnecessarily (sorry to offend anyone) but I have just been bitten by this problem and got an answer!

    What I was wondering is is this good code?

    Code:
    #include <stdio.h>
    
    void main()
    {
    int i, integer[10000], array[10000], total=0;
    float temp[10000], orig, result[10000];
    char answer;
    
    while (answer !='n')
    {
    printf("\nEnter a number:\n");
    scanf("%f", &orig);
    
    total=0;
    
    for(i=0;i<10000;i++)
    {
    	temp[i]=0;
    	array[i]=0;
    	integer[i]=0;
    	result[i]=0;
    }
    
    for(i=1;i<orig;i++)
    {
    temp[i]=orig/i;
    integer[i]=orig/i;
    }
    
    for(i=1;i<orig;i++)
    {
    	result[i]=temp[i]-integer[i];
    	}
    
    
    for(i=1;i<orig;i++)
    {
    	if(result[i]>0)
    		array[i]=0;
    	else
    		array[i]=1;
    
    }
    
    for(i=1;i<orig;i++)
    {
    	if(array[i] !=0)
    total=total+i;
    
    }
    
    
    if(total==orig)
    printf("\nThe number %.0f is PERFECT!!\n", orig);
    else
    printf("\nThe number %.0f is not perfect, sorry!\n", orig);
    
    flushall();
    printf("\nAgain? (y/n)\n");
    scanf("%c", &answer);
    }
    }
    Thanks

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK....

    >void main()
    Nope, not good. Use int main(void)

    >char answer;
    >while (answer !='n')
    You defined answer, then tested it's value. How did you know it was or wasn't already n? I don't believe this type of variable gets initialised to a default value, you'll get whatever is in that memory location when the program starts.

    >scanf("%f", &orig);
    Not good. scanf() is pretty poor at reading user data, and if you are going to use it, you must check it's return code to see what it actually did for you.

    >for(i=1;i<orig;i++)
    i is an int, orig is a float. Not sure of the consequences without going through what your code is really doing, but normally you wouldn't compare different variable types in a loop like this if you could help it.

    >missing return(0);
    main() should return something to the OS. Add a return statement at the end.

    That's a few pointers for you. I've only looked at the syntax though, not the maths involved (I can't do maths at @ 2am!) Maybe someone else will advise you on that front.

    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. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21