-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
201 lines (201 loc) · 8.22 KB
/
main.c
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 <stdio.h>
/*Helper - Uzair - Convert 365 Day format to DDMM*/
void convertToDDMM(int day, int *MM, int *DD){
int mday[] = {31,28,31,30,31,30,31,31,30,31,30,31}, sum = 0, i = 0;
while (day > sum) {
sum += mday[i];
i++;
}
*MM = i;
*DD = (mday[i-1])-(sum-day);
}
/* Part 1 - Uzair*/
void averages(const double data[365][8], char *nameData[6], double averageData[6]){
printf("\nQuestion 1\n");
/*sum all the temperatures for each lake on each day*/
for (int i = 0; i <365; i++){
for(int j = 0; j<6; j++) averageData[j] += data[i][j+2];
}
/*divide by the number of days and print*/
printf("Lake\t\t\tTemperature\n");
for (int i = 0; i<6; i++){
averageData[i] /= 365;
printf("Lake %s Average:\t%5.2lf\n", nameData[i], averageData[i]);
}
printf("\t\t\t--------\n");
}
/* Part 2 - Uzair*/
void avgAnalysis(const double averageData[6], char *nameData[6]){
printf("\nQuestion 2\n");
/*initialize variables*/
double avgavg = 0;
int coldest = 0, hottest = 0;
/*sum all the numbers and divide by number of terms for average*/
for (int i = 0; i<6; i++) avgavg+=averageData[i];
avgavg /= 6;
printf("Average temperature of all six lakes: %.2lf Degrees Celsius\n", avgavg);
/*go through the data to locate the coldest and hottest lakes*/
for(int i = 0; i<6; i++){
if (averageData[coldest] > averageData[i]) coldest = i;
if (averageData[hottest] < averageData[i]) hottest = i;
}
printf("Lake %s had the warmest average temperature of %5.2lf Degrees Celsius.\n", nameData[hottest], averageData[hottest]);
printf("Lake %s had the coldest average temperature of %5.2lf Degrees Celsius.\n", nameData[coldest], averageData[coldest]);
printf("\n");
/*check if above or below and print immediately*/
for (int i = 0; i<6; i++){
if(averageData[i] > avgavg) printf("Lake %s:\tAbove Average Lake Temperature.\n", nameData[i]);
else if(averageData[i] < avgavg) printf("Lake %s:\tBelow Average Lake Temperature.\n", nameData[i]);
else if(averageData[i] == avgavg) printf("Lake %s:\tAverage Lake Temperature.\n", nameData[i]);
}
printf("\t\t\t--------\n");
}
/* Part 3 - Uzair*/
void perLakeHotnCold(const double data[365][8], char *nameData[6], char *monthName[12], int hottestDays[6], int coldestDays[6]){
int coldest = 0, hottest = 0;
printf("\nQuestion 3\n");
printf("Warmest and Coldest Temperatures, Per lake and Overall\n");
printf("Lake\t\tWarmest\t Date\t\tColdest\t Date\n");
/*Go through all the columns in the data (ignore first two)*/
for(int i = 2; i < 8; i++){
int MM=0, MM2=0, DD=0, DD2=0;
/*Go through all the rows to find smallest/largest in each column, then save the index*/
for(int j = 0; j<365; j++){
if(data[coldest][i] > data[j][i]) coldest = j;
else if(data[hottest][i] < data[j][i]) hottest = j;
}
/* Save index to an array of vertical indexes for future reference*/
coldestDays[i-2]=coldest;
hottestDays[i-2]=hottest;
/*Convert 365 form to DDMM and print*/
convertToDDMM((int)data[hottest][1], &MM, &DD);
convertToDDMM((int)data[coldest][1], &MM2, &DD2);
printf("Lake %s:\t%5.2lf \t%s %d\t\t%5.2lf \t%s %d\n", nameData[i-2], data[hottest][i], monthName[MM-1], DD, data[coldest][i], monthName[MM2-1], DD2);
}
printf("\t\t\t--------\n");
}
/* Part 4 - Param*/
void overallHotnCold (const double data[365][8], char *nameData[6], char *monthName[12], int hottestDays[6], int coldestDays[6]){
printf("\nQuestion 4\n");
int hottest = 0, coldest = 0, MM=0,MM2=0,DD=0,DD2=0;
for (int i = 0; i < 6; i++){
if(data[coldestDays[coldest]][i+2] > data[coldestDays[i]][i+2]) coldest = i;
if(data[hottestDays[hottest]][i+2] < data[hottestDays[i]][i+2]) hottest = i;
}
convertToDDMM((int)data[hottestDays[hottest]][1], &MM, &DD);
convertToDDMM((int)data[coldestDays[coldest]][1], &MM2, &DD2);
printf("Lake %s had the warmest overall temperature of %5.2lf Degrees Celsius on %s %d \n", nameData[hottest], data[hottestDays[hottest]][hottest+2], monthName[MM-1], DD);
printf("Lake %s had the coldest overall temperature of %5.2lf Degrees Celsius on %s %d \n", nameData[coldest], data[coldestDays[coldest]][coldest+2], monthName[MM2-1], DD2);
printf("\t\t\t--------\n");
}
/* Part 5 - Param*/
void summerAvgs (const double data[365][8], char *nameData[6]){
printf("\nQuestion 5\n");
printf("Summer Averages (Warmest to Coldest)\n");
double avgOrdered[6];
char *nNameData[6];
for(int i = 0; i < 6; i++){
double avg = 0;
for(int j = 171 ; j < 265; j++) avg += data[j][i+2];
avgOrdered[i]= (avg/93);
nNameData[i]=nameData[i];
}
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
if (avgOrdered[j] < avgOrdered[i]){
double tmp = avgOrdered[i];
char *tmp2 = nNameData[i];
avgOrdered[i] = avgOrdered[j];
nNameData[i] = nNameData[j];
avgOrdered[j] = tmp;
nNameData[j] = tmp2;
}
}
}
for (int i = 0; i < 6; i++) printf("Lake %s:\t%5.2lf Degrees Celsius\n",nNameData[i], avgOrdered[i]);
printf("\t\t\t--------\n");
}
/* Part 6 - Param*/
void winterAvgs (const double data[365][8], char *nameData[6]){
printf("\nQuestion 6\n");
printf("Summer Averages (Warmest to Coldest)\n");
double avgOrdered [6];
char *nNameData[6];
double average;
for( int l = 0 ; l < 6 ; l++) {
double avg = 0;
double avgt =0;
for (int k = 0; k < 79; k++) avg += data[k][l + 2];
for (int d = 354; d < 365; d++) avgt += data[d][l+2];
average = (avgt + avg)/89;
avgOrdered[l] = average;
nNameData[l]=nameData[l];
}
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
if (avgOrdered[j] < avgOrdered[i]){
double tmp = avgOrdered[i];
char *tmp2 = nNameData[i];
avgOrdered[i] = avgOrdered[j];
nNameData[i] = nNameData[j];
avgOrdered[j] = tmp;
nNameData[j] = tmp2;
}
}
}
for (int i = 0; i < 6; i++) printf("Lake %s:\t%5.2lf Degrees Celsius\n",nNameData[i], avgOrdered[i]);
printf("\t\t\t--------\n");
}
/* Part 7 - Karthik*/
void comfyWaters (const double data[365][8], char *nameData[6]){
printf("\nQuestion 7\n");
printf("Days Above 20 Degrees Celsius\n");
int niceDays[6]= {0};
for(int i = 2; i < 8; i ++){
for(int j = 0; j < 365; j++){
if(data[j][i] > 20) niceDays[i-2]++;
}
printf("Lake %s:\t%5d Warm Days\n", nameData[i-2], niceDays[i-2]);
}
printf("\t\t\t--------\n");
}
/* Part 8 - Karthik*/
void yourMixtape (const double data[365][8], char *nameData[6]){
printf("\nQuestion 8\n");
printf("Days Below 0 Degrees Celsius\n");
int freezeDays[6]= {0};
for(int i = 2; i < 8; i ++){
for(int j = 0; j < 365; j ++){
if(data[j][i] < 0) freezeDays[i-2]++;
}
printf("Lake %s:\t%5d Freezing Days\n",nameData[i-2], freezeDays[i-2]);
}
printf("\t\t\t--------\n");
}
/*Main Function - Uzair*/
int main() {
char *nameData[6] = {"Superior","Michigan","Huron","Erie","Ontario","St. Clair"}, *monthName[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
/* Initialize 2D Array for data*/
double data[365][8],averageData[6] = {0};
int coldestDays[6] = {0}, hottestDays[6] = {0};
/* Read from File and add to array*/
FILE *file;
file = fopen("LakeTemp2017.dat", "r");
for (int i = 0; i<365; i++){
for (int j = 0; j < 8; ++j) {
double n;
fscanf(file, "%lf", &n);
data[i][j] = n;
}
}
/*Run Per-Question Functions*/
averages(data, nameData, averageData);
avgAnalysis(averageData, nameData);
perLakeHotnCold(data, nameData, monthName, hottestDays, coldestDays);
overallHotnCold(data, nameData, monthName, hottestDays, coldestDays);
summerAvgs(data,nameData);
winterAvgs(data,nameData);
comfyWaters(data, nameData);
yourMixtape(data, nameData);
return 0;
}