Are these functions correct for the following program?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Are these functions correct for the following program?

  1. #1
    Join Date
    Dec 2012
    Posts
    1

    Are these functions correct for the following program?

    The local baseball team is computerizing its records. Write a program that computes batting averages and other statistics.

    There are 20 players on the team, identified by the player ID 1 through 20.

    You will have two input files: roster.txt and statistics.txt

    1) The Roster File (roster.txt) -
    This file contains 21 lines, first line is an integer indicating the season. The rest is in the format “player ID last name first name” (separated by a space) for each player, as follows:

    2012
    4 Smith Sam
    15 Jones Bob
    5 Miller Miles
    3 Carter John
    11 Apbee Cory
    9 Jackson James
    2 Zutter Tommy
    18 Buzz Bee
    8 Duncan Michael
    20 Tso Terry
    12 Lenn Lance
    1 Johnson Albert
    6 Knight Owl
    19 Neon Bret
    13 Ora Adam
    16 Elfin Victor
    7 Hoo Frank
    17 Greene Martin
    10 Crane David
    14 Rutter Jack

    (2) The Statistic File (statistics.txt) -
    This file contains the game-by game statistics for the players as a “player ID number, hits, walks, outs”

    15 3 2 1
    10 2 1 1
    9 5 2 1
    3 2 2 1
    19 1 2 3
    20 1 1 1
    5 3 1 3
    1 2 2 3
    11 0 2 2
    12 6 3 2
    8 3 0 0
    7 1 2 3
    19 2 2 1
    6 5 2 0
    5 1 1 1
    15 2 5 3
    11 1 1 3
    14 4 2 3
    9 0 1 2
    5 1 1 1
    15 3 2 1
    9 5 2 1
    3 2 2 1
    20 1 1 1
    5 3 1 3
    19 1 2 3
    12 6 3 2
    8 3 0 0
    7 1 2 3
    19 2 2 1

    Here is an example:

    3 2 1 1

    The example above indicates that during a particular game in the season, player number 3 made 2 hits, 1 walk, and 1 out. Notice, that means they were at bat 4 times.

    There might be several lines in the file for the same player. (A player can play more than one game in a season.) The file is organized by the batting line-ups (it is not sorted by player number).

    Each player’s batting average is computed by adding the player’s total number of hits, then dividing by the total number of times at bat.

    A walk does not count as either a hit or a time at bat when the batting average is being calculated.

    When your statistics file contains no data, print message to output file saying, “The 2012 baseball season was cancelled” and end the execution.

    Define a struct type PlayerInfo:
    struct PlayerInfo
    {
    int PlayerID;
    string LastName;
    string FirstName;
    int Hits, Walks, Outs;
    double Batting, OnBase;
    };

    Create an array of PlayerInfo structs to hold the players’ data. The array is local to main function and passes to other functions as argument.

    Code:
     PlayerInfo Player[20];
    Requirements: (your program must follow this logic) Functions must be called in this order inside main function.

    1. In the main function, you will call a function GetPlayer to read from Roster file and store the player names and id into the array and return the year for the season.

    Code:
    int GetPlayer ( PlayerInfo [ ] );
    (player in first line will store in index of 0, second line in index of 1, third line in index of 2, and etc.)

    2. Call a SortPlayerID function to sort them by Player ID. (e.g. use bubble sort or selection sort)

    3. Call a PrintRoster function to send the roster for the team with player ID in order to an output file

    4. Then call a GetStatistics function to read the statistics file, updating each player’s statistics of hits, walks and outs.
    Hints – you need to keep the player identification numbers with the player names and the data in the struct. When you read a player’s game line, look up the player in the array of structs before you adding up the hits, walks and outs (Player ID can identify the index of the array)

    5. A function to calculate each player’s batting average and on base average and store them.
    (remember to do casting in order to retain the factional part)

    batting average = (hits) / (hits + outs)
    on base average = (hits + walks) / (total at bats)

    6. Call a SortPlayerName function to sort the players by last name

    7. A function to Send the players’ statistics to the output file including their Batting Average, On Base Average, in DL list, the best hitter, the worst hitter, the best base runner and the worst base runner. (Note: A player is in the Disable List if he didn’t play at all in the entire season.)

    Sample of an output file:

    Baseball Season 2012
    --------------------------------
    Johnson Albert Player 1
    Zutter Tommy Player 2
    Miller Miles Player 3
    and etc……
    Tso Terry Player 20

    The alphabetical list of players with each player’s statistics (partial list)
    Player Number Hits Walks Outs Batting Average On Base Average In Disable List
    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Apbee Cory 11 xx xx xx 0.xx 0.xx
    Buzz Bee 18 xx xx xx 0.xx 0.xx
    Carter John 3 Yes
    Crane David 10 xx xx xx 0.xx 0.xx
    and etc……

    Give the last name(s) and batting average of the best hitter (i.e. highest batting average).
    Give the last name(s) and on-base average of the best base runner
    Give the last name(s) and batting average of the worst hitter
    Give the last name(s) and on-base average of the worst base runner

    If there is a tie for best or worst, Print all the players’ names.

    Example: (might not be true for your input file)
    The best hitter with batting average of 0.68: Jones
    The worst hitter with batting average of 0.04: Miller, Smith

    Here is my solution:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    #define infile "Roster.txt"
    #define outfile "Statistic.txt"
    
    int GetPlayer(PlayerInfo[]);
    
    struct PlayerInfo
    {
         int PlayerID;
         string LastName;
         string FirstName;
         int Hits, Walks, Outs;
         double Batting, OnBase;
    };
    
    int GetPlayer(PlayerInfo players[])
    {
    	players[20];
    	PlayerInfo playerid[20];
    	PlayerInfo firstname[20];
    	PlayerInfo lastname[20];
    
    	ifstream inputFile("Roster.txt");
    
    	if(!inputFile.is_open())
    	{
    		cout << "ERROR: Failed to open \"roster.txt\" for input!" << endl;
    		return -1;
    	}
    	for(int i; i < players; i++)
    	{
    		inputFile >> playerid >> firstname >> lastname;
    		players += inputFile;
    	}
    	return 0;
    }
    
    void SortPlayerID(PlayerInfo players[])
    {
    	players[20];
    	PlayerInfo PlayerIDs[20];
    	int index = 0;
    	for(int i = index+1; i > 0; i++)
    	{
    		if(PlayerIDs[i] < PlayerIDs[index])
    		{
    			PlayerIDs[i] = PlayerIDs[index];
    			PlayerIDs[index] = players[i];
    			players[i] = PlayerIDs[i];
    		}
    		index++;
    	}
    }
    
    void PrintRoster(PlayerInfo players[])
    {
    	players[20];
    	PlayerInfo PlayerIDs[20];
    	cout << "		Baseball Season 2012	";
    	cout << "--------------------------------";
    	cout << PlayerIDs << endl;
    }
    
    int GetStatistics(PlayerInfo players[])
    {
    	players[20];
    	PlayerInfo hits, walks, outs;
    	PlayerInfo playerid[20];
    	PlayerInfo firstname[20];
    	PlayerInfo lastname[20];
    	int total;
    	total = hits + walks + outs;
    	return 20;
    }
    
    int Calculate(PlayerInfo players[])
    {
    	players[20];
    	PlayerInfo batting_avg, onbase_avg;
    	PlayerInfo hits, walks, outs;
    	int	total = hits + walks + outs;
    	batting_avg = int((hits)/(hits + outs));
    	onbase_avg = int((hits + walks)/(total));
    	return 0;
    }
    
    void SortPlayerName(PlayerInfo players[])
    {
    	players[20];
    	PlayerInfo lastnames[20];
    	int index = 0;
    	for(int i = index+1; i > 0; i++)
    	{
    		if(lastnames[i] < lastnames[index])
    		{
    			lastnames[i] = lastnames[index];
    			lastnames[index] = players[i];
    			players[i] = lastnames[i];
    		}
    		index++;
    	}
    }
    
    void DisplayStatistics(PlayerInfo players[])
    {
    	 char disabled_list;
    	 double highest_batting;
    	 double highest_onbase;
    	 double lowest_batting;
    	 double lowest_onbase;
    	 players[20];
    	 PlayerInfo PlayerID[20];
    	 PlayerInfo LastName[20];
    	 PlayerInfo FirstName[20];
         PlayerInfo Hits, Walks, Outs;
         PlayerInfo Batting, OnBase;
    	 cout << "Player	" << "Number	" << "Hits	" << "Walks		" << "Outs		" << "Batting Average		" << "On Base Average		" << "In Disable List" << endl;
    	 cout << LastName << FirstName << "\t" << PlayerID << "\t" << Hits << "\t" << Walks << "\t" << Outs << "\t" << Batting << "\t" << OnBase << disabled_list << endl;
    	 for(int i; i > 0; i++)
    	 {
    		 if(highest_batting < Batting)
    		 {
    			 cout << "The best hitter with batting average of " << Batting << ": " << LastName << endl;
    		 }
    		 else if(highest_onbase < OnBase)
    		 {
    			 cout << "The best base runner with on-base average of " << OnBase << ": " << LastName << endl;
    		 }
    		 else if(lowest_batting > OnBase)
    		 {
    			 cout << "The worst hitter with batting average of " << Batting << ": " << LastName << endl;
    		 }
    		 else if(lowest_onbase < OnBase)
    		 {
    			 cout << "The worst base runner with on-base average of " << OnBase << ": " << LastName << endl;
    		 }
    		 else if(LastName == disabled_list)
    		 {
    			 cout << "Yes" << endl;
    		 }
    	 }
    }
    
    int main()
    {
    	ifstream team;
    	ofstream stats;
    	char disabled_list;
    	PlayerInfo Player[20];
    	PlayerInfo PlayerID[20];
    	PlayerInfo LastName[20];
    	PlayerInfo FirstName[20];
    	PlayerInfo Hits, Walks, Outs;
    	PlayerInfo Batting, OnBase;
    
    	team.open(infile);
    	if(team.fail())
    	{
    		cout << "The 2012 baseball season was cancelled" << endl;
    		return -1;
    	}
    
    	stats.open(outfile);
    	if(stats.fail())
    	{
    		cout << "The 2012 baseball season was cancelled" << endl;
    		team.close();
    		return -1;
    	}
    
    	while(!team.eof())
    	{
    		GetPlayer(Player);
    		SortPlayerID(Player);
    		PrintRoster(Player);
    		GetStatistics(Player);
    		Calculate(Player);
    		SortPlayerName(Player);
    		DisplayStatistics(Player);
    	}
    
    	team.close();
    	stats.close();
    
    	return 0;
    }
    I'm not sure if I am doing the functions correctly. Is there anything I need to change? Also, my code is giving these compiler errors: No operator matches these operands. It it happening with these: <<, <, >, ==, +, +=. Is there anything I can do to fix them?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Are these functions correct for the following program?

    Quote Originally Posted by andy2012 View Post
    Write a program that computes batting averages and other statistics.
    It is not necessary to post your entire project outline.
    I'm not sure if I am doing the functions correctly. Is there anything I need to change?
    If you wrote the code, then you better know exactly what you're doing at each step. That's the responsibility of the person writing a program -- every single step must be understood, from the declaration of variables, to the calling of functions, usage of classes, etc.

    If you're not sure, then you should write smaller, sample programs to familiarize yourself with what you don't know. That is how a programmer solves their own issues with not understanding something fully.
    Also, my code is giving these compiler errors: No operator matches these operands. It it happening with these: <<, <, >, ==, +, +=. Is there anything I can do to fix them?
    Please post the actual compiler errors as well as the line of code that doesn't compile.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center