I am trying to write a program that calculates the number of soccer teams and players that will be available. I am very confused as to which parameters need to be pass by reference. I've looked at the definition and I don't understand what it means. My code is probably wrong in the functions, but hopefully I can figure those out after I get the pass-by-references right.
Code:TODO: Rewrite this program using the following 4 functions. (1) An input function for getting the number of players per team. (2) Another input function for getting the number of players available. (3) A function to calculate the number of teams and number of left-over players. (4) An output function to print the result. NOTE: Use and document the appropriate parameter passing mechanism for each parameter: in, out, inout In the end, the main() function should include just the variable declarations, 4 function call statements, and the return statement. */ #include <iostream> using namespace std; int main() { int players, // Number of available players teamPlayers, // Number of desired players per team numTeams, // Number of teams leftOver; // Number of players left over void getPlayers(int&); void playersPerTeam(int&); void numOfTeams(int&, int&, int&); void displayResults(int, int, int, int); getPlayers(players); playersPerTeam(teamPlayers); numOfTeams(numTeams, teamPlayers, players); displayResults(numTeams, leftOver); return 0; } void getPlayers(int& players) { cout << "How many players are available? "; cin >> players; // Validate the input using a while loop. while (players <= 0) { cout << "Please enter a positive number: "; cin >> players; } } void playersPerTeam(int& teamPlayers) { // Validate the input using a while loop. while (teamPlayers < 9 || teamPlayers > 15) { cout << "You should have at least 9 but no\n"; cout << "more than 15 per team.\n"; cout << "How many players do you wish per team? "; cin >> teamPlayers; } cout << endl; } void numOfTeams(int& numTeams, int& teamPlayers, int& players) { numTeams = players / teamPlayers; } void displayResults(int players, int leftOver, int teamPlayers, int numTeams) { leftOver = players % teamPlayers; cout << "There will be " << numTeams << " teams with "; cout << leftOver << " players left over.\n"; }



LinkBack URL
About LinkBacks



