C++ Programming Using these functions: leap, days_in_month, valiDATE (shown belo
ID: 3678526 • Letter: C
Question
C++ Programming
Using these functions: leap, days_in_month, valiDATE (shown below) write another function that will convert a date of the form MM DD YYYY to the Julian date form DDD.YYYY where DDD is the day of the year. Your main program will prompt the user to enter a date in MM DD YYYY form. It will then call the valiDATE function to make sure the date is correct. Following validation the main program will call your convert function to produce the Julian date. Print out the dates in normal MM DD YYYY and DDD.YYYY form. Example: 01 25 2015 will convert to 025.2015 and 12 31 2015 will convert to 365.2015.
Explanation / Answer
#include <iostream>
using namespace std;
unsigned int days_count = 0;
unsigned int leap(unsigned int year)
{
if (year % 400 == 0)
return 1;
else if(year % 100 != 0 && year % 4 == 0)
return 1;
else
return 0;
}
unsigned int days_in_month(unsigned int month, unsigned int year){
unsigned int days = 0;
if (month == 2) {
days = 28 + leap(year); // function leap returns 1 for leap years
}
else if ( month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
}
else if(month !=0){
days = 31;
}
return days;
}
bool valiDATE(unsigned int month, unsigned int day, unsigned int year) {
unsigned int flag = 0;
unsigned int days = 0;
// Validate the month first
if (month < 1 || month > 12) {
cout << "Value for month (MM) is out of range." << endl;
return false;
}
// Validate the day of month
days = days_in_month(month, year);
if ( day < 1 || day > days) {
cout << "Value for day (DD) is out of range." << endl;
return false;
}
// Validate the year
if (year < 1990 || year > 2020) {
cout << "Value for year (YYYY) is out of range." << endl;
return false;
}
return true;
}
unsigned int days_count_in_year(unsigned int month, unsigned int year){
return days_in_month(month,year)+days_count_in_year(month-1,year);
}
int main() {
// your code goes here
unsigned int Day,Month,Year;
cout<<" Enter Long Date (DD MM YYYY) "<<endl;
cin>>Month>>Day>> Year;
if(valiDATE(Month,Day,Year)){
cout<<Day+days_count_in_year(Month,Year)<<"."<<Year;
}else{
cout << "Invalid date format";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.