I need help with constructing the code for my recent C++ assignment. I\'m super
ID: 3578525 • Letter: I
Question
I need help with constructing the code for my recent C++ assignment. I'm super confused on the assignment and help would be greatly appreciated! An Application of a Stack For this assignment you are to write a C++ program that reads a data file consisting of a player’s name followed by their batting average. The program will then print the highest batting average and the names of the people who have the highest batting average. It will also print the lowest batting average and the names of the people who have the lowest average. We will also assume that the file will not contain more than 100 players. Input: The program reads an input file consisting of each player’s name followed by their average. Here is a small sample: John .425 Terry .696 Glenn .704 Gary .400 Joe .704 A text file containing 25 names and averages can be downloaded from this link Output: The program outputs the highest and lowest batting averages and the names associated with these averages. For example, in the above sample data Glenn and Joe have the highest batting average and Gary has the lowest. In order to keep track of all the people with the highest and lowest you will need to use a stack. Actually you will need to use two instances of the stack. One to hold the names of the highest and one to hold the names of the lowest averages. You should have created a stack in an earlier assignment. Here is how the program might flow When you read the first name and average at that time it is both the highest and lowest. You will store the average in variables that keep track of the highest and lowest average. You will then push the name on to both of the stacks. After reading the second name and average you are faced with some choices. 1. The new average is greater than the highest stored a. Update the value of the highest average so far b. Initialize the stack that holds the highest – that is remove the names of the players from the stack c. Save the name of the player having the highest average so far on the Stack 2. The new average is equal to the highest average so far. In this case simply add the name of the player to the top of the stack 3. The new average is smaller than the highest average so far. In this case you check to see if it is less than the lowest average found so far. If this is the case you need to do the following a. Update the value of the lowest average so far b. Initialize the stack that holds the lowest – that is remove the names of the players from the stack c. Save the names of the players having the lowest average so far on the stack 4. The average is equal to the lowest average so far. In this case add the name of the player to the top of the stack. 5. The average is not smaller than the smallest average so far and it is not larger than the largest so far. In this case simply discard the name and the average. When the file has been completely read you should have a stack that contains the names of the players with the highest average and a stack with the names of the lowest average. You should also have a variable that contains the highest average and a variable that contains the lowest average. Print to the screen the highest average and the players with this average. Do the same with the lowest. Please Note: I do NOT want to see all of your code written in main. This is the end of an advanced programming course and you should write functions when appropriate. I want to see at least one class. For example, I might have a class named Payers with a function AddPlayer(string name, double batAvg);, a function like printLow(), printHigh() etc. You don't have to follow this example, just write good code that FULLY meets the requirements as stated. Of course, your stack class from Assignment 14 would be very useful as well.I need help with constructing the code for my recent C++ assignment. I'm super confused on the assignment and help would be greatly appreciated! An Application of a Stack For this assignment you are to write a C++ program that reads a data file consisting of a player’s name followed by their batting average. The program will then print the highest batting average and the names of the people who have the highest batting average. It will also print the lowest batting average and the names of the people who have the lowest average. We will also assume that the file will not contain more than 100 players. Input: The program reads an input file consisting of each player’s name followed by their average. Here is a small sample: John .425 Terry .696 Glenn .704 Gary .400 Joe .704 A text file containing 25 names and averages can be downloaded from this link Output: The program outputs the highest and lowest batting averages and the names associated with these averages. For example, in the above sample data Glenn and Joe have the highest batting average and Gary has the lowest. In order to keep track of all the people with the highest and lowest you will need to use a stack. Actually you will need to use two instances of the stack. One to hold the names of the highest and one to hold the names of the lowest averages. You should have created a stack in an earlier assignment. Here is how the program might flow When you read the first name and average at that time it is both the highest and lowest. You will store the average in variables that keep track of the highest and lowest average. You will then push the name on to both of the stacks. After reading the second name and average you are faced with some choices. 1. The new average is greater than the highest stored a. Update the value of the highest average so far b. Initialize the stack that holds the highest – that is remove the names of the players from the stack c. Save the name of the player having the highest average so far on the Stack 2. The new average is equal to the highest average so far. In this case simply add the name of the player to the top of the stack 3. The new average is smaller than the highest average so far. In this case you check to see if it is less than the lowest average found so far. If this is the case you need to do the following a. Update the value of the lowest average so far b. Initialize the stack that holds the lowest – that is remove the names of the players from the stack c. Save the names of the players having the lowest average so far on the stack 4. The average is equal to the lowest average so far. In this case add the name of the player to the top of the stack. 5. The average is not smaller than the smallest average so far and it is not larger than the largest so far. In this case simply discard the name and the average. When the file has been completely read you should have a stack that contains the names of the players with the highest average and a stack with the names of the lowest average. You should also have a variable that contains the highest average and a variable that contains the lowest average. Print to the screen the highest average and the players with this average. Do the same with the lowest. Please Note: I do NOT want to see all of your code written in main. This is the end of an advanced programming course and you should write functions when appropriate. I want to see at least one class. For example, I might have a class named Payers with a function AddPlayer(string name, double batAvg);, a function like printLow(), printHigh() etc. You don't have to follow this example, just write good code that FULLY meets the requirements as stated. Of course, your stack class from Assignment 14 would be very useful as well.
I need help with constructing the code for my recent C++ assignment. I'm super confused on the assignment and help would be greatly appreciated! An Application of a Stack For this assignment you are to write a C++ program that reads a data file consisting of a player’s name followed by their batting average. The program will then print the highest batting average and the names of the people who have the highest batting average. It will also print the lowest batting average and the names of the people who have the lowest average. We will also assume that the file will not contain more than 100 players. Input: The program reads an input file consisting of each player’s name followed by their average. Here is a small sample: John .425 Terry .696 Glenn .704 Gary .400 Joe .704 A text file containing 25 names and averages can be downloaded from this link Output: The program outputs the highest and lowest batting averages and the names associated with these averages. For example, in the above sample data Glenn and Joe have the highest batting average and Gary has the lowest. In order to keep track of all the people with the highest and lowest you will need to use a stack. Actually you will need to use two instances of the stack. One to hold the names of the highest and one to hold the names of the lowest averages. You should have created a stack in an earlier assignment. Here is how the program might flow When you read the first name and average at that time it is both the highest and lowest. You will store the average in variables that keep track of the highest and lowest average. You will then push the name on to both of the stacks. After reading the second name and average you are faced with some choices. 1. The new average is greater than the highest stored a. Update the value of the highest average so far b. Initialize the stack that holds the highest – that is remove the names of the players from the stack c. Save the name of the player having the highest average so far on the Stack 2. The new average is equal to the highest average so far. In this case simply add the name of the player to the top of the stack 3. The new average is smaller than the highest average so far. In this case you check to see if it is less than the lowest average found so far. If this is the case you need to do the following a. Update the value of the lowest average so far b. Initialize the stack that holds the lowest – that is remove the names of the players from the stack c. Save the names of the players having the lowest average so far on the stack 4. The average is equal to the lowest average so far. In this case add the name of the player to the top of the stack. 5. The average is not smaller than the smallest average so far and it is not larger than the largest so far. In this case simply discard the name and the average. When the file has been completely read you should have a stack that contains the names of the players with the highest average and a stack with the names of the lowest average. You should also have a variable that contains the highest average and a variable that contains the lowest average. Print to the screen the highest average and the players with this average. Do the same with the lowest. Please Note: I do NOT want to see all of your code written in main. This is the end of an advanced programming course and you should write functions when appropriate. I want to see at least one class. For example, I might have a class named Payers with a function AddPlayer(string name, double batAvg);, a function like printLow(), printHigh() etc. You don't have to follow this example, just write good code that FULLY meets the requirements as stated. Of course, your stack class from Assignment 14 would be very useful as well.
Explanation / Answer
//header files declaration
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//input and output file
#define infile "Roster.txt"
#define outfile "Statistic.txt"
//user defined function for player information
int GetPlayer(PlayerInformation[]);
struct PlayerInformation
{
int PlayerID;
string LastName;
string FirstName;
int Hits, Walks, Outs;
double Batting, OnBase;
};
int GetPlayer(PlayerInformation[])
{
PlayerInformation playerid = {20};
PlayerInformation firstname = {20};
PlayerInformation lastname = {20};
cin >> playerid >> firstname >> lastname;
return 0;
}
//user defined function for playerID
void SortPlayerID()
{
int PlayerIDs[20];
int index = 0;
for(int i = index+1; i > 0; i++)
{
if(PlayerIDs[i] < PlayerIDs[index])
{
PlayerIDs[i] = PlayerIDs[index];
PlayerIDs[index] = PlayerIDs[i];
}
index++;
}
}
//print the input fie Roster.txt
void PrintRoster()
{
PlayerInformation PlayerIDs = {20};
cout << " Baseball Season 2012 ";
cout << "--------------------------------";
cout << PlayerIDs << endl;
}
//user defined function to get statistics about the player
int GetStatistics()
{
int hits, walks, outs, total;
PlayerInformation playerid = {20};
PlayerInformation firstname = {20};
PlayerInformation lastname = {20};
total = hits + walks + outs;
return 20;
}
//user defined function to calculate the averages
int Calculate()
{
double batting_avg, onbase_avg;
int hits;
int walks;
int outs;
int total = hits + walks + outs;
batting_avg = int((hits)/(hits + outs));
+ walks)/(total));
return 0;
}
//user defined function to sort the names based on the average
void SortPlayerName()
{
int lastnames[20];
int index = 0;
for(int i = index+1; i > 0; i++)
{
if(lastnames[i] < lastnames[index])
{
lastnames[i] = lastnames[index];
lastnames[index] = lastnames[i];
}
index++;
}
}
//user defined function to print the averages of the players
void DisplayStatistics(char disabled_list)
{
double highest_batting;
double highest_onbase;
double lowest_batting;
double lowest_onbase;
PlayerInformation PlayerID = {20};
PlayerInformation LastName = {20};
PlayerInformation FirstName = {20};
PlayerInformation Hits, Walks, Outs;
PlayerInformation Batting, OnBase;
cout << "Player " << "Number " << "Hits " << "Walks " << "Outs " << "Batting Average " << "On Base Average " << "In Disable List" << endl;
cout << LastName << FirstName << " " << PlayerID << " " << Hits << " " << Walks << " " << Outs << " " << Batting << " " << 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;
}
}
}
//main function declaration
int main()
{
ifstream team;
ofstream stats;
char disabled_list;
PlayerInformation Player[20];
PlayerInformation PlayerID;
PlayerInformation LastName;
PlayerInformation FirstName;
PlayerInformation Hits, Walks, Outs;
PlayerInformation 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();
PrintRoster();
GetStatistics();
Calculate();
SortPlayerName();
DisplayStatistics(disabled_list);
}
team.close();
stats.close();
return 0;
}
////*** Thank You ***////
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.