In: Computer Science
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.
#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.