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

IN C++ Problem 1: Arrays of Structures Define a structure to store a date, in te

ID: 3695808 • Letter: I

Question

IN C++

Problem 1: Arrays of Structures

Define a structure to store a date, in terms of three numeric fields for the month, day, and year. Write a method that, given as input parameters an array of dates (using your new type) and its logical size, initializes an output parameter array prints all of the same dates, formatted as strings like "March 31, 2016." (Perhaps you might want a helper method to print a single date.)

Write a short program that initializes a small array of dates and test your method.

Explanation / Answer

#include<iostream>

using namespace std;

// structure
struct Date{
   int day;
   int month;
   int year;
};

string getMonthName(int month){
  
   switch(month){
       case 1:
           return "January";
       case 2:
           return "Feburary";
       case 3:
           return "March";
       case 4:
           return "April";
       case 5:
           return "May";
       case 6:
           return "June";
       case 7:
           return "July";
       case 8:
           return "August";
       case 9:
           return "September";
       case 10:
           return "October";
       case 11:
           return "November";
       case 12:
           return "December";
   }
  
   return "Invalid Month Number";
}

void display(Date date){
   cout<<getMonthName(date.month)<<" "<<date.day<<", "<<date.year<<endl;
}

int main(){
  
   Date dates[5] = {
       {1,12,1992}, {4,5,2003}, {23,4,1998},{12,8,2016},{9,7,2000}
       };
      
   for(int i=0; i<5; i++){
       display(dates[i]);
   }
  
   return 0;
}

/*

Output:

December 1, 1992
May 4, 2003
April 23, 1998
August 12, 2016
July 9, 2000

*/