Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I get this code to work in c++. I also need to verify that the month rang

ID: 3717057 • Letter: H

Question

How do I get this code to work in c++. I also need to verify that the month range is correct and if not tell the user, veryify that the day range is correct and if not tell the user

#include "std_lib_facilities.h"

a = (14 - Month) / 12

m = Month + 12a - 3

y = Year + 4800 - a;

nDate = Day + ((153 * m + 2) / 5) + 365 * y + y / 4 - y / 100 + y / 400 - 32045

* /


#include <iostream>

using namespace std;

//function definition

int serial_julian_date(int Month, int Day, int Year)
{
   double a = (double)(14 - Month) / 12;
   int m = Month + 12 * a - 3;
   int y = Year + 4800 - a;
   double nDate =
       Day + ((double)(153 * m + 2) / 5) +
       365 * y + (double)y / 4 - (double)y / 100 + (double)y / 400 - 32045;
   return (int)nDate;
}
//main function
int main()
{

   cout << "Enter the date in MM DD YYYY format (seperated by spaces)" << endl;
   //declaring necessary variables
   int Month, Day, Year;
   //taking inputs
   cin >> Month >> Day >> Year;
   //converting
   int d = serial_julian_date(Month, Day, Year);
   //display result
   cout << "Serial Julian date is :";
   cout << endl << d << endl;

   return 0;
}

Explanation / Answer

#include <iostream>
using namespace std;
//function definition
int serial_julian_date(int Month, int Day, int Year)
{
    double a = (double)(14 - Month) / 12;
    int m = Month + 12 * a - 3;
    int y = Year + 4800 - a;
    double nDate =
        Day + ((double)(153 * m + 2) / 5) +
        365 * y + (double)y / 4 - (double)y / 100 + (double)y / 400 - 32045;
    return (int)nDate;
}
//main function
int main()
{
    //declaring necessary variables
    int Month, Day, Year;
    //flag is used to terminate while loop
    //only when date,month are valid
    bool flag;
    do
    {
       flag=false;
       //prompt user to enter
       cout << " Enter the date in MM DD YYYY format (seperated by spaces)" << endl;
       //taking inputs
       cin >> Month >> Day >> Year;
       //find year is leap or not, to check date is valid or not for feb month
       bool leap=(((Year%4==0)&&(Year%100!=0))||(Year%400==0));
       //if month is not in the range 1 to 12
       if(Month>12||Month<1)
       {
           cout<<" Invalid Month";
           flag=true;
       }
       //check date ranges based on month
       //months that are 31 days
       if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12)
       {
           if(Day>31||Day<1){
               cout<<" Invalid Date";
               flag=true;
           }
       }
       //months that are 30 days
       else if(Month==4||Month==6||Month==9||Month==11)
       {
           if(Day>30||Day<1){
               cout<<" Invalid Date";
               flag=true;  
           }
       }
       //checking for feb month
       else
       {
           if(leap){
               if(Day>29||Day<1){
                   cout<<" Invalid Date";
                   flag=true;
               }
           }
           else{
               if(Day>28||Day<1){
                   cout<<" Invalid Date";
                   flag=true;
               }
           }
          
       }  
   }while(flag);
    //converting
    int d = serial_julian_date(Month, Day, Year);
    //display result
    cout << "Serial Julian date is :";
    cout << endl << d << endl;

    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote