none
Guidance for Homework RRS feed

  • Question

  • First, in no way am I asking anyone to complete or do my homework. If that were the case, I would pay someone online via paypal to do just that.

    I am just seeking guidance / help to understand what I'm not "getting".

    This is a intro C++ class. The topic this week is arrays and passing arrays / functions.

    Here is the pseudocode:

    Write a program to do the following:

    In main, declare a PlayerName Arrayand a Score Array. Declare the size of the arrays to be 100.
    In the InputData function, input the player name and score into the arrays for an unknown number of players up to 100. 

    In the DisplayPlayerData function, display the name and score of each player. 

    In the CalculateAverageScore function, calculate the average score and return it by value. 

    In the DisplayBelowAverage function, display the name and score for any player who scored below the average. Do not use global variables.

    Using the pseudocode below, write the code that will meet the requirements.

    Main Function

       Declare the player name and score arrays, number of players, and average score.

       Call the InputData function

       Call the DisplayPlayerData function

       Call the CalculateAverageScore function and assign the returned value in average score

       Call the DisplayBelowAverage function

    InputData function

        While the number of players is less than the length of the array

            Prompt for the player's name

            If the user entered Q, break out of the loop

            Prompt the user for the player's score

            Add 1 to the number of players

        End-While

    DisplayPlayerData function

        Display the name and score of each player

    CalculateAverageScore function

        Add up the scores and divide by the number of scores to calculate the average score

        Display the average score

        Return the average score to main

    DisplayBelowAverage function

        Display the names and scores of all players who scored below the average score      

    This is the coding I have so far. I'm not quite sure I'm understanding the whole return by value and passing functions / arrays. In fact, I'm pretty sure my whole coding so far has many hole / errors in it, so you can see my nature in requesting assistance.

    Any guidance would be greatly appreciated

    #include<iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    // Prototypes
    void InputData(string PlayerName[], int Score[], int numPlayers);
    void PlayerData(string PlayerName[], int Score[], int numPlayers);
    double averageScore(int Score[], int numPlayer);
    void BelowAverage(string PlayerName[], int Score[], int numPlayers, double AverageScore);
    
    
    int main () //Begin Main
    {
    	string PlayerName[100] = {""};
    	int Score[100] = {};
    	int numPlayers;
    	InputData(string PlayerName[], int Score[], int numPlayers);
    	PlayerData(string PlayerName[], int Score[], int numPlayers);
    	double averageScore(int Score[], int numPlayer);
    	BelowAverage();
    } // End Main
    
    
    void InputData(string PlayerName[], int Score[], int numPlayers) // InputData Function
    { //Start InputData Function
    	for (int i = 0; i < 100; i++)
    	{ // Start Loop
    		cout << "Enter Player Name (Q to Quit): " << endl;
    		getline(cin, PlayerName[100]);
    		if (PlayerName[100] == "Q" || PlayerName[100] == "q")
    			break;
    		else
    			cout << "Enter Score for" << PlayerName << endl;
    		cin >> Score[100];
    		numPlayers++;
    	} // End Loop
    } // End InputData Function
    
    
    void PlayerData(string PlayerName[], int Score[], int numPlayers) // PlayerData Function
    { // Start PlayerData Function
    	for (int i = 0; i < 100; i++)
    	{ // Start Loop
    		cout << PlayerName[i] << "";
    		cout << Score[i] << "";
    	} // End Loop
    } // End PlayerData Function
    
    
    double averageScore(int Score[], int numPlayer) // Calculate Average Score Function
    { // Start Calc Avg Score Functions
    	double averageScore = 0;
    	for (int i = 0; i < 100; i++)
    	{ // Start Loop
    		averageScore += Score[i];
    		averageScore /= Score[i];
    		return averageScore; // No clue if this is right?
    	} // End Loop
    } // End Calc Avg Score Function
    
    
    void BelowAverage(string PlayerName[], int Score[], int numPlayers, double averageScore) // Below Average Function
    { // Start Below Average Function
    	for (int i = 0; i < 100; i++)
    	{ // Start Loop
    		if (Score[100] < averageScore)
    		{ // If
    			cout << PlayerName[i] << "";
    			cout << Score[i] << "";
    		} // End If
    	} // End Loop
    } // End Below Average Function


    • Edited by donwon86 Saturday, March 31, 2012 8:53 PM
    Saturday, March 31, 2012 8:50 PM

