WHAT IS THE ERROR IN THIS PROGRAM.(plz dont suggest any new program,only modificatio)

This is a discussion on WHAT IS THE ERROR IN THIS PROGRAM.(plz dont suggest any new program,only modificatio) within the C Programming forums, part of the General Programming Boards category; Code: /*PROGRAM TO PRINT ARMSTRONG NUMBERS FROM 1 TO 1500*/ #include<stdio.h> #define boundary 1500 *FUNCTION TO FIND X^N*/ int raise(int ...

  1. #1
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Unhappy WHAT IS THE ERROR IN THIS PROGRAM.(plz dont suggest any new program,only modificatio)

    Code:
    /*PROGRAM TO PRINT ARMSTRONG NUMBERS FROM 1 TO 1500*/
    #include<stdio.h>
    #define boundary 1500
    
    *FUNCTION TO FIND X^N*/
    
    int raise(int x,int n)
    {if(n==1)
    return x;
    return x*raise(x,n-1);
    }
    /*FUNCTION TO ADD DIGITS*/
    
    long int sum(int n,int noofdigit)
    {	int temp;
    	static long int sum1=0;
    	
    	sum1+=raise(n%10,noofdigit);
    	if(n/10!=0)
    	sum(n/10,noofdigit);
    	
    	else return sum1;
    }
    /*FUNCTION TO FIND NUMBER OF DIGITS*/
    int digitn(int n)
    	{int i;
    	for(i=1;n/10!=0;++i)
    		n/=10;
    	return i;
    	}
    /*FUNCTION FOR CHECKING WHETHER NUMBER IS ARMSTRONG*/
    void armstrong()
    {
    	int i,a,digits;
    	for(i=0;i<=boundary;++i)
    		
    		if(sum(i,digitn(i))==i)
    		printf("%d ",i);
    		
    	
    	printf("\n");
    
    }
    main()
    {
    printf("ARMSTRONG NUMBERS IN FIRST 1500 NUMBERS ARE: ");
    armstrong();
    
    
    }
    /*why i'm getting only 1 and 0?
    I'm working in linux not TC
    */
    Last edited by linuxlover; 11-02-2010 at 11:38 AM. Reason: Code tags added by Fordy

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,563
    Do you get any error/warning messages when you compile?

    If so please post the complete error message.

    Have you tried to print out intermediate results?


    Jim

  3. #3
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Hello JIM
    no errors,no warnings
    I've checked all functions.All working properly
    Loop also works.

    But what happens to printf() ?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Learn what "\n" means; add it to all of your printf, Or use flush stdout. This might not be problem, but not sure it is not a problem.

    Learn to indent correctly.
    Learn to comment your code.
    Learn to use "{" and "}" even when they are not needed in loops.
    Learn to test each function using test code.

    Tim S.
    Last edited by stahta01; 11-02-2010 at 11:36 AM.

  5. #5
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Angry

    Hello TIM
    "\n" is of no importance in this program .just for printing another line.note that for loop has only one statement which is a compound statement

    i removed "\n" ,the same problem...........................

  6. #6
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    HELLO TIM
    See I've added comments
    Each fucnctions are checked for different inputs
    Last edited by linuxlover; 11-02-2010 at 11:42 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,563
    So you have tried something like:

    Code:
    int raise(int x,int n)
    {
       if(n==1)
          return x;
    
       printf("%d %d\n", x, n);
    
       return x*raise(x,n-1);
    }
    I'm not saying that this is where the error is by the way.

    Jim

  8. #8
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Ya I've tried that test..............

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    For starters:
    • Indent your code properly.
    • Remove unused variables.
    • Fully capitalise your macro names.
    • When your function has a return type other than void, return something. (This is optional for main with respect to C99.)

    I would expect something like this:
    Code:
    /*PROGRAM TO PRINT ARMSTRONG NUMBERS FROM 1 TO 1500*/
    
    #include<stdio.h>
    
    #define BOUNDARY 1500
    
    /*FUNCTION TO FIND X^N*/
    
    int raise(int x, int n)
    {
        if (n == 1)
            return x;
        return x * raise(x, n - 1);
    }
    
    /*FUNCTION TO ADD DIGITS*/
    
    long int sum(int n, int noofdigit)
    {
        static long int sum1 = 0;
    
        sum1 += raise(n % 10, noofdigit);
        if (n / 10 != 0)
            sum(n / 10, noofdigit);
        else return sum1;
    }
    
    /*FUNCTION TO FIND NUMBER OF DIGITS*/
    int digitn(int n)
    {
        int i;
        for (i = 1; n / 10 != 0; ++i)
            n /= 10;
        return i;
    }
    
    /*FUNCTION FOR CHECKING WHETHER NUMBER IS ARMSTRONG*/
    void armstrong()
    {
        int i;
        for (i = 0; i <= BOUNDARY; ++i)
            if (sum(i, digitn(i)) == i)
                printf("%d ", i);
        printf("\n");
    }
    
    int main(void)
    {
        printf("ARMSTRONG NUMBERS IN FIRST 1500 NUMBERS ARE: ");
        armstrong();
        return 0;
    }
    Now, compile your code with warnings turned on. You should get a warning about the sum function not returning a value for a control path. This is a problem. In fact, you should turn that static local variable in sum into a non-static local variable, and then use iteration, not recursion. Likewise, use iteration for the raise function (unless you want to use a more efficient implementation, in which case recursion might be an easy way out).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    I added this to main
    Code:
    printf("Number of digits in 153: %d\n", digitn(153));
    printf("Sum of digits in 153: %d\n", sum(153,digitn(153)));
    printf("Number of digits in 10: %d\n", digitn(10));
    printf("Sum of digits in 10: %d\n", sum(10,digitn(10)));
    And, I got this
    Code:
    Number of digits in 153: 3
    Sum of digits in 153: 153
    Number of digits in 10: 2
    Sum of digits in 10: 154
    Do you see the problem?
    See laserlight post for hint on fix.

    Tim S.
    Last edited by stahta01; 11-02-2010 at 12:41 PM.

  11. #11
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    TIM actually the sum() returns the sum as like in armstrong numbers.So sum(153,3) will return 1^3+5^3+3^3=153
    153 is an armstrong number

    Incase ofsum(10,digitn(10)); I got 1 which is 1^2 +0^2
    I don,t know why you got 154

    Thanks to laserlight for suggestions
    But why should'nt recursion is not so good? Is it due to something like processing speed?................
    Last edited by linuxlover; 11-02-2010 at 10:55 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by linuxlover
    But why should'nt recursion is not so good? Is it due to something like processing speed?
    To perform recursion, the state of the function is saved. This can potentially use up the available stack space. There is also a limit to the recursive depth. These are not likely to be problems for your particular program, but when it is easy to use iteration instead of recursion, you might as well do so.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    /*PROGRAM TO PRINT ARMSTRONG NUMBERS FROM 1 TO 1500*/

    #include<stdio.h>

    #define BOUNDARY 1500

    /*FUNCTION TO FIND X^N*/

    int raise(int x , int n)
    {
    int i,temp=x;

    for(i=1;i<n;++i)
    x*=temp;

    return x;
    }

    /*FUNCTION TO ADD DIGITS*/
    long int sum(int n,int noofdigit)
    { int temp;
    long int sum1=0;

    for(;n!=0;n/=10)
    sum1+=raise(n%10,noofdigit);

    return sum1;
    }


    /*FUNCTION TO FIND NUMBER OF DIGITS*/
    int digitn(int n)
    {
    int i;
    for (i = 1; n / 10 != 0; ++i)
    n /= 10;
    return i;
    }

    /*FUNCTION FOR CHECKING WHETHER NUMBER IS ARMSTRONG*/
    void armstrong()
    {
    int i;
    for (i = 0; i <= BOUNDARY; ++i)
    if (sum(i, digitn(i)) == i)
    printf("%d ", i);
    printf("\n");
    }

    int main(void)
    {
    printf("ARMSTRONG NUMBERS IN FIRST 1500 NUMBERS ARE: ");
    armstrong();
    return 0;
    }
    /*THANKS LASERLIGHT THE ABOVE CODE WORKED*/

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    You're welcome

    I'd be happier if you remembered to post in code tags though, heheh.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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