-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHan_Question4.cpp
More file actions
354 lines (288 loc) · 10.7 KB
/
Copy pathHan_Question4.cpp
File metadata and controls
354 lines (288 loc) · 10.7 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
// Important note: I created two separate functions in this program to answer Question 4c(i) and 4c(ii). These are called in lines
// 347 and 348, one at a time - resulting in the desired output in the files called "outputYearData.txt" and "outputMonthData.txt".
// The plotting was done in a separate program (check "Han_PlotsQuestion4.cpp").
// Please note that only one of these functions should be called at a time (while the other can be called in another run)
// for optimal results :)
class DailyMeteoEvent{
private:
string theCityName;
int theDay, theMonth, theYear, theJulianDate;
double theMaxT, theMinT;
public:
string getCityName()
{
return theCityName;
}
void setCityName(string str)
{
theCityName = str;
}
int getDay()
{
return theDay;
}
void setDay(int d)
{
theDay = d;
}
int getMonth()
{
return theMonth;
}
void setMonth(int m)
{
theMonth = m;
}
int getYear()
{
return theYear;
}
void setYear(int y)
{
theYear = y;
}
int getJulian()
{
return theJulianDate;
}
void setJulian(int j)
{
theJulianDate = j;
}
double getMaxT()
{
return theMaxT;
}
void setMaxT(double max)
{
theMaxT = max;
}
double getMinT()
{
return theMinT;
}
void setMinT(double min)
{
theMinT = min;
}
double GetMeanTOfDate(int d, int m, int y)
{
double mean = (theMaxT + theMinT)/2;
return mean;
}
};
// defining structure to hold year, max temp for that year, min temp for that year
struct absMinMax_Year
{
int year;
double absmin_year;
double absmax_year;
};
// defining structure to hold month, max temp for that month, min temp for that month
struct absMinMax_Month
{
int month;
double absmin_month;
double absmax_month;
};
// a function to answer the first part of Q4c
void question4c_one(DailyMeteoEvent entries[], int number_of_Entries)
{
// output all the required data to this file directly from cout
freopen("outputYearData.txt","w",stdout);
// setting initial values
int yearNow = entries[0].getYear();
double minT_yearNow = entries[0].getMinT();
double maxT_yearNow = entries[0].getMaxT();
// array containing structs absMinMax_Year corresponding to each year
absMinMax_Year answer1[41]; // since there are 41 years
int x = 0; // iterates over each object in entries[] (an array containing objects corresponding to each line in the input file)
int b = 0; // iterates over the array answer1
while (x <= number_of_Entries)
{
if (entries[x].getYear() != yearNow)
{
std::cout << yearNow << " " << maxT_yearNow << " " << minT_yearNow << endl;
// setting the corresponding element in answer1 to the correct values
answer1[b].year = yearNow;
answer1[b].absmin_year = minT_yearNow;
answer1[b].absmax_year= maxT_yearNow;
b++;
// resetting the temp variables to appropriate starting points
yearNow = entries[x].getYear();
minT_yearNow = entries[x].getMinT();
maxT_yearNow = entries[x].getMaxT();
}
else
{ // in order to get to the min and max values for each year - minT_yearNow and maxT_yearNow will be updated accordingly
if (entries[x].getMinT() < minT_yearNow)
{
minT_yearNow = entries[x].getMinT();
}
if (entries[x].getMaxT() > maxT_yearNow)
{
maxT_yearNow = entries[x].getMaxT();
}
}
x++;
}
// now to answer the question about the min and max temp along the entire interval
double absmin_year_overall = answer1[0].absmin_year; //min temp along entire interval
double absmax_year_overall = answer1[0].absmax_year; //max temp along entire interval
int k = 0;
while (k < 41) //41 years
{
if (answer1[k].absmin_year < absmin_year_overall)
{
absmin_year_overall = answer1[k].absmin_year;
}
if (answer1[k].absmax_year > absmax_year_overall)
{
absmax_year_overall = answer1[k].absmax_year;
}
k++;
}
std::cout << "Absolute Max Temperature across entire period is : " << absmax_year_overall << endl;
std::cout << "Absolute Min Temperature across entire period is : " << absmin_year_overall << endl;
fclose (stdout);
}
// function to answer the second part of 4c
void question4c_two(DailyMeteoEvent entries[], int number_of_Entries)
{
// setting intiial values
int monthNow = entries[0].getMonth();
double minT_monthNow = entries[0].getMinT();
double maxT_monthNow = entries[0].getMaxT();
// array to store answers
absMinMax_Month answer2[12]; //since there are 12 months
// set up the months in answer2
for (int i = 1; i <= 12; i++)
{
answer2[i-1].month = i;
}
int x = 0; // iterates over each object in entries[] (an array containing objects corresponding to each line in the input file)
int b = 0; // iterates over each element in the array answer2
while (x <= number_of_Entries)
{
if (entries[x].getMonth() != monthNow) // when we change months
{
if (entries[x].getYear() == 1960) // for the first line of code we read
{
answer2[b].absmax_month = maxT_monthNow; // we simply set the answer[2] values to the "now" values
answer2[b].absmin_month = minT_monthNow;
}
else // here we change the answer[2] values (which are supposed to the 'correct' ones) if needed
{
if (answer2[b].absmax_month < maxT_monthNow)
{
answer2[b].absmax_month = maxT_monthNow;
}
if (answer2[b].absmin_month > minT_monthNow)
{
answer2[b].absmin_month = minT_monthNow;
}
}
b++;
if (b==12) // we reset it to zero when b = 12 because our array answer2 contains only 12 months ie 12 elements
b=0;
// resetting the temp variables
monthNow = entries[x].getMonth();
minT_monthNow = entries[x].getMinT();
maxT_monthNow = entries[x].getMaxT();
}
else
{ // to find the min and max T values within a particular month
if (entries[x].getMinT() < minT_monthNow)
{
minT_monthNow = entries[x].getMinT();
}
if (entries[x].getMaxT() > maxT_monthNow)
{
maxT_monthNow = entries[x].getMaxT();
}
}
x++;
}
fstream outputMonthData;
outputMonthData.open("outputMonthData.txt", ios::out);
for (int i = 0; i < 12; i++)
{
outputMonthData << answer2[i].month << " " << answer2[i].absmax_month << " " << answer2[i].absmin_month << endl;
}
outputMonthData.close();
}
// function to check whether a string contains a digit
bool isNumber(const string& line)
{
for (char const &c : line) {
if (isdigit(c) == 0) return false;
}
return true;
}
int main()
{
// first we want to count the number of lines in our files
fstream countFile;
countFile.open("outputFile.txt", ios::in);
int number_of_Entries = 0; //aka number of lines
string line;
if (countFile.is_open()){
while (getline(countFile, line)) {
if (isNumber(line.substr(0, 1))){
number_of_Entries++;}
}
}
countFile.close();
// -----------------------------------------------------------------
// now we start the actual part
DailyMeteoEvent entries[number_of_Entries]; // defining an array where each element is an object of class DailyMeteoEvent
fstream inputFile;
int x = -1; // to iterate through the lines in the file
int t = 0; // to iterate over each element in the string called details
string temp;
string details[6]; // to store details related to a particular date after original string is broken into component strings for day, date, month, etc
inputFile.open("outputFile.txt", ios::in);
if (inputFile.is_open()){
while (getline(inputFile, line)) {
if (isNumber(line.substr(0, 1)))
{// now we have a line of the form 18;1;1960;2436952;10.8;-1.4; that we need to assign correctly
// to each "pocket" of the object to fill it up
t = 0;
for (int j = 0; j < (int)line.size(); j++) { // interating through each character in the line string
if (line[j] != ';') { // if we encounter a character that is not ';' then
temp += line[j]; // that character gets added to the temp string
}
else { // if we encounter a ';' then
details[t] = temp; //
t++;
temp = "";
}
}
x++;
// now the array details[] has all the items but in string form. we feed it into the object after typecasting correctly.
// iterator that iterates around the entries[] ie the array of objects is : x
entries[x].setDay(stoi(details[0]));
entries[x].setMonth(stoi(details[1]));
entries[x].setYear(stoi(details[2]));
entries[x].setJulian(stoi(details[3]));
entries[x].setMaxT(stod(details[4]));
entries[x].setMinT(stod(details[5]));
}
}
}
// at this stage, we have a completely filled array of objects called entries[], all data from the file is input-ed into this array.
// in the beginning of this program, we have defined the following functions which allow us to get the results that we require:
question4c_one(entries, number_of_Entries);
// question4c_two(entries, number_of_Entries);
// NOTE: only call one of the functions at a time
// THE RESULTS ARE STORED IN "outputMonthData.txt" AND "outputYearData.txt"
inputFile.close();
}