Solved

Recursive Problem

Posted on 2000-05-02
13
211 Views
Last Modified: 2011-10-03
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
Comment
Question by:SaraBiz
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 3

Expert Comment

by:sreenathk
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

by:
imladris earned 50 total points
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

by:imladris
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 16

Expert Comment

by:imladris
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

by:imladris
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

by:SaraBiz
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

by:imladris
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

by:e6694811
ID: 2773224
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
 

Author Comment

by:SaraBiz
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

by:SaraBiz
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

by:imladris
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

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

Author Comment

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

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question