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

Using C++ A struct (or object) to represent dates is to be implemented. It must

ID: 3834814 • Letter: U

Question

Using C++

A struct (or object) to represent dates is to be implemented. It must contain a string for the day of the week, and three numbers for the day of the month, the month, and the year.

Part A. Give the struct definition, along with a suitable set function for initialising a date, and a print function that prints one nicely. Using your definitions, I should be able to write this:

date today, st_swithins_day;

set(today, “Tuesday”, 22, 11, 2011); set(st_swithins_day, “Friday”, 15, 7, 2011); print(today);

and when run, that code should print something very close to this

Tuesday 22nd November 2011

or this

Tuesday November 22nd 2011

Part B. Define a function called latest, which takes two dates as parameters, and returns as its result the one that is latest (i.e. comes second). So for example, this

date x = latest(today, st_swithins_day);

print(x)

should also print today’s date.

Part C. Define a function called next, which takes one date as its parameter, and modifies that date by moving it on to the next day, so this

next(today);

print(today);

next(today);

print(today);

would print

Wednesday November 23rd 2011

Thursday November 24th 2011

Explanation / Answer

void set(Date &d,string dayofweek,int day,int month,int year){
  
   d.dayofweek=dayofweek;
   d.day=day;
   d.month=month;
   d.year=year;
  
}

void print(Date &d){
   cout << d.dayofweek << " ";
  
   if(d.day==1 or d.day== 21 or d.day==31){
       cout << d.day <<"st ";
   }else if(d.day == 3 or d.day== 23){
       cout << d.day <<"rd ";
   }else if(d.day >3 & d.day<21){
       cout << d.day <<"th ";
   }else{
       cout << d.day <<"nd ";
   }
  
   switch(d.month){
       case 1:
           cout << "January ";
           break;
          
       case 2:
           cout << "Febraury ";
           break;      

       case 3:
           cout << "March ";
           break;
          
       case 4:
           cout << "April ";
           break;  
          
       case 5:
           cout << "May ";
           break;  
          
       case 6:
           cout << "June ";
           break;  
          
       case 7:
           cout << "July ";
           break;
          
       case 8:
           cout << "August ";
           break;      

       case 9:
           cout << "September ";
           break;
          
       case 10:
           cout << "October ";
           break;  
          
       case 11:
           cout << "November ";
           break;  
          
       case 12:
           cout << "December ";
           break;
   }
  
   cout << d.year;
}

Date latest(Date d1,Date d2){
   Date rd;
   if(d1.year>d2.year){
       rd=d1;
   }else if(d2.year > d1.year){
       rd = d2;
   }else if (d1.month >d2.month){
       rd=d1;
   }else if(d2.month> d1.month){
       rd=d2;
   }else if (d1.day >d2.day){
       rd=d1;
   }else if(d2.day> d1.day){
       rd=d2;
   }
  
   return rd;
  
}

void next(Date &d){
  
   if(d.month == 1 or d.month == 3 or d.month == 5 or d.month == 7 or d.month == 8 or d.month == 10 or d.month == 12){
       if(d.day==31){
           d.day=1;
           if(d.month==12){
               d.month=1;
               d.year++;
           }else{
               d.month++;
           }
       }else{
           d.day++;
       }
   }else if(d.month == 4 or d.month == 6 or d.month == 9 or d.month == 11){
       if(d.day==30){
           d.day=1;          
           d.month++;
          
       }else{
           d.day++;
       }
   }else{
       if(d.year%100 == 0 or d.year%4 ==0){
           if(d.day==29){
           d.day=1;          
           d.month++;
          
       }else{
           d.day++;
       }
       }else{
           if(d.day==28){
           d.day=1;          
           d.month++;
          
       }else{
           d.day++;
       }
       }
   }
  
  
       if(d.dayofweek== "Monday"){
           d.dayofweek="Tuesday";
       }else if(d.dayofweek== "Tuesday"){
           d.dayofweek="Wednesday";
       }else if(d.dayofweek== "Wednesday"){
           d.dayofweek="Thursday";
       }else if(d.dayofweek== "Thursday"){
           d.dayofweek="Friday";
       }else if(d.dayofweek== "Friday"){
           d.dayofweek="Saturday";
       }else if(d.dayofweek== "Saturday"){
           d.dayofweek="Sunday";
       }else if(d.dayofweek== "Sunday"){
           d.dayofweek="Monday";
       }
  
}

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