Convert a Date to the Julian Day Number in C++

Convert a Date to the Julian Day Number in C++ Homework Sample

Convert a date in terms of year, month and day to the Julian day number. Get the current date, and the date of your birth, and display your age in days. For more C++ assignment help contact us.

Solution:

Julian.cpp

// Program : Julian Day Numbers
// Description: This program will compute and display the given date by a user to
// the corresponding JDN.

#include <iostream>
#include <limits>

void get_date(int &month, int &day, int &year);
void compute_jdn(int month, int day, int year, int &jdn);
void compute_dow(int month, int day, int year, int &dow);
void display_results(int month, int day, int year, int jdn, int b_month, int b_day, int b_year, int b_jdn, int dow, int days_between);
void compute_days(int jdn_1, int jdn_2, int &days_between);
void pause();

int main()
{
int month, day, year, jdn, b_month,b_day,b_year,b_jdn,dow,days_between;

std::cout << ” For today’s date: “<<std::endl;
get_date(month, day, year);

std::cout << ” For your birthday: “<<std::endl;
get_date(b_month,b_day,b_year);

compute_jdn(month, day, year, jdn);
compute_jdn(b_month,b_day,b_year,b_jdn);

compute_dow(b_month,b_day,b_year,dow);
compute_days(b_jdn,jdn,days_between);

display_results(month,day,year,jdn,b_month,b_day,b_year,b_jdn,dow,days_between);

pause();
}

//
// An input module that asks the user to enter a date.
//
void get_date(int &month, int &day, int &year)
{
std::cout << ” Please enter a Month (Jan = 1, Feb = 2, etc): “;
std::cin >> month;
std::cout << ” Please enter a Day (1 – 31): “;
std::cin >> day;
std::cout << ” Please enter a Year in a four digit format: “;
std::cin >> year;
}

//
// A module that calculates and processes the dow of a given date
//
void compute_dow(int month, int day, int year, int &dow)
{
int jdn;
compute_jdn(month,day,year,jdn);
dow=(jdn+1)%7;
}
//
// A module that calculates and processes the date to JDN.
//
void compute_jdn(int month, int day, int year, int &jdn)
{
int a, m, y, leap_days;
a = ((14 – month) / 12);
m = (month – 3) + (12 * a);
y = year + 4800 – a;
leap_days = (y / 4) – (y / 100) + (y / 400);
jdn = day + (((153 * m) + 2) / 5) + (365 * y) + leap_days – 32045;
}

//
// A module that calculates the difference between two jdns
//
void compute_days(int jdn_1, int jdn_2, int &days_between)
{
days_between=jdn_2-jdn_1;
}

//
// A module that displays the JDN.
//
void display_results(int month, int day, int year, int jdn, int b_month, int b_day, int b_year, int b_jdn, int dow, int days_between)
{
std::cout << std::endl;
std::cout << “For the date: “;
std::cout << month << ” / “;
std::cout << day << ” / “;
std::cout << year;
std::cout << ” the Julian Date is:”;
std::cout << jdn << std::endl;

std::cout << std::endl;
std::cout << “For your birthday: “;
std::cout << b_month << ” / “;
std::cout << b_day << ” / “;
std::cout << b_year;
std::cout << ” the Julian Date is:”;
std::cout << b_jdn << std::endl;

std::cout<< std::endl <<“DOW for your birthday (0=Sunday, 1=Monday, 2=Tuesday, etc..): “<< dow <<std::endl;
std::cout<<“It’s been “<< days_between <<” days since your birthday”<<std::endl;
}

//
//A module that helps keep the console window open when using
// Microsoft Visual Studio.
//
void pause() {
// Omit these next two lines if your program doesn’t get any input
// from the user via the keyboard
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);
std::cout << std::endl;
std::cout << “Press the enter key to continue…”;

std::cin.get();
}