Answers

  • Hints for your consideration

         Instead of hardcoding the size of the array in many different places, use either a #define or an enum.  If you ever have to change the size, it will require only a single change to your program.

         Input data should return the number of players you enter so that later you don't process all the elements of the array even though many are "empty".  You need two integer values related to the arrays.  One, passed in as a parameter, contains the max size of the arrays.  The second, computed by the function, contains the number of players that have been processed by the function (how many of the array elements are actually "in use".

         In main, you should not be repeating the prototypes for the functions but should actually be calling them.  When you call a function, you normally do not prive a type for the function or the arguments.  One possible source of your confusion is that the prototypes inlcude the names for the parameters.  While acceptable syntactically, the names are ignored in the protypes.  You should review your text for an example of how a function is declared with a prototype, then called (probably from main), and then defined in the source after main.

         Your function DisplayData does not have the name specified in the instructions.  Neither do your functions averageScore and BlowAverage

         When you call averageScore, you need to assign the returned value to a variable as specified in the instructions.  In averageScore, you should not be dividing until you have finished summing the scores.  Think about how you do it manually.  What is the average of 1, 5, and 6?

        In BelowAverage, look very carefully at the if statement.  It currently invokes undefined behavior.

         Unless your instructure requires them, your comments at each { and } are useless.  They provide no information that is not immediately obvious to anyone looking at the code.

    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Marked as answer by Helen Zhao Monday, April 9, 2012 2:11 AM
    Saturday, March 31, 2012 9:47 PM
  • In the averageScore function, the return should come after the for loop. The way you have it right now it will return after the first iteration of the loop and that is not really a good average in this case.

    double averageScore(int Score[], int numPlayer) // Calculate Average Score Function
    { // Start Calc Avg Score Functions
    	double averageScore = 0;
    	for (int i = 0; i < 100; i++)
    	{ // Start Loop
    		averageScore += Score[i];
    		averageScore /= Score[i];
    	} // End Loop
    	return averageScore; // Should be a bit better
    } // End Calc Avg Score Function
    


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Marked as answer by Helen Zhao Monday, April 9, 2012 2:11 AM
    Saturday, March 31, 2012 9:46 PM
  • For starters, you need to review the differences between:

    function prototypes
    function definitions
    function calls

    Review:

    Functions
    http://www.cprogramming.com/tutorial/lesson4.html

    Functions (I)
    http://www.cplusplus.com/doc/tutorial/functions/

    Functions (II)
    http://www.cplusplus.com/doc/tutorial/functions2/

    The code you posted doesn't have the needed function
    prototypes. Also, in main your calls to the functions
    have their arguments specified as they would be in
    prototypes and definitions. In function *calls* you
    don't specify the argument or return types.

    - Wayne
    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Marked as answer by Helen Zhao Monday, April 9, 2012 2:11 AM
    Saturday, March 31, 2012 10:00 PM

