Christmas Toys

Code

Here is a program that creates a list of toy requests for a list of children, then assigns toys they get based on their goodness (or badness!).

Program Code:

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>

using namespace std;

// Function to generate a random name
string generateRandomName() {
const vector<string> names = {"Alice", "Bob", "Charlie", "Daisy", "Ethan", "Fiona", "George", "Hannah", "Ivy", "Jack"};
return names[rand() % names.size()];
}

// Function to generate a random toy
string generateRandomToy() {
const vector<string> toys = {"Teddy Bear", "Toy Car", "Lego Set", "Doll", "Action Figure", "Puzzle", "Board Game", "Yo-Yo"};
return toys[rand() % toys.size()];
}

// Function to generate random toy requests
vector<string> generateToyRequests(int averageToys) {
vector<string> toyList;
int numToys = max(1, rand() % (2 * averageToys)); // Random toys around the average
for (int i = 0; i < numToys; ++i) {
toyList.push_back(generateRandomToy());
}
return toyList;
}

int main() {
srand(time(0)); // Seed random number generator

// Input: Number of children and average number of toys
int numChildren, avgToys;
cout << "Enter the number of children: ";
cin >> numChildren;
cout << "Enter the average number of toys per child: ";
cin >> avgToys;

// Vector to store children and their toy requests
vector<pair<string, vector<string>>> children;

// Generate random children and toy requests
for (int i = 0; i < numChildren; ++i) {
string childName = generateRandomName();
vector<string> toyRequests = generateToyRequests(avgToys);
children.push_back({childName, toyRequests});
}

// Process goodness scores and decide toy allocation
cout << "\nResults:\n";
cout << "----------------------------------\n";
for (auto &child : children) {
string name = child.first;
vector<string> requestedToys = child.second;

// Random goodness score (1-100)
int goodnessScore = rand() % 100 + 1;

// Allocate toys based on goodness score
vector<string> grantedToys;
for (size_t i = 0; i < requestedToys.size(); ++i) {
if (goodnessScore >= 50 || (rand() % 100) < goodnessScore) { // Grant toys based on goodness
grantedToys.push_back(requestedToys[i]);
}
}

// Print results
cout << "Child: " << name << "\n";
cout << "Goodness Score: " << goodnessScore << "\n";
cout << "Requested Toys: ";
for (const string &toy : requestedToys) {
cout << toy << ", ";
}
cout << "\nGranted Toys: ";
for (const string &toy : grantedToys) {
cout << toy << ", ";
}
cout << "\n----------------------------------\n";
}

return 0;
}

Explanation:

  1. Random Name and Toy Generation:
    • Random names are picked from a predefined list.
    • Each child is assigned a random number of toys (based on the average).
  2. Goodness Scoring:
    • Each child gets a random goodness score between 1 and 100.
    • Toys are granted based on the score. High goodness scores increase the likelihood of receiving more toys.
  3. Data Structure:
    • A vector<pair<string, vector<string>>> maps each child’s name to their list of toy requests.
  4. Results:
    • The program prints each child, their goodness score, requested toys, and the toys they will actually receive.

Sample Output:

Enter the number of children: 3
Enter the average number of toys per child: 3

Results:
----------------------------------
Child: Alice
Goodness Score: 78
Requested Toys: Teddy Bear, Puzzle, Lego Set,
Granted Toys: Teddy Bear, Puzzle, Lego Set,
----------------------------------
Child: Charlie
Goodness Score: 42
Requested Toys: Doll, Toy Car,
Granted Toys: Doll,
----------------------------------
Child: Daisy
Goodness Score: 95
Requested Toys: Board Game, Yo-Yo, Puzzle, Action Figure,
Granted Toys: Board Game, Yo-Yo, Puzzle, Action Figure,
----------------------------------

Customization:

  • You can tweak toy probabilities and goodness thresholds to make the program more “magical” or challenging.
  • Add more names or toys to the lists for greater variety.


Scroll to Top