I need help with constructing the code for my recent C++ assignment. I\'m super
ID: 3578444 • Letter: I
Question
I need help with constructing the code for my recent C++ assignment. I'm super confused on the assignment and any help will be appreciated!
Edit: Here are the instructions in text format
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.
?course ide 63967 18content id .2872706 1&modet; reset of a slack The program reads an input file consisting of each player's name followodby their average. Here is a small sample Terry 696 A text file containing 25 names and averages can be downloaded form thi Ink The program outputs the highest and owest batting averagrs and ew names associated with these averages Forexample, in the above sample data Glernand Joe have the righest batting average and Gay of stack one held the names of the highest and one to hold the In ander to keep tack of anthe people with the highest and owkst you need to use a stack Actually you wilneed to use two instances names of the lowest averages. You should have croatod a stack in an eater assignment Here is how the program might now and average at that time i boen the highest andlowest You slo ethe average hvalables that keep tack of highest and lowest average You" "en Push the name When you read the first name bath After reading the second name and average you are faced with some choices 1.The new average is greater than the highest stored Update the value the highest average so far remove the names of the players from the stack b Initialize the stack that holds the highest that is 2 The new average is c Save the name the player having the highest average far 5sack stack found so far this is the case you need to do folowing equal to the highest average so far in this case simply the name of the player the top of the The new is smaller than the highest average is than the average Update value the average so far b the holds is remove the ofte players from the stack c Save the names of the play having the lowest average so far on the stack 4 The average is equal the lowest average so this case add the name name and the average is not smaller than the smallest average so tar and it is not larger tan the largest so far In this case simply discard the 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 Nor want to see alofyor code wraten in main This is advanced programmng course and you should write functions when appropria want to see at least one class for example, might name double a orintLow0 You to this good code that meets nequirements as stated o course your stack class from Assignment would be very useful as welExplanation / Answer
Please find below your C++ program with comments for better understanding:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#define infile "Roster.txt" //input file from which the inputs will be read
#define outfile "Output.txt" //output will be written to this file
int GetPlayer(PlayerInfo[]); //GetPlayer() function
struct PlayerInfo //PlayerInfo
{
int PlayerID;
string LastName;
string FirstName;
int Hits, Walks, Outs;
double Batting, OnBase;
};
int GetPlayer(PlayerInfo[]) //GetPlayer() function
{
PlayerInfo playerid = {20};
PlayerInfo firstname = {20};
PlayerInfo lastname = {20};
cin >> playerid >> firstname >> lastname;
return 0;
}
void SortPlayerID() //SortPlayerID() function declared here
{
int PlayerIDs[20]; //PlayerIDs with limit of 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++;
}
}
void PrintRoster() //PrintRoster() function
{
PlayerInfo PlayerIDs = {20};
cout << " Baseball Season 2016 ";
cout << "--------------------------------";
cout << PlayerIDs << endl;
}
int GetStatistics() //GetStatistics() function declared here
{
int hits, walks, outs, total;
PlayerInfo playerid = {20}; //PlayerIDs with limit of 20
PlayerInfo firstname = {20}; //firstname with limit of 20
PlayerInfo lastname = {20};
total = hits + walks + outs;
return 20;
}
int Calculate() //Calculate() function declared here
{
double batting_avg, onbase_avg;
int hits;
int walks;
int outs;
int total = hits + walks + outs;
batting_avg = int((hits)/(hits + outs)); //batting_avg calculation
+ walks)/(total)); //onbase_avg calculation
return 0;
}
void SortPlayerName() //SortPlayerName() function declared here
{
int lastnames[20];
int index = 0;
for(int i = index+1; i > 0; i++)
{
if(lastnames[i] < lastnames[index]) //if statement
{
lastnames[i] = lastnames[index];
lastnames[index] = lastnames[i];
}
index++;
}
}
void DisplayStatistics(char disabled_list) //DisplayStatistics() function declared
{
double highest_batting;
double highest_onbase;
double lowest_batting;
double lowest_onbase;
PlayerInfo PlayerID = {20}; //PlayerIDs with limit of 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 << " " << PlayerID << " " << Hits << " " << Walks << " " << Outs << " " << Batting << " " << OnBase << disabled_list << endl;
for(int i; i > 0; i++)
{
if(highest_batting < Batting) //If statement
{
cout << "The best hitter with batting average of " << Batting << ": " << LastName << endl;
}
else if(highest_onbase < OnBase) //Else-if condition
{
cout << "The best base runner with on-base average of " << OnBase << ": " << LastName << endl;
}
else if(lowest_batting > OnBase) //Else-if condition
{
cout << "The worst hitter with batting average of " << Batting << ": " << LastName << endl;
}
else if(lowest_onbase < OnBase) //Else-if condition
{
cout << "The worst base runner with on-base average of " << OnBase << ": " << LastName << endl;
}
else if(LastName == disabled_list) //Else-if condition
{
cout << "Yes" << endl;
}
}
}
int main() //main() function
{r ifstream team; //input declarations
ofstream stats;
char disabled_list;
PlayerInfo Player[20]; //PlayerIDs with limit of 20
PlayerInfo PlayerID;
PlayerInfo LastName; //LastName
PlayerInfo FirstName; //FirstName
PlayerInfo Hits, Walks, Outs;
PlayerInfo Batting, OnBase;
team.open(infile);
if(team.fail())
{
cout << "The 2016 baseball season was cancelled" << endl;
return -1;
}
stats.open(outfile); // Output file will be open for writting
if(stats.fail())
{
cout << "The 2016 baseball season was cancelled" << endl;
team.close();
return -1;
}
while(!team.eof()) //while condition
{
GetPlayer(Player); //function declaration starts here
SortPlayerID();
PrintRoster();
GetStatistics();
Calculate();
SortPlayerName();
DisplayStatistics(disabled_list);
}
team.close();
stats.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.