Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Recursive Problem

Posted on 2000-05-02
Medium Priority
217 Views
There's something wrong with this recursive call...I want to get the total number of days in the year so far, but I can't get the days in january added without making it fail to work if the date is in january.  Thanks for the help!

int numday(int month, int day, int *total)

{

if(month==1)
*total=*total+day;
else
{
if(month==3||||month==12||month==5||month==7||month==8
||month==10)
*total=*total+31+numday(month-1, day, total);

else if(month==2)
*total=*total+28+numday(month-1, day, total);

else
*total=*total+30+numday(month-1, day, total);
}

}
0
Question by:SaraBiz
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 4
• 2
• +1

LVL 3

Expert Comment

ID: 2769989
Use braces properly otherwise the OR operation may result in ambiguities.

int numday(int month, int day, int *total)
{
if(month==1)
*total=*total+day;
else
{
if((month==3) || (month==12) || (month==5) || (month==7) || (month==8) || (month==10))                       *total=*total+31+numday(month-1, day, total);
else if(month==2)
*total=*total+28+numday(month-1, day, total);

else
*total=*total+30+numday(month-1, day, total);
}

}

Sreenath
0

LVL 16

Accepted Solution

ID: 2770203
The flaw is that the algorithm should be adding together the total number of days in all the months except the current one. For the current one you need to add "day" days (which is presumably the current date). However, your function is adding "day" days for January. That works fine, of course, if January is the current month, but otherwise, the month==1 call should be adding 31, not day. I think that to make this work recursively you would need a small driver routine, or maybe recurse "forward", which would necessitate passing through the value of the current month. The driver solution would be:

int numday(int month,int day,int *total)
{   if(month==1)return(day);
*total=day;
getdays(month-1,*total);
return;
}

int getdays(int month,int *total)
{   if(month==1)*total=*total+31;
else
{   if(month==3 || month==5...
*total=*total+31+getdays(month,total);
etc.
}
}
0

LVL 16

Expert Comment

ID: 2770214
Sorry, numday should return values with total, so it should be:

int numday(int month,int day,int *total)
{   *total=day;
if(month>1)getdays(month-1,*total);
return;
}
0

LVL 16

Expert Comment

ID: 2770247
If you recurse "forward", and return values, rather than using total, you can still manage with three arguments though. It would look something like:

int numdays(int month,int day,int curmon)
{   int td;

if(month==curmon)return(day);
if(month==1 || month==3 || month==5 ||
month==7 || month==8 || month==10 ||
month==10 || month==12)
td=31;
else if(month==2)td=28;
else td=30;
return(td+numdays(month+1,day,curmon);
}

If I had to write this recursively this is how I would do it. On the whole though I must say, I would not have. There does not appear to be any advantage in efficiency or clarity, over the iterative solution.
0

LVL 16

Expert Comment

ID: 2770337
Note: you would have to call it with a 1:

....
code that needs to know how many days
in the year so far
....
ytddays=numdays(1,date,month);
0

Author Comment

ID: 2770548
Ok, I realized that I needed to change the month numbers, but its still not working.  I'm supposed to stay very close to this format...is there any way I can do that?  Thanks...here's the changes...

int numday(int month, int day, int *total)

{
if(month==1)
*total=*total+day;
else
{

if(month==2||month==4||month==6||month==8
||month==10||month==11)
*total=*total+31+numday(month-1, day, total);

else if(month==3)
*total=*total+28+numday(month-1, day, total);

else
*total=*total+30+numday(month-1, day, total);
}
}

0

LVL 16

Expert Comment

ID: 2770723
OK. We can rescue it with a global variable. Again, the crux of the problem is that for January you must add 31 days to total, unless the current date is January in which case you only add day. Also, I don't understand the way you renumbered the months. Your new code implies that month 3 is February. That seems very odd. I think you should stick to the original, obvious scheme. Then you would get something like:

static int firstmonth=1;

int numdays(int month,int day,int *total)
{   if(firstmonth)
{   firstmonth=0;
*total=*total+day;
if(month>1)numdays(month-1,day,total);
firstmonth=1;
}
if(month==1)
*total=*total+31;
else
{   if(month==3 || month==5 || month==7 ||
month==8 || month==10 || month==12)
*total=*total+31+numdays(month-1,day,total);
else if(month==2)
*total=*total+28+numdays(month-1,day,total);
else *total=*total+30+numdays(month-1,day,total);
}
}

So what is happening is that firstmonth is initialized to 1. When numdays is called, and the firstmonth is set (which it will be initially) the number of days in the current month is added to total, firstmonth is set to false, then numdays is called for all previous months. For all calls other than firstmonth, the first clause is skipped and the full length of the months are added. Encountering month 1 (January) causes further calls to numdays to stop.

0

Expert Comment

ID: 2773224
Why using a recursive procedure ? try this

static int numDaysMonth[12] ={31 ,28 ,..........,31};

int numdays(int month ,int day ,int *total)
{
total =days;
for (char i =month -1 ;i >=0 ;i++)
total +=numDaysMonth[i];
}

I you want to keep on using a recursive procedure bear in mind
that the parameter "day" should be used only in the first call
(number of days of the current month)
0

Author Comment

ID: 2773577
trust me, I'd love to avoid using a recursive procedure, but I'm being forced.  However, the code imladris gave me won't compile right...is there something missing?
0

Author Comment

ID: 2773733
Whoops.  I looked back at my code and realized my mistake...I'm adding total waaay to many times, and I forgot to return it in the end.  Thanks for all your help though...I'm sure it would work your ways, but I guess I'm just stubborn!  ;)

Here's my final (working wonderfully!) code...

int numday(int month, int day)

{
int total;

if(month==1)
total=day;
else
{

if((month==2)||(month==4)||(month==6)||(month==8)
||(month==10)||(month==11))
total=31+numday(month-1, day);

else if(month==3)
total=28+numday(month-1, day);

else
total=30+numday(month-1, day);
}

return(total);
}
0

LVL 16

Expert Comment

ID: 2773899
I see what you're doing! For each month you add in the days in the previous month and then for "January" you add in the days for the current month! Very clever.

0

Expert Comment

ID: 2774222
Yes ,but rather expensive and slow  .Why don't use my iterative version ,for instance ?
0

Author Comment

ID: 2774344
I was told to use recursive...trust me, I'd much rather not!  Thanks!!!!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month8 days, 4 hours left to enroll