All replies

  • In the averageScore function, the return should come after the for loop. The way you have it right now it will return after the first iteration of the loop and that is not really a good average in this case.

    double averageScore(int Score[], int numPlayer) // Calculate Average Score Function
    { // Start Calc Avg Score Functions
    	double averageScore = 0;
    	for (int i = 0; i < 100; i++)
    	{ // Start Loop
    		averageScore += Score[i];
    		averageScore /= Score[i];
    	} // End Loop
    	return averageScore; // Should be a bit better
    } // End Calc Avg Score Function
    


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Marked as answer by Helen Zhao Monday, April 9, 2012 2:11 AM
    Saturday, March 31, 2012 9:46 PM
  • Hints for your consideration

         Instead of hardcoding the size of the array in many different places, use either a #define or an enum.  If you ever have to change the size, it will require only a single change to your program.

         Input data should return the number of players you enter so that later you don't process all the elements of the array even though many are "empty".  You need two integer values related to the arrays.  One, passed in as a parameter, contains the max size of the arrays.  The second, computed by the function, contains the number of players that have been processed by the function (how many of the array elements are actually "in use".

         In main, you should not be repeating the prototypes for the functions but should actually be calling them.  When you call a function, you normally do not prive a type for the function or the arguments.  One possible source of your confusion is that the prototypes inlcude the names for the parameters.  While acceptable syntactically, the names are ignored in the protypes.  You should review your text for an example of how a function is declared with a prototype, then called (probably from main), and then defined in the source after main.

         Your function DisplayData does not have the name specified in the instructions.  Neither do your functions averageScore and BlowAverage

         When you call averageScore, you need to assign the returned value to a variable as specified in the instructions.  In averageScore, you should not be dividing until you have finished summing the scores.  Think about how you do it manually.  What is the average of 1, 5, and 6?

        In BelowAverage, look very carefully at the if statement.  It currently invokes undefined behavior.

         Unless your instructure requires them, your comments at each { and } are useless.  They provide no information that is not immediately obvious to anyone looking at the code.

    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Marked as answer by Helen Zhao Monday, April 9, 2012 2:11 AM
    Saturday, March 31, 2012 9:47 PM
  • For starters, you need to review the differences between:

    function prototypes
    function definitions
    function calls

    Review:

    Functions
    http://www.cprogramming.com/tutorial/lesson4.html

    Functions (I)
    http://www.cplusplus.com/doc/tutorial/functions/

    Functions (II)
    http://www.cplusplus.com/doc/tutorial/functions2/

    The code you posted doesn't have the needed function
    prototypes. Also, in main your calls to the functions
    have their arguments specified as they would be in
    prototypes and definitions. In function *calls* you
    don't specify the argument or return types.

    - Wayne
    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Marked as answer by Helen Zhao Monday, April 9, 2012 2:11 AM
    Saturday, March 31, 2012 10:00 PM
  • Thanks everyone for your help .... I'm at whits end right now. 7hrs of playing with code has got me this far, and still not right. Please review and any advice on what I'm doing wrong is welcomed.

    #include<iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    // Prototypes
    
    void InputData(string PlayerName[], int Score[], int &numPlayers);
    void DispPlayerData(string PlayerName[], int Score[], int numPlayers);
    double calcAverageScore(int Score[], int numPlayers);
    void DispBelowAverage(string PlayerName[], int Score[], int numPlayers, double averageScore);
    
    
    int main () //Begin Main
    {
    	string PlayerName[100];
    	int Score[100];
    	int numPlayers = 0;
    	double averageScore = 0;
    
    	cout << fixed << showpoint << setprecision(2);
    	InputData(PlayerName, Score, numPlayers);
    	DispPlayerData(PlayerName, Score, numPlayers);
    	calcAverageScore(Score, numPlayers);
    	DispBelowAverage(PlayerName, Score, numPlayers, averageScore);
    	return 0;
    } // End Main
    
    
    void InputData(string PlayerName[], int Score[], int &numPlayers) // InputData Function
    { //Start InputData Function
    	for (int i = 0; i < numPlayers; i++)
    	{ // Start Loop
    		cout << "Enter Player Name (Q to Quit): ";
    		getline(cin, PlayerName[i]);
    		if (PlayerName[i] == "Q" || PlayerName[i] == "q")
    			break;
    		cout << "Enter Score for " << PlayerName[i] << ": ";
    		cin >> Score[i];
    		numPlayers++;
    	} // End Loop
    } // End InputData Function
    
    
    void DispPlayerData(string PlayerName[], int Score[], int numPlayers) // PlayerData Function
    { // Start PlayerData Function
    	cout << setw(10) << left << "\n Name" << setw(5) << right << "Score" << endl;
    	for (int i = 0; i < numPlayers; i++)
    	{ // Start Loop
    		cout << setw(10) << left << PlayerName[i];
    		cout << setw(5) << right << Score[i];
    	} // End Loop
    } // End PlayerData Function
    
    
    double calcAverageScore(int Score[], int numPlayers) // Calculate Average Score Function
    { // Start Calc Avg Score Functions
    	double averageScore = 0;
    	double totalScore = 0;
    	for (int i = 0; i < numPlayers; i++)
    	{ // Start Loop
    		totalScore += Score[i];
    		averageScore = totalScore/i;
    	} // End Loop
    			cout << fixed << showpoint << setprecision(2);
    			cout << "\n\nAverage Score: " << averageScore << endl;
    			return averageScore;
    } // End Calc Avg Score Function
    
    
    void DispBelowAverage(string PlayerName[], int Score[], int numPlayers, double averageScore) // Below Average Function
    { // Start Below Average Function
    	cout << "\n\nPlayers Who Scored Below Average\n";
    	cout << setw(10) << left << "\n Name" << setw(5) << right << "Score" << endl;
    	for (int i = 0; i < numPlayers; i++)
    	{ // Start Loop
    		if (Score[i] < averageScore)
    		{ // If
    			cout << setw (10) << left << PlayerName[i];
    			cout << setw (5) << right << Score[i];
    		} // End If
    	} // End Loop
    } // End Below Average Function

    Sunday, April 1, 2012 2:06 PM
  • numPlayers is 0 so the loop in InputData does nothing.
    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Unproposed as answer by donwon86 Wednesday, April 4, 2012 11:10 AM
    Sunday, April 1, 2012 2:22 PM
  • Thanks, fixing now. Have become overwhelmed at this point ....
    Sunday, April 1, 2012 2:30 PM
  • Walk through the program step by step. You can do this mentally and/or you can do it with the breakpoint and single-step features of the debugger. Most or all of the errors you have made will reveal themselves rather quickly if you do this.
    • Proposed as answer by Helen Zhao Wednesday, April 4, 2012 8:29 AM
    • Unproposed as answer by donwon86 Wednesday, April 4, 2012 11:10 AM
    Sunday, April 1, 2012 5:24 PM