Calendar/Days of the Week Program

Sep 26, 2008 at 9:37pm
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.

here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int DailyTemp[365];
	int Total = 0, Avg = 0;
	int HighestTemp, LowestTemp;
	unsigned seed = time(0);
	srand(seed);

	for (int x = 0; x <= 30; x++)				// January
	{
		do
		DailyTemp[x] = 1 + rand() % 38;
		while (DailyTemp[x] <= 25);

		Total += DailyTemp[x];
	}
		
	Avg = Total / 31;
	cout << "January's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 31; x <= 58; x++)				// February
	{
		do
		DailyTemp[x] = 1 + rand() % 40;
		while (DailyTemp[x] <= 26);

		Total += DailyTemp[x];
	}

	Avg = Total / 28;
	cout << "February's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 59; x <= 89; x++)				// March
	{
		do
		DailyTemp[x] = 1 + rand() % 48;
		while (DailyTemp[x] <= 34);

		Total += DailyTemp[x];
	}

	Avg = Total / 31;
	cout << "March's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for  (int x = 90; x <= 119; x++)			// April
	{
		do
		DailyTemp[x] = 1 + rand() % 58;
		while (DailyTemp[x] <= 43);

		Total += DailyTemp[x];
	}

	Avg = Total / 30;								// May
	cout << "April's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 120; x <= 151; x++)
	{
		do
		DailyTemp[x] = 1 + rand() % 68;
		while (DailyTemp[x] <= 52);

		Total += DailyTemp[x];
	}

	Avg = Total / 31;
	cout << "May's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 152; x <= 180; x++)			// June
	{
		do
		DailyTemp[x] = 1 + rand() % 77;
		while (DailyTemp[x] <= 62);

		Total += DailyTemp[x];
	}

	Avg = Total / 30;
	cout << "June's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 181; x <= 211; x++)			// July
	{
		do
		DailyTemp[x] = 1 + rand() % 87;
		while (DailyTemp[x] <= 68);

		Total += DailyTemp[x];
	}

	Avg = Total / 31;
	cout << "July's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 212; x <= 242; x++)			// August
	{
		do
		DailyTemp[x] = 1 + rand() % 75;
		while (DailyTemp[x] <= 60);

		Total += DailyTemp[x];
	}

	Avg = Total / 31;
	cout << "August's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 243; x <= 272; x++)			// September
	{
		do
		DailyTemp[x] = 1 + rand() % 65;
		while (DailyTemp[x] <= 49);
		
		Total += DailyTemp[x];
	}

	Avg = Total / 30;
	cout << "September's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 273; x <= 304; x++)			// October
	{
		do
		DailyTemp[x] = 1 + rand() % 54;
		while (DailyTemp[x] <= 41);
		
		Total += DailyTemp[x];
	}

	Avg = Total / 31; 
	cout << "October's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 305; x <= 333; x++)			// November
	{
		do
		DailyTemp[x] = 1 + rand() % 43;
		while (DailyTemp[x] <= 31);

		Total += DailyTemp[x];
	}

	Avg = Total / 30;
	cout << "November's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	for (int x = 334; x <= 364; x++)			// December
	{
		do
		DailyTemp[x] = 1 + rand() % 61;
		while (DailyTemp[x] <= 47);

		Total += DailyTemp[x];
	}

	Avg = Total / 31;
	cout << "December's Average Temperature: " << Avg << endl;
	Avg = 0;
	Total = 0;

	cout << "----------------" << endl;

	HighestTemp = DailyTemp[0];
	for (int x = 1; x < 365; x++)
	{
		if (DailyTemp[x] > HighestTemp)
			HighestTemp = DailyTemp[x];
	}


	LowestTemp = DailyTemp[0];
	for (int x = 1; x < 365; x++)
	{
		if (DailyTemp[x] < LowestTemp)
			LowestTemp = DailyTemp[x];
	}


	return 0;
}
Sep 26, 2008 at 10:22pm
Data driven programming. Or use functions.

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.



Sep 27, 2008 at 12:16am
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.
Last edited on Sep 27, 2008 at 12:25am
Sep 27, 2008 at 4:43pm
What if() statement are you having problems writing? The only if()s in your code above
look fine.
Sep 27, 2008 at 6:21pm
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.
Sep 27, 2008 at 11:18pm
*Just noticed the code*
Dear god, man!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char *month_names[]={
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
};
char month_lengths[]={31,28,31,30,31,30,31,31,30,31,30,31};

While you're writing something like that, doesn't the thought of "could I be doing something wrong" ever cross your mind?
Last edited on Sep 28, 2008 at 11:02am
Sep 28, 2008 at 5:45am
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;
 }
Last edited on Sep 28, 2008 at 7:00am
Topic archived. No new replies allowed.