-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeory_assignment.cpp
More file actions
277 lines (255 loc) · 7.02 KB
/
theory_assignment.cpp
File metadata and controls
277 lines (255 loc) · 7.02 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
#include <iostream>
#include <string>
#include <limits>
using namespace std;
const int MAX_YEARS = 5; // max years
const int MAX_CITIES = 15; // max cities
const double SENTINEL = -9999.0; // represents missing data
// ================= Weather Record ADT =================
class WeatherRecord
{
public:
string date; // Format: DD/MM/YYYY
string city;
double temperature;
WeatherRecord(string d = "", string c = "", double t = SENTINEL)
: date(d), city(c), temperature(t) {}
};
// ================= Weather Data Storage =================
class WeatherDataStorage
{
private:
double data[MAX_YEARS][MAX_CITIES];
string cities[MAX_CITIES];
int years[MAX_YEARS];
int cityCount, yearCount;
public:
WeatherDataStorage(int y[], int yCount, string c[], int cCount)
{
yearCount = yCount;
cityCount = cCount;
for (int i = 0; i < yearCount; i++)
years[i] = y[i];
for (int j = 0; j < cityCount; j++)
cities[j] = c[j];
// initialize with sentinel
for (int i = 0; i < yearCount; i++)
{
for (int j = 0; j < cityCount; j++)
{
data[i][j] = SENTINEL;
}
}
}
int getYearIndex(int year)
{
for (int i = 0; i < yearCount; i++)
{
if (years[i] == year)
return i;
}
return -1;
}
int getCityIndex(string city)
{
for (int j = 0; j < cityCount; j++)
{
if (cities[j] == city)
return j;
}
return -1;
}
// Insert new record
void insert(const WeatherRecord &rec)
{
int year = stoi(rec.date.substr(6, 4)); // extract year
int y = getYearIndex(year);
int c = getCityIndex(rec.city);
if (y != -1 && c != -1)
{
data[y][c] = rec.temperature;
cout << "Record inserted successfully.\n";
}
else
{
cout << "Invalid year or city!\n";
}
}
// Delete record (set to sentinel)
void remove(string city, int year)
{
int y = getYearIndex(year);
int c = getCityIndex(city);
if (y != -1 && c != -1)
{
data[y][c] = SENTINEL;
cout << "Record deleted successfully.\n";
}
else
{
cout << "Invalid year or city!\n";
}
}
// Retrieve temperature data
double retrieve(string city, int year)
{
int y = getYearIndex(year);
int c = getCityIndex(city);
if (y != -1 && c != -1)
{
return data[y][c];
}
return SENTINEL;
}
// Row-major access
void rowMajorAccess()
{
cout << "\nRow-Major Order (Year-wise):\n";
for (int i = 0; i < yearCount; i++)
{
for (int j = 0; j < cityCount; j++)
{
cout << "Year " << years[i] << ", City " << cities[j] << " -> ";
if (data[i][j] == SENTINEL)
cout << "No Data\n";
else
cout << data[i][j] << "°C\n";
}
}
}
// Column-major access
void columnMajorAccess()
{
cout << "\nColumn-Major Order (City-wise):\n";
for (int j = 0; j < cityCount; j++)
{
for (int i = 0; i < yearCount; i++)
{
cout << "City " << cities[j] << ", Year " << years[i] << " -> ";
if (data[i][j] == SENTINEL)
cout << "No Data\n";
else
cout << data[i][j] << "°C\n";
}
}
}
// Sparse data handling
void handleSparseData()
{
int missing = 0, total = yearCount * cityCount;
for (int i = 0; i < yearCount; i++)
{
for (int j = 0; j < cityCount; j++)
{
if (data[i][j] == SENTINEL)
missing++;
}
}
cout << "\nSparse Data Handling:\nMissing entries = "
<< missing << " out of " << total << "\n";
}
// Complexity Analysis
void analyzeComplexity()
{
cout << "\nComplexity Analysis:\n";
cout << "Insert: O(1)\n";
cout << "Delete: O(1)\n";
cout << "Retrieve: O(1)\n";
cout << "Row/Column traversal: O(n*m)\n";
cout << "Space: O(n*m)\n";
}
};
// ================= Main Driver =================
int main()
{
int years[] = {2023, 2024, 2025};
string cities[] = {"Delhi", "Mumbai", "Chennai"};
WeatherDataStorage system(years, 3, cities, 3);
int choice;
do
{
cout << "\nWeather Data Storage System\n";
cout << "1. Insert a record\n";
cout << "2. Delete a record\n";
cout << "3. Retrieve a record\n";
cout << "4. Display data (Row-Major)\n";
cout << "5. Display data (Column-Major)\n";
cout << "6. Handle Sparse Data\n";
cout << "7. Analyze Complexity\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
choice = -1;
}
switch (choice)
{
case 1:
{
string date, city;
double temp;
cout << "Enter date (DD/MM/YYYY): ";
cin >> date;
cout << "Enter city(Delhi/Mumbai/Chennai): ";
cin >> city;
cout << "Enter temperature: ";
cin >> temp;
WeatherRecord rec(date, city, temp);
system.insert(rec);
break;
}
case 2:
{
string city;
int year;
cout << "Enter city to delete: ";
cin >> city;
cout << "Enter year to delete: ";
cin >> year;
system.remove(city, year);
break;
}
case 3:
{
string city;
int year;
cout << "Enter city to retrieve: ";
cin >> city;
cout << "Enter year to retrieve: ";
cin >> year;
double result = system.retrieve(city, year);
if (result == SENTINEL)
{
cout << "No record found.\n";
}
else
{
cout << "Temperature for " << city << " in " << year
<< " is " << result << "°C\n";
}
break;
}
case 4:
system.rowMajorAccess();
break;
case 5:
system.columnMajorAccess();
break;
case 6:
system.handleSparseData();
break;
case 7:
system.analyzeComplexity();
break;
case 8:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please enter a number between 1 and 8.\n";
}
} while (choice != 8);
return 0;
}