Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you use this array instead of asking for input?
    primarykey
    data
    text
    <p>Learning about arrays in class. Trying to understand them by playing with the code below using my arrays instead of this sample below.</p> <p>example of array: </p> <pre><code>string[16] = {"Toelle","Red Lightning","Penguins","Tigers","You Know It","VP4LIFE","OG WOW","Indy","Ok","NOSER","LAK State","THE State","NY","Ks","Tahaa","Fosda"}; </code></pre> <p>Code:</p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; // This program inputs a list of team names and runs a random tournament to find the winner. void print_teams(string *ptr, int size) { for(int team = 0; team &lt; size; team++) { cout &lt;&lt; "Team "&lt;&lt;team&lt;&lt;"="&lt;&lt;ptr[team]&lt;&lt;endl; } } int main() { const int NTEAMS=4; string *teams = new string[NTEAMS]; // Get input from the user for the teams for(int team = 0; team &lt; NTEAMS; team++) { cout &lt;&lt;"Enter the name for team #"&lt;&lt;team&lt;&lt;":"; getline(cin, teams[team]); } print_teams(teams, NTEAMS); int teams_left = NTEAMS; string *team_ptr = teams; for(int round = 0; teams_left &gt; 1; round++) { // Allocate space for the winners int new_size = teams_left/2; string *new_teams = new string[new_size]; // Run a round of the tournament for(int team = 0; team &lt; teams_left; team+=2) { // Pick a winner int winner = rand()%2; //update the winners array new_teams[team/2] = team_ptr[team+winner]; } cout &lt;&lt; "Round "&lt;&lt;round&lt;&lt;endl; print_teams(new_teams, new_size); // Update the size and team pointer for the next round teams_left = new_size; // Free up the space for the old teams list delete[] team_ptr; team_ptr = new_teams; } system("pause"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload