C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 10th, 2012, 05:42 PM
miz656 miz656 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 17 miz656 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 3 h 31 m 51 sec
Reputation 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

Reply With Quote
  #2  
Old December 10th, 2012, 06:40 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 9 sec
Reputation Power: 1974
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?

Reply With Quote
  #3  
Old December 10th, 2012, 07:07 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 9 sec
Reputation Power: 1974
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > The arithmetic operator, %

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap