Hi iam working on a problem given to me by my teacher.The problem is
!.People from different states send their application to participate in the dance competition organised at the country level.Only 30 people will be selected to take par in the competition.Participants will be selected randomly from one state.No more than 5 participants can take part in the competition.We have to write a function to print names of 20 people selected randomly to take paet in the competition.

Can anyone help me on this question.

Dani AI

Generated

clarified there are six cities and five slots per city; 's array-plus-rand() idea is a fine conceptual start but has real problems (bias, poor randomness, portability). Prefer sampling without replacement using the modern C++ random utilities: store each city's names in a std::vector<std::string>, verify the vector has at least five entries, then draw 5 unique names per city with std::sample (C++17) or std::shuffle + take-first-N for older compilers.

Example (concise, C++17):

#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>

std::mt19937 rng(std::random_device{}()); // or fixed seed for reproducible tests
std::vector<std::string> picked;
std::sample(city.begin(), city.end(), std::back_inserter(picked), 5, rng);

picked will contain up to five unique names (ensure city.size() >= 5 first). Use std::shuffle + std::vector::erase if you need to support pre-C++17. See the C++ docs for details on std::sample and the <random> facilities (std::sample documentation, <random> overview).

Troubleshooting notes: handle duplicates in input (dedupe beforehand), decide behavior when a city has fewer than five candidates, and seed carefully: std::random_device is fine for production randomness; use a fixed seed during unit tests so results are deterministic.

Recommended Answers

All 3 Replies

??? First you say that 30 people will be selected to compete. Then, you say 5 participants can compete. Then, you want the names of 20 competitors. Either make up your mind or clarify your assignment.

??? First you say that 30 people will be selected to compete. Then, you say 5 participants can compete. Then, you want the names of 20 competitors. Either make up your mind or clarify your assignment.

Actually 30 peoples are to be selected 5 from each city and there are 6 cities given.These 5 people should be selected randomly

I would have an array of names of people from each city. That is, have 6 arrays, one for each city.
Then, have a for loop like such:

for (i = 0; i < 5; i++)
{
     result = rand();
     contestant[6*i + 1] = denver[result];
     contestant[6*i + 1] = cleveland[result];
     ........
     ........
     ........
     ........
}

That will fill up the 30 contestant slots with 5 from each city.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.