Solved

Recursive Problem

Posted on 2000-05-02
13
208 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now