Question

In: Computer Science

Write a program that randomly generates 100 dates and store them into a vector. Use the...

Write a program that randomly generates 100 dates and store them into a vector. Use the Date.h provided . The dates generated must be within 1000 days after 1/1/2000. and Sort the 100 dates generated in ascending order.

date.h

#ifndef DATE_H_
#define DATE_H_

#include
#include

using namespace std;

class Date {
   friend ostream &operator<<( ostream &, const Date & );
private:
   int day;
   int month;
   int year;
static const int days[]; // array of days per month
void helpIncrement(); // utility function for incrementing date

public:
   Date(int=1, int=1, int=0);
   void setDate(int,int,int);
   bool leapYear( int ) const; // is date in a leap year?
bool endOfMonth( int ) const; // is date at the end of month?
Date &operator++(); // prefix increment operator
Date operator++( int ); // postfix increment operator
const Date &operator+=( int ); // add days, modify object
bool operator<(const Date&) const;

   void showdate();
};

const int Date::days[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

Date::Date(int d, int m, int y) {
day = d;
month = m;
year = y;
// initialize static member at file scope; one classwide copy
}

// set month, day and year
void Date::setDate( int dd, int mm, int yy )
{
year = yy;
month = ( mm >= 1 && mm <= 12 )? mm : 1;

// test for a leap year
if ( month == 2 && leapYear( year ) )
day = ( dd >= 1 && dd <= 29 ) ? dd : 1;
else
day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;
} // end function setDate

// if the year is a leap year, return true; otherwise, return false
bool Date::leapYear( int testYear ) const
{
if ( testYear % 400 == 0 ||
( testYear % 100 != 0 && testYear % 4 == 0 ) )
return true; // a leap year
else
return false; // not a leap year
} // end function leapYear

// determine whether the day is the last day of the month
bool Date::endOfMonth( int testDay ) const
{
if ( month == 2 && leapYear( year ) )
return testDay == 29; // last day of Feb. in leap year
else
return testDay == days[ month ];
} // end function endOfMonth

// function to help increment the date
void Date::helpIncrement()
{
// day is not end of month
if ( !endOfMonth( day ) )
day++; // increment day
else
if ( month < 12 ) // day is end of month and month < 12
{
month++; // increment month
day = 1; // first day of new month
} // end if
else // last day of year
{
year++; // increment year
month = 1; // first month of new year
day = 1; // first day of new month
} // end else
} // end function helpIncrement

// overloaded prefix increment operator
Date &Date::operator++()
{
helpIncrement(); // increment date
return *this; // reference return to create an lvalue
} // end function operator++

// overloaded postfix increment operator; note that the
// dummy integer parameter does not have a parameter name
Date Date::operator++( int )
{
Date temp = *this; // hold current state of object
helpIncrement();

// return unincremented, saved, temporary object
return temp; // value return; not a reference return
} // end function operator++

// add specified number of days to date
const Date &Date::operator+=( int additionalDays )
{
for ( int i = 0; i < additionalDays; i++ )
helpIncrement();

return *this; // enables cascading
} // end function operator+=

// overloaded output operator
ostream &operator<<( ostream &output, const Date &d )
{
// static string monthName[ 13 ] = { "", "January", "February",
// "March", "April", "May", "June", "July", "August",
// "September", "October", "November", "December" };
//
// output << d.day << " " << monthName[ d.month ] << " "<< d.year;
   output << setfill('0')
           << setw(2) << d.day << '/'
           << setw(2) << d.month << '/'
           << setw(4) << d.year;

   return output; // enables cascading
} // end function operator<<

bool Date::operator<(const Date& right) const {
   return (year < right.year || (year == right.year && month < right.month) ||
           (year == right.year && month == right.month && day < right.day));
}

void Date::showdate()
{
cout << "The date is ";
cout << setfill('0')
       << setw(2) << day << '/'
       << setw(2) << month << '/'
       << setw(2) << year % 100
       << endl;
return;
}

#endif /* DATE_H_ */

Solutions

Expert Solution

Here is the program:

#include <iostream>
#include "date.h"
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    
    vector<Date> dv;
    
    
    for (int i = 0; i < 100; i++) {
        // Random number less than 1000
        int daysAfter2K = (rand () ) % 1000;
        // Find date after 1/1/2000 that is daysAfter2K days later
        int yr = daysAfter2K / 365;
        int mn = (daysAfter2K - yr * 365) / 30 + 1;
        int dt = daysAfter2K - yr * 365 - (mn - 1) * 30;
        yr = yr + 2000;
        
        Date d;
        d.setDate (dt, mn, yr);
        dv.push_back (d);
    }

    sort (dv.begin(), dv.end());
    
    for (const auto &d : dv)
        cout<<d << endl;

    return 0;
}

Note that lines 3 and 4 in date.h are incomplete in code snippet given in problem, they should be:

#include <iostream>
#include <iomanip>


Related Solutions

Write a program in date_generator.py that generates all the dates of next year in mmm dd,...
Write a program in date_generator.py that generates all the dates of next year in mmm dd, yyyy format, as shown below. You need not calculate whether 2021 is a leap year--it is NOT. If you want to see a hint, scroll WAY down! Jan 1, 2021 Jan 2, 2021 Jan 3, 2021 . . . Dec 30, 2021 Dec 31, 2021 ***my teachers hint:My solution to this program is quite short, but the code is not super simple. I used...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and...
[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and stores them into an array, where n is a parameter of the method. The method returns all the numbers above the average of the n random numbers. Use loops wherever possible. For example, if the 10 random numbers are 44 61 98 45 45 17 63 24 9 95 The method should returns an array which contains 61 98 63 95 In the main...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT