Challenge 62

Description

The purpose of this challenge is to use arrays and functions. This challenge creates “teams” of people.

Requirements

  1. Write additional includes for <ctime> and <cstdlib> at the top of your program
  2. Write the sample main as below
    int main()
    {  
      int team_size;
      srand(time(NULL));  
    
      string people[12] = { "Adam", "Benjamin", "Caleb", "Daniel",
       "Ephraim", "Frank", "Gideon" };
    
      // ask the user for 5 more entries to fill in the 
      // blank elements after Gideon
    
      cout << "How big a team to create? ";
      cin >> team_size;
    
      // create a team
      string team = create_team(people, 12, team_size);
      
      cout << "Team: " << team << endl;
      return 0;
    }
  3. In main(), ask the user to enter 5 more names to fill in the remaining slots of the people array. Note above that the people array has 12 elements, but only has 7 filled in.
  4. Write a function string create_team(string people[], int size, int team_size). This function creates a concatenated string of random names from the people array. team_size represents how many people will be used to form the team.
    string create_team(string people[], int size, int team_size)
    {
      string team;
     
      // more code here
    
      return team;
    }
  5. In the create_team() function, create a bool array, selected, of the same size as size, so that it can contain the same number of  elements as people. Initialize all elements of selected to false.
    string create_team(string people[], int size, int team_size)
    {
      string team;
      bool selected[size];
    
      // initialize all elements of selected to false
     
      // more code here
    
      return team;
    }
    
  6. Strings can be concatenated (added together) similarly to other data types, like an accumulator. This technique will be used in the following step.
      string team = "";
      team = team + "sample";   // concatenates "sample" to team
      team = team + " ";        // concatenates a space to team
  7. The code below generates a random integer between 0 and size. In create_team(), use index as a subscript to select a name out of the people array. A do-while loop is given below to select an index for a name that has not been previously selected.  After finding an index not previously selected, make sure to set the corresponding element in selected to true, and concatenate the corresponding element in people to the team string. What we are doing here is trying to select random names out of the people array, but not allowing the selection of the same name more than once. The selected array keeps track of what names have already been selected. Remember that team_size dictates how many names should be selected; you would have to involve this in a loop to select all the members of the team.
    int index;
    
    // identify ONE person that has not been previously selected
    do
    {
      index = rand() % size;
    } while (selected[index] == true);
    
    // at this point index points to an item 
    // that has not been previously selected
    // Now, add this person to the team 
    // and mark them as having been selected
  8. In main(), display the resulting team that comprises of a random list of people. Note that every time you run the program, your resulting team will be random and different from the sample below.

Sample Interaction / Output

Enter name 1: Jackie
Enter name 2: Tito
Enter name 3: Jermaine
Enter name 4: Marlon
Enter name 5: Michael

How big a team to create? 5

Team: Marlon Adam Caleb Tito Frank

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL00062

Print Requirements