o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
You are managing a conference where the number of participants varies between 30 and 50.…
- User
You are managing a conference where the number of participants varies between 30 and 50. You have 5 types of meal boxes (Veg, Non-Veg, Vegan, Gluten-Free, and Halal). Write a C program to allocate meal boxes to participants based on the available count of each type of meal box. Ensure that each participant receives one meal box.
- Assistant
#include <stdio.h> #include <stdlib.h> int main() { int N; // Number of participants int Veg_count, NonVeg_count, Vegan_count, GlutenFree_count, Halal_count; int total_meal_boxes; // Input the number of participants printf("Enter the number of participants (30 to 50): "); scanf("%d", &N); if (N < 30 || N > 50) { printf("Number of participants must be between 30 and 50.\n"); return 1; } // Input the available counts of each type of meal box printf("Enter the available counts of meal boxes:\n"); printf("Veg: "); scanf("%d", &Veg_count); printf("Non-Veg: "); scanf("%d", &NonVeg_count); printf("Vegan: "); scanf("%d", &Vegan_count); printf("Gluten-Free: "); scanf("%d", &GlutenFree_count); printf("Halal: "); scanf("%d", &Halal_count); // Calculate the total number of meal boxes available total_meal_boxes = Veg_count + NonVeg_count + Vegan_count + GlutenFree_count + Halal_count; // Check if there are enough meal boxes for all participants if (total_meal_boxes < N) { printf("Not enough meal boxes for all participants.\n"); return 1; } // Initialize arrays to store meal counts and types int meal_counts[5]; meal_counts[0] = Veg_count; meal_counts[1] = NonVeg_count; meal_counts[2] = Vegan_count; meal_counts[3] = GlutenFree_count; meal_counts[4] = Halal_count; const char* meal_types[5]; meal_types[0] = "Veg"; meal_types[1] = "Non-Veg"; meal_types[2] = "Vegan"; meal_types[3] = "Gluten-Free"; meal_types[4] = "Halal"; // Allocate meal boxes to participants int participant_alloc[N]; // Stores the meal type index allocated to each participant int idx = 0; // Index for the meal_types array for (int i = 0; i < N; i++) { int attempts = 0; // Find the next available meal box while (meal_counts[idx] == 0 && attempts < 5) { idx = (idx + 1) % 5; attempts++; } if (attempts == 5) { printf("Error: Not enough meal boxes during allocation.\n"); return 1; } // Assign meal box to participant participant_alloc[i] = idx; meal_counts[idx]--; // Move to the next meal type for the next participant idx = (idx + 1) % 5; } // Output the allocation printf("\nMeal Box Allocation:\n"); for (int i = 0; i < N; i++) { printf("Participant %d: %s\n", i + 1, meal_types[participant_alloc[i]]); } return 0; }This C program reads the number of participants and the available counts of each type of meal box. It then allocates meal boxes to each participant, ensuring that each participant receives one meal box and that the allocation does not exceed the available counts of meal boxes. The allocation is done in a round-robin fashion, distributing meal boxes evenly based on availability.
Sample Output:
Enter the number of participants (30 to 50): 35 Enter the available counts of meal boxes: Veg: 10 Non-Veg: 10 Vegan: 5 Gluten-Free: 5 Halal: 5 Meal Box Allocation: Participant 1: Veg Participant 2: Non-Veg Participant 3: Vegan Participant 4: Gluten-Free Participant 5: Halal Participant 6: Veg Participant 7: Non-Veg Participant 8: Vegan Participant 9: Gluten-Free Participant 10: Halal Participant 11: Veg Participant 12: Non-Veg Participant 13: Vegan Participant 14: Gluten-Free Participant 15: Halal Participant 16: Veg Participant 17: Non-Veg Participant 18: Veg Participant 19: Non-Veg Participant 20: Veg Participant 21: Non-Veg Participant 22: Veg Participant 23: Non-Veg Participant 24: Veg Participant 25: Non-Veg Participant 26: Veg Participant 27: Non-Veg Participant 28: Veg Participant 29: Non-Veg Participant 30: Veg Participant 31: Non-Veg Participant 32: Veg Participant 33: Non-Veg Participant 34: Veg Participant 35: Non-Veg