Question

In: Computer Science

Write a c++ program that randomly generates 100 dates, store them into four vectors of date...

Write a c++ program that randomly generates 100 dates, store them into four vectors of date objects according to their seasons.. The dates generated must be within 1000 days after 1/1/2000.

Solutions

Expert Solution

#include <iostream>
#include <sstream>
#include <vector>
#include <cstdlib>

using namespace std;

class Date {
        private:
                int m, d, y;
        public:
                static bool isLeapYear(int y) {
                        return (y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
                }
                static bool isValid(int m, int d, int y) {
                        if(m < 1 || m > 12 || d < 1 || d > 31) {
                                return false;
                        }
                        if((m == 6 || m == 11 || m == 4 || m == 9) && (d > 30)) {
                                return false;
                        }
                        if(isLeapYear(y) && m == 2 && d > 29) {
                                return false;
                        }
                        if(!isLeapYear(y) && m == 2 && d > 28) {
                                return false;
                        }
                        if(y < 1753 || y > 9999) {
                                return false;
                        }
                        return true;
                }
                Date(int m, int d, int y) {
                        this->m = m;
                        this->d = d;
                        this->y = y;
                }

                void increaseDate(int days) {
                        while(days != 0) {
                                d++;
                                if(!isValid(m, d, y)) {
                                        m++;
                                        d = 1;
                                        if(!isValid(m, d, y)) {
                                                y++;
                                                m = 1;
                                        }
                                }
                                days--;
                        }
                }

                string printDate() {
                        stringstream ss;
                        ss << m << "/" << d << "/" << y;
                        return ss.str();
                }
};

vector<string> getRandomDates(int count) {
        
        vector<string> result;

        for(int i=0; i<count; i++) {
                Date d(1, 1, 2000);
                d.increaseDate(rand() % 1000);
                result.push_back(d.printDate());
        }

        return result;
}

int main() {
        vector<string> dates = getRandomDates(100);

        for(string s: dates) {
                cout << s << endl;
        }
}
**************************************************
hey, This might not be an exact solution to your problem, because you have not given much information on like, what is the range of a season.. when does that starts.. Is it quarterly and so on.. But i think the above code must help you a lot.. and you can re-arrange little bit of code to customize it as per your requirement.. anyway, i am just a comment away.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

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...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The...
C++ code: Write a program that randomly generates an integer between 0 and 100, inclusive. The program prompts the user to enter a number continuously until the number matches the randomly generated number. For each user input, the program tells the user whether the input is too low or too high, so the user can choose the next input intelligently. Here is a sample run:
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 C++ app to read both files, store them into parallel vectors, sort the list...
Write a C++ app to read both files, store them into parallel vectors, sort the list of people in alphabetical order, display the new sorted list of names with their corresponding descriptions. Use the Bubble Sort strategy to rearrange the vector(s). File 1: Marilyn Monroe Abraham Lincoln Nelson Mandela John F. Kennedy Martin Luther King Queen Elizabeth II Winston Churchill Donald Trump Bill Gates Muhammad Ali Mahatma Gandhi Margaret Thatcher Mother Teresa Christopher Columbus Charles Darwin Elvis Presley Albert Einstein...
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...
Write a program in C which randomly generates two integers X and Y between 10 and...
Write a program in C which randomly generates two integers X and Y between 10 and 50, and then creates a dynamic array which can hold as many numbers as there are between those two random numbers. Fill the array with numbers between X and Y and print it on screen. Do not forget to free the allocated memory locations whenever they are no longer needed. Example: Randomly generated numbers are: 43 23 Expected Output : 23 24 25 26...
C++ PLEASE IMPORTANT: The use of vectors is not allowed This program will store roster and...
C++ PLEASE IMPORTANT: The use of vectors is not allowed This program will store roster and rating information for a basketball team. Coaches rate players during tryouts to ensure a balanced team. A roster can include at most 10 players. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int array and the ratings in another int array. Output...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers called months and days to store the month and day of each date generated, a constant array of 12 integers called num_of_days that specify the number of days of each of the 12 months and an integer called size that specifies how many dates to generate and randomly generates size dates, storing the generated months in months array and generated days in days array....
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 asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT