I have this assignment, in which I'm supposed to output the hottest and coldest days of the year based on ranged random numbers. The output needs to look like this example: "Hottest day was Thursday July 19." I need the DailyTemp array (required for the assignment). The program is also supposed to show the averages for each month, which I have done, but feel could be coded more simply. The main problem is the assigning the name of the day to everyday of the 365 day year without coding it all in longhand, which is an impossibility.
Look at your code for each month. There are a couple of differences (for example, the number of days in the month). Those differences become parameters to a function you'll write.
If you know what day of the week January 1st fell on, then you can easily calculate the day of the week any day fell on. Eg, if the 1st day of the year (Jan 1) fell on Monday, then the 8th day of the year also was Monday and the 15th, 22nd, 29th, 36th, and so forth.
The rest is just computing the month and day. If you know how many days are in each month, then you can easily figure out which month the day was in and what the date was.
I know that about the days of the week, but I can't for the life of me figure out the code for it, how to write a loop or a if statement for it. It's possible I'm just having a brain fart or something. January 1st is a Monday.
I just can't figure out where to put the code for the names of the days of the week or the month names, or even what code to put in. Especially if I change the monthly for() statements in to a function.
haha. Yeah I knew that, but I just wanted to work out the logic and see if i could work it this way. I'm still a little fuzzy with functions. I understand the logic, mostly, but the application is a little strange to figure out for me. but with what you're suggesting, how do I use those to display the month and date within a function?
Here's the general idea of the function (obviously wouldn't work yet; told you I was bad at functions):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void MonthCalc (int DailyTemp[], int DAYS_OF_YEAR, int Highest, int Lowest,
int CurrDays, int LastDay, char MonthName[], int y, int MonthDays [])
{
int Total = 0;
int Avg = 0;
for (int x = CurrDay; x <= LastDay ; x++)
{
do
DailyTemp[x] = 1 + rand() % Highest;
while (DailyTemp[x] <= Lowest);
Total += DailyTemp[x];
}
Avg = Total / MonthDays[y];
cout << MonthName[y] <<"'s Average Temperature: " << Avg << endl;
}