Requirements
Write the following function:
void splitDate(int dayOfYear, int year, int *month, int *day);
dayOfYear is an integer between 1 and 366, specifying a particular day within the year designated by year. month and day point to variables in which the function will store the equivalent month (1-12) and day within the month(1-31).
Write a main function to test the correctness of your function.
Note: Do consider that February has 29 days for leap years.

Based on my code, is this the right way to go about it or is there a simpler way to do this?

#include <iostream>
#include <stdio.h> 

using namespace std;

void splitDate(int dayOfYear, int year, int *month, int *day);

int main()
{
int dayOfYear, year;
 int *month, *day;

cout<<"Enter a day"<<endl; 
cin>>dayOfYear;

cout<<"Enter a year"<<endl; 
cin>>year; 

splitDate(dayOfYear, year, month, day);
system("pause"); 
return 0; 

}
void splitDate(int dayOfYear, int year, int *month, int *day)
{ 
     if(dayOfYear >=1 || dayOfYear <= 31)
     {
          cout<<"month = 1"<<endl; 
          cout<<"day = "<<dayOfYear<<endl;
     }


     else if(dayOfYear >=32 || dayOfYear <= 70)
     {
          cout<<"month = 2"<<endl; 
          cout<<"day = "<<dayOfYear<<endl;
     }

     else if(dayOfYear >=71 || dayOfYear <= 101)
     {
          cout<<"month = 3"<<endl; 
          cout<<"day = "<<dayOfYear<<endl;
     }

}

Your code doesn't take leap year into account, and "magic numbers" are generally a bad idea. (Although they are unlikely to change in this case..)

You could try something like this:

#include <iostream>
#include <cassert>

using namespace std;

enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
const int DAYS_PER_MONTH[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };


bool IsLeapYear (const int year)
{
    assert(year >= 0);

    // See: http://en.wikipedia.org/wiki/Leap_year
    return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0));
}


// Obtain the daynumber of the first day of a given month in a given year.
int GetFirstDay(const Month month, const int year)
{
    assert (year >= 0);
    int result = 1;

    for (int currentMonth = Jan; currentMonth < month; currentMonth++)
    {
        // Add 1 day for february when the supplied year is a leap year.
        result += DAYS_PER_MONTH[currentMonth] + (currentMonth == Feb && IsLeapYear(year));
    }

    return result;
}


// Obtain the amount of days in a year.
int DaysInYear (const int year)
{
    assert (year >= 0);
    return GetFirstDay(Dec, year) + DAYS_PER_MONTH[Dec] - 1;
}


// Your function.
void SplitDate(const int dayOfYear, const int year, int *month, int *day)
{
    assert (dayOfYear >= 1 && dayOfYear <= DaysInYear(year) && year >= 0 && month != NULL && day != NULL);

    // Set the month
    for (*month = Jan; *month < Dec && GetFirstDay(static_cast<Month>(*month + 1), year) <= dayOfYear; (*month)++);

    // Set the day and increment the month by 1 so Jan = 1, Feb = 2, etc.
    (*day) = dayOfYear - GetFirstDay(static_cast<Month>(*month), year) + 1;
    (*month)++;
}

int main()
{
    int month, day;

    for (int i = 1; i <= DaysInYear(2012); i++)
    {
        SplitDate(i, 2012, &month, &day);
        cout << "month: " << month << ", day: " << day << endl;
    }

    return 0;
}

-edit-

Forgot to set the day in your function. Also made it so the function returns values starting by 1 instead of 0 like the enum. Depends on what you want I guess.

Edited 4 Years Ago by Gonbe

This article has been dead for over six months. Start a new discussion instead.