Thread: need help with this function

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    1

    need help with this function

    so I have tried a lot of different things to try to get these functions to work but have not been able to get them working together. The problem is to output armstrong numbers within a certain range selected by the user. please help

    Code:
    bool IsArmstrongNumber(int x)
    //-----------------------------------------------------
    {
    
    
        int NumberOfDigits(int x);
        int DthDigitOfX(int x,int d);
        int sum,d;
    int n = NumberOfDigits(x);
        for(d=0;d<=n;d++)
        {
            x=n%10;
            sum+=DthDigitOfX(x,d);
            n=n/10;
        }
    
        if (x==sum)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    
    
    
    
    
    }
    Code:
    int DthDigitOfX(int x,int d)
    Code:
    //-----------------------------------------------------
    {
    /*
    Precondition: (x >= 0)
    Precondition: (1 <= d <= 10) 
    
    
    d is interpreted as follows
    
    
        1
    d = 0,987,654,321
    x = X,XXX,XXX,XXX
    
    
    For example, if x = 153, then
       d = 1 returns 3, 
       d = 2 returns 5,
       d = 3 returns 1, and
       d > 3 returns 0. 
    */
    

  2. #2
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Could you show the rest of your code? The two functions NumberOfDigits(...) and DthDigitOfX(...). Also, why are you declaring them inside the IsArmstrongNumber(...) function?
    Code:
     int NumberOfDigits(int x);
     int DthDigitOfX(int x,int d);
    I don't think I've ever seen a function declared inside another function like that before. I believe nested functions aren't part of the C standard but are available as a language extension in GCC, Nested Functions - Using the GNU Compiler Collection (GCC)

    I don't really see those functions in IsArmstrongNumber, just that you declare them for some reason in there.
    Last edited by Spork Schivago; 09-07-2015 at 04:11 PM. Reason: Gramatical correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  2. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  3. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM