-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz3_eseneo1.c
443 lines (361 loc) · 11.8 KB
/
quiz3_eseneo1.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* SCENARIO
You are going grocery shopping.
The following coding challenges will task you to do the following:
- Determine if you need to refuel your car based on the given fuel level in the tank
- Count how many broken eggs are in a carton containing a dozen
- Determine the freshest bag of milk from a given set of dates
- Locate the aisle based on a desired grocery item in a store
- Recursively search the entire store to determine the aisle of a grocery item
You have 25 minutes, minus the time you spent reading this! Let's get shopping!
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
void setRandomSeed(int seedValue);
void generateDozen();
float checkFuelLevel();
int *generateBestBeforeDates(int *bytes);
char *getItemFromList();
char *getItemToFind();
void generateStore();
int searchAisle();
bool challenge1()
{
/*
On the drive to the grocery store, you check if your gas tank needs
to be refueled. If the tank is less than 25%, you refuel. Otherwise,
do not refuel the tank.
INPUT: fuelLevel will have a value between 0 and 1.
OUTPUT: Whether it is necessary to refuel the gas tank or not.
*/
bool refuel;
float fuelLevel;
fuelLevel = checkFuelLevel(); // DO NOT EDIT THIS FUNCTION. THE VALUE IS GENERATED FOR YOU.
/* ENTER YOUR CODE HERE*/
if(fuelLevel<0.25){
refuel = False;
}
else{
refuel = True;
}
/* STOP WRITING YOUR CODE HERE*/
if (refuel)
{
printf("Refuel");
}
else
{
printf("Do not refuel");
}
printf("\n");
return refuel;
}
int challenge2()
{
/*
The eggs at the store are very bad quality. Many eggs are broken
Let a dozen of eggs be a 2 x 6 matrix, where each egg is a cell of
the matrix. The "egg" has value 0 if it is GOOD and a value of -1
if it is BROKEN.
INPUT: A 2 x 6 integer array of values either 0 or -1.
OUTPUT: Count the number of BROKEN eggs (-1) in the dozen.
*/
int dozen[2][6];
generateDozen(dozen); // DO NOT EDIT THIS FUNCTION. THE ARRAY IS GENERATED FOR YOU.
int brokenEggCount = 0;
/* ENTER YOUR CODE HERE*/
for(int i = 0; i<2, i++){
for(int j = ; j<6; j++){
if(dozen[i][j]==-1){
brokenEggCount++;
}
}
}
/* STOP WRITING YOUR CODE HERE*/
printf("Number of broken eggs:\t%d\n", brokenEggCount);
return brokenEggCount;
}
int challenge3()
{
/*
You want to purchase the freshest milk on the shelf. Choose the milk with
the furthest Best Before date in the future. The Best Before dates are
given between September 1st, 2023 (230901) and October 31st, 2023 (231031).
INPUT: An integer array of unknown length of values between 230901-230930 and 231001-231031.
There will be an arbitrary amount of dates to check.
You are provided with numBytes, the size of the array in bytes.
You are guaranteed the array ends with a 0.
OUTPUT: The newest best before date from the input list.
*/
int numBytes;
int *bestBeforeDates = generateBestBeforeDates(&numBytes);
int freshestMilk = 0;
/* ENTER YOUR CODE HERE*/
int date;
for(int i = 0; i<bestBeforeDates.size; i++){
if(bestBeforeDates[i]>=230901 && bestBeforeDates[i]<=231031){
date = bestBeforeDates[i]
}
}
freshestMilk = date;
/* STOP WRITING YOUR CODE HERE*/
printf("The Best Before of the freshest milk is:\t%d\n", freshestMilk);
return freshestMilk;
}
int challenge4()
{
/*
You are implementing an algorithm to tell you the aisle a grocery
item is in. You successfully managed to make it with a nested-switch
block. This is not an intuitive way to code this and it is difficult
to read. Adding more items will add more layers to the switch block.
GOAL: Convert the below switch block into an if, else if, else block.
*/
char *groceryItem = getItemFromList();
int aisleNum = -1;
/* ENTER YOUR CODE HERE*/
/*
switch (strcmp(groceryItem, "Cheerios")) {
case 0:
aisleNum = 6;
break;
default:
switch (strcmp(groceryItem, "Kraft Dinner")) {
case 0:
aisleNum = 7;
break;
default:
aisleNum = 0;
break;
}
break;
}*/
if(groceryItem =='Cheerios'){
aisleNum = 6;
}
else if(groceryItem == "Kraft Dinner"){
aisleNum = 7;
}
else{
aisleNum = 0;
}
/* STOP WRITING YOUR CODE HERE*/
printf("Grocery Item:\t%s\tAisle Number:\t%d\n", groceryItem, aisleNum);
return aisleNum;
}
int challenge5()
{
/*
Let's try a different approach to finding the Aisle Number of an item.
Rather than checking the Aisle Number corresponding to a specific item,
you will recursively search for an item throughout the entire store,
aisle by aisle, until you find the aisle that the item is located in.
This has been implemented in the function searchAisle, but the recursion
is not fully working. Figure out what's wrong and fix it!
GOAL: Fix the recursion in searchAisle() defined below, just after this function
Do not edit the challenge5() function. Only edit searchAisle().
*/
// Select an item to find in the store
char *itemToLocate = getItemToFind(); // DO NOT EDIT THIS FUNCTION. THE VALUE IS GENERATED FOR YOU.
// Create the list of items on each aisle of the store
int numAisles = 10;
char* storeList[numAisles];
generateStore(storeList); // DO NOT EDIT THIS FUNCTION. THE VALUE IS GENERATED FOR YOU.
// Recursively search to find the located aisle
int locatedAisle = searchAisle(itemToLocate, storeList, numAisles, 0, 0);
printf("%s\n", itemToLocate);
if (locatedAisle != -1)
{
printf("Item found in Aisle %d\n", locatedAisle); // Adding 1 because aisles are typically numbered starting from 1.
}
else if (locatedAisle == -2)
{
printf("You got lost!.\n");
}
else
{
printf("Item not found in any aisle.\n");
}
return locatedAisle;
}
int searchAisle(const char *item, const char *aisles[], int numAisles, int currentAisle, int areYouLost)
{
if (areYouLost == 100)
{
// This is preventing the code from running into a segmentation fault
printf("You're lost in the store (you have infinite recursion)\n");
return -2;
}
// Base case: All aisles have been searched, and the item hasn't been found
if (currentAisle == numAisles)
{
return 0;
}
// Search for the item in the current aisle.
const char *currentItems = aisles[currentAisle];
if (strstr(currentItems, item) != NULL)
{
return currentAisle; // Found the item in the current aisle.
}
// Recursively search the next aisle.
return searchAisle(item, aisles, numAisles, ++currentAisle, ++areYouLost);
}
// ------------- IGNORE FROM HERE ------------- //
int main(int argc, char *argv[]) {
if (argc < 2) {
setRandomSeed(0);
challenge1();
challenge2();
challenge3();
challenge4();
challenge5();
} else {
int seedValue = atoi(argv[1]);
setRandomSeed(seedValue);
bool test1 = challenge1();
int test2 = challenge2();
int test3 = challenge3();
int test4 = challenge4();
int test5 = challenge5();
char file1_name[256], file2_name[256], file3_name[256], file4_name[256], file5_name[256];
snprintf(file1_name, sizeof(file1_name), "test1_%i.txt", seedValue);
snprintf(file2_name, sizeof(file2_name), "test2_%i.txt", seedValue);
snprintf(file3_name, sizeof(file3_name), "test3_%i.txt", seedValue);
snprintf(file4_name, sizeof(file4_name), "test4_%i.txt", seedValue);
snprintf(file5_name, sizeof(file5_name), "test5_%i.txt", seedValue);
FILE *file1 = fopen(file1_name, "w");
FILE *file2 = fopen(file2_name, "w");
FILE *file3 = fopen(file3_name, "w");
FILE *file4 = fopen(file4_name, "w");
FILE *file5 = fopen(file5_name, "w");
// Check if file openings were successful
if (file1 == NULL || file2 == NULL || file3 == NULL || file4 == NULL || file5 == NULL) {
printf("Error opening files!\n");
exit(1);
}
// Write test results to their respective files
fprintf(file1, "%d", test1);
fprintf(file2, "%d", test2);
fprintf(file3, "%d", test3);
fprintf(file4, "%d", test4);
fprintf(file5, "%d", test5);
// close the files
fclose(file1), fclose(file2), fclose(file3), fclose(file4), fclose(file5);
return 0;
}
}
void setRandomSeed(int seed)
{
if (seed == 0)
{
seed = (int)time(NULL);
}
srand(seed); // Set the seed
}
float checkFuelLevel()
{
// Generate a random float between 0 and 1
return (float)rand() / (float)RAND_MAX;
}
void generateDozen(int array[2][6])
{
// Generate and populate the array with random -1s and 0s
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 6; j++)
{
// Generate a random number that's either -1 or 0
array[i][j] = (rand() % 2 == 0) ? 0 : -1;
}
}
}
int *generateBestBeforeDates(int *bytes)
{
// Generate a random number of best before dates
int numDates = (rand() % 30);
// Allocate memory for the array
int *array = (int *)malloc((numDates + 1) * sizeof(int));
*bytes = numDates * sizeof(int);
if (array == NULL)
{
perror("Memory allocation failed");
exit(1);
}
int randVal;
for (int i = 0; i < numDates; i++)
{
// Generate a random integer between min and max (inclusive)
randVal = (rand() % 61);
if (randVal > 29)
{
array[i] = randVal + 231001 - 30;
}
else
{
array[i] = randVal + 230901;
}
// printf("%d\n", array[i]);
}
array[numDates] = 0;
return array;
}
char *getItemFromList()
{
// An array of strings
char *groceryItems[] = {
"Apples",
"Bananas",
"Bread",
"Cheerios",
"Eggs",
"Milk",
"Kraft Dinner",
"Spinach",
"Chicken",
"Oreos"};
int length = sizeof(groceryItems) / sizeof(groceryItems[0]);
for (int i = length - 1; i > 0; i--)
{
int j = rand() % (i + 1);
// Swap the strings at positions i and j
char *temp = groceryItems[i];
groceryItems[i] = groceryItems[j];
groceryItems[j] = temp;
}
return groceryItems[0];
}
char *getItemToFind()
{
char *listOfItems[32] = {
"Pineapple", "Bananas", "Bread", "Milk", "Eggs",
"Cheese", "Chicken", "Rice", "Tomatoes", "Potatoes",
"Onions", "Pasta", "Cereal", "Yogurt", "Oranges",
"Carrots", "Broccoli", "Salmon", "Applesauce", "Pepper",
"Salt", "Peanut Butter", "Jelly", "Lettuce", "Cucumbers",
"Ketchup", "Mustard", "Mayonnaise", "Pickles", "Kraft Dinner",
"Oreos", "Gingerale"};
int length = sizeof(listOfItems) / sizeof(listOfItems[0]);
int i = rand() % (length);
return listOfItems[i];
}
void generateStore(char **storeList)
{
char *aisleItems[10] = {
"",
"Bananas, Bread, Milk, Eggs",
"Cheese, Chicken, Rice, Tomatoes",
"Potatoes, Onions, Pasta",
"Yogurt, Oranges, Carrots, Broccoli",
"Salmon, Applesauce, Pepper, Salt",
"Peanut Butter, Jelly",
"Lettuce, Cucumbers",
"Pickles",
"Ketchup, Mustard, Mayonnaise"};
for (int i = 0; i < 10; i++)
{
storeList[i] = (char *)malloc(256); // Allocate memory for each string
strcpy(storeList[i], aisleItems[i]);
}
}