#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXZONES 5 // Maximum…
- User
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX_ZONES 5 // Maximum number of zones #define STACK_SIZE 10 // Stack size for supplies
// Structures typedef struct { int id; int priority; int people; int supplies; } Zone;
typedef struct { int supplies[STACK_SIZE]; int top; } Stack;
typedef struct Node { int time; char name[20]; struct Node *left, *right; } RescuedPerson;
// Function Prototypes void initStack(Stack *s); int isStackEmpty(Stack *s); int isStackFull(Stack *s); void push(Stack *s, int supply); int pop(Stack *s); void addZone(Zone zones[], int *zoneCount); void prioritizeZones(Zone zones[], int zoneCount); void rescue(Stack *s, Zone zones[], int *zoneCount, RescuedPerson *root); RescuedPerson insertBST(RescuedPerson *root, int time, char name[]); void displayRescued(RescuedPerson *root); void inOrder(RescuedPerson *root);
// Stack Functions void initStack(Stack *s) { s->top = -1; }
int isStackEmpty(Stack *s) { return s->top == -1; }
int isStackFull(Stack *s) { return s->top == STACK_SIZE - 1; }
void push(Stack *s, int supply) { if (!isStackFull(s)) s->supplies[++(s->top)] = supply; }
int pop(Stack *s) { return isStackEmpty(s) ? -1 : s->supplies[(s->top)--]; }
// Priority Queue for Zones void addZone(Zone zones[], int *zoneCount) { if (*zoneCount >= MAX_ZONES) { printf("Cannot add more zones.\n"); return; } int id, priority, people, supplies; printf("Enter Zone ID, Priority (1-5), People, and Supplies: "); scanf("%d %d %d %d", &id, &priority, &people, &supplies); zones[*zoneCount] = (Zone){id, priority, people, supplies}; (*zoneCount)++; }
void prioritizeZones(Zone zones[], int zoneCount) { for (int i = 0; i < zoneCount - 1; i++) { for (int j = 0; j < zoneCount - i - 1; j++) { if (zones[j].priority > zones[j + 1].priority) { Zone temp = zones[j]; zones[j] = zones[j + 1]; zones[j + 1] = temp; } } } }
// Rescue Operation void rescue(Stack *s, Zone zones[], int *zoneCount, RescuedPerson **root) { if (*zoneCount == 0) { printf("No zones available for rescue.\n"); return; }
prioritizeZones(zones, *zoneCount); Zone zone = zones[0]; printf("Rescuing in Zone %d with priority %d.\n", zone.id, zone.priority); int requiredSupplies = zone.people * 1; // 1 supply per person as an assumption if (s->top + 1 < requiredSupplies) { printf("Not enough supplies for rescue in this zone.\n"); return; } // Using supplies for (int i = 0; i < requiredSupplies; i++) pop(s); // Record rescued people for (int i = 0; i < zone.people; i++) { char name[20]; int time; printf("Enter name and time of rescue for person %d: ", i + 1); scanf("%s %d", name, &time); *root = insertBST(*root, time, name); } // Shift remaining zones up for (int i = 1; i < *zoneCount; i++) zones[i - 1] = zones[i]; (*zoneCount)--;}
// Binary Search Tree Insertion and Display RescuedPerson* insertBST(RescuedPerson newNode = (RescuedPerson*)malloc(sizeof(RescuedPerson)); newNode->time = time; strcpy(newNode->name, name); newNode->left = newNode->right = NULL; return newNode; } if (time < root->time) root->left = insertBST(root->left, time, name); else root->right = insertBST(root->right, time, name); return root; }
- Assistant
- User
the previous code was proper for supplies part, just make sure that the supplies added to rescuers is being added to total supplies
- Assistant
- User
the rescue should only occur if a supply is available for a person
- Assistant
- User
i put 1 person in zone and gave rescuers 5 supplies but still rescue operation failed, make sure the supplystack function is working properly. Also each zone can have its own amount of supply which should be provided by the user in add_zone function. these supplies are different from the supplies given to the rescuers, these are used first to rescue.
- Assistant
- User
remove the comments
- Assistant
- User
"Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 1 Enter Zone ID, Priority (1-5), People, and Zone Specific Supplies: 1 2 3 1
Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 1 Enter Zone ID, Priority (1-5), People, and Zone Specific Supplies: 2 3 5 3
Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 2 Enter supply to add: 2
Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 3 Rescuing in Zone 1 with priority 2. Not enough supplies for rescue in this zone.
this is the error
- Assistant
- User
it still says same error
- Assistant
- User
its still now working
- Assistant
- User
revisit the code once again and make it more efficient
- Assistant
- User
its giving error, also add all the comments back to the code
- Assistant
- User
dsa_hackathon.c: In function 'rescue': dsa_hackathon.c:107:17: warning: implicit declaration of function 'insertBST' [-Wimplicit-function-declaration] 107 | *root = insertBST(*root, time, name); | ^~~~~~~~~ dsa_hackathon.c:107:15: warning: assignment to 'RescuedPerson *' {aka 'struct Node *'} from 'int' makes pointer from integer without a cast [-Wint-conversion]
107 | *root = insertBST(*root, time, name); | ^ dsa_hackathon.c: At top level: dsa_hackathon.c:131:16: error: conflicting types for 'insertBST'; have 'RescuedPerson *(RescuedPerson *, int, char *)' {aka 'struct Node *(struct Node *, int, char )'} 131 | RescuedPerson insertBST(RescuedPerson *root, int time, char name[]) { | ^~~~~~~~~ dsa_hackathon.c:107:17: note: previous implicit declaration of 'insertBST' with type 'int()' 107 | *root = insertBST(*root, time, name); | ^~~~~~~~~ dsa_hackathon.c: In function 'displayRescued': dsa_hackathon.c:151:5: warning: implicit declaration of function 'inOrder' [-Wimplicit-function-declaration] 151 | inOrder(root); | ^~~~~~~ dsa_hackathon.c: At top level: dsa_hackathon.c:155:6: warning: conflicting types for 'inOrder'; have 'void(RescuedPerson *)' {aka 'void(struct Node *)'} 155 | void inOrder(RescuedPerson *root) { | ^~~~~~~ dsa_hackathon.c:151:5: note: previous implicit declaration of 'inOrder' with type 'void(RescuedPerson *)' {aka 'void(struct Node *)'} 151 | inOrder(root); - Assistant
- User
Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 1 Enter Zone ID, Priority (1-5), People, and Zone Specific Supplies: 1 2 3 1
Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 1 Enter Zone ID, Priority (1-5), People, and Zone Specific Supplies: 2 3 5 3
Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 2 Enter supply to add: 2
Menu:
- Add Zone
- Add Supply to Rescuers
- Perform Rescue
- Display Rescued People
- Exit Enter your choice: 3 Not enough supplies for rescue in this zone.
- Assistant
- User
the problem is that the stack is considering the amount of supplies given to rescuers as 1
- Assistant
- User
make the code more efficient
- Assistant