Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Recursive Problem

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
SaraBiz
Asked:
SaraBiz
  • 6
  • 4
  • 2
  • +1
1 Solution
 
sreenathkCommented:
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
 
imladrisCommented:
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
 
imladrisCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
imladrisCommented:
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
 
imladrisCommented:
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
 
SaraBizAuthor Commented:
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
 
imladrisCommented:
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
 
e6694811Commented:
Why using a recursive procedure ? try this

// add the missing days
 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
 
SaraBizAuthor Commented:
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
 
SaraBizAuthor Commented:
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
 
imladrisCommented:
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
 
e6694811Commented:
Yes ,but rather expensive and slow  .Why don't use my iterative version ,for instance ?
0
 
SaraBizAuthor Commented:
I was told to use recursive...trust me, I'd much rather not!  Thanks!!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now