#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    The arithmetic operator, %


    I made a program. This program is supposed to print all the divisors that, when divided, are not decimals.

    #include<stdio.h>

    int divisible(int number,int divisor);

    int divisible(int number,int divisor)
    {
    if (divisor == number)
    {
    return;
    }
    if (number % divisor == 0)
    {
    return divisor,divisible(number,divisor + 1);
    }
    divisible(number,divisor + 1);
    }

    main()
    {
    printf("%d",divisible(10,1));
    getchar();
    }

    This program is supposed to print 2 and 5 because 10/2 does not have a fraction in it. Same with 5.

    It's not doing this. Why?

    On that note, may I ask. When dealing with arrays, is there a way you can insert or remove elements after you defined the identifier?

    New to C, just started 2 weeks ago. Just started this forum now. Hope I get to meet all you
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Use code tags to post your code. That will preserve your code's indentation and thus keep it readable.

    Code:
    #include<stdio.h>
    
    int divisible(int number,int divisor);
    
    int divisible(int number,int divisor)
    {
        if (divisor == number)
        {
            return;
        }
        if (number % divisor == 0)
        {
            return divisor,divisible(number,divisor + 1);
        }
        divisible(number,divisor + 1);
    }
    
    main()
    {
        printf("%d",divisible(10,1));
        getchar();
    }
    You should be getting all kinds of warnings from your compiler about that function. Never ignore warnings! They are much more important than error messages.

    You've declared divisible to return an int, but the first return returns nothing, so you've broken your promise to the compiler and it should have complained about that. The other return should work, but probably not the way you think it should:
    return divisor,divisible(number,divisor + 1);
    That statement will evaluate divisor and then throw that value away and then make the recursive call from which it should eventually return with a value (that promise that you broke with that first return). We should expect a return at the end of the function, but that additional recursive call whose return value you throw away might keep the compiler from complaining about that.

    On the face of it, that printf in main expects divisible to return a single int, yet in your narrative you expect two values to be printed. Why would you expect that?

    Since a function can only return a single value and you want to print out more than one value, I would recommend either using an array or else have divisible print each divisor as it finds it.

    Also, are you being required to use recursion? That's a fairly advanced concept and technique for someone with only two weeks experience with C. Couldn't you just use a for-loop to iterate through all the possible divisors?
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Originally Posted by miz656
    On that note, may I ask. When dealing with arrays, is there a way you can insert or remove elements after you defined the identifier?
    I'm not sure what you are asking here.

    If you're asking whether you can change the size of the array during run-time, then the answer is "no". At least with normal arrays that you declare statically. You could create an array dynamically with calloc and a pointer and that you could resize with realloc. But that is yet another topic that you will need to build yourself up to.

IMN logo majestic logo threadwatch logo seochat tools logo