This is c++ program i need help with and it needs to be compiled with GNU++ Comp
ID: 3860667 • Letter: T
Question
This is c++ program i need help with and it needs to be compiled with GNU++ Compiler
it has part A, b please do both
Part A
Part B
Baseball.cpp
#include "Player.h"
#include <iostream>
#include <fstream>
void displayArray(Player team[], int team_size);
int sequeSearch(Player team[], int team_size,int number);
int main()
{
fstream fin("baseball.txt");
const int LIST_LENGTH = 20;
int number = 0, hits, walks, outs,players,index,teamSize = 0;
cout << "This program tracks a baseball player's number "
<< "and their number of hits, walks, and outs for "
<< "each games in a season. ";
Player team[LIST_LENGTH];
fin >> number >> hits >> walks >> outs;
while (!fin.eof())
{
int index=sequeSearch(team, teamSize, number);
if(index == -1)
{
team[teamSize].setNumber(number);
team[teamSize].setHits(hits);
team[teamSize].setWalks(walks);
team[teamSize].setOuts(outs);
teamSize++;
}
else
{
team[index].setHits(hits);
team[index].setWalks(walks);
team[index].setOuts(outs);
}
fin >> number >> hits >> walks >> outs;
}
displayArray(team,teamSize);
fin.close();
}
void displayArray(Player team[], int team_size)
{
cout << " Player Hits Walks Outs "
<< "------ ---- ----- ---- ";
for (int i=0; i < team_size; i++)
{
cout << team[i] << endl;
}
}
int sequeSearch(Player team[], int team_size,int number)
{
for (int i=0; i < team_size; i++)
{
if(team[i].getNumber()==number)
return i;
}
return -1;
}
Baseball.txt
baseball.txt
1 2 2 2
19 0 5 1
2 0 0 6
18 4 2 0
4 1 2 3
12 2 2 2
7 0 0 3
8 1 4 1
10 2 2 2
3 2 1 3
11 6 0 0
2 0 5 1
19 0 0 6
17 4 2 0
9 3 2 1
4 2 1 3
3 1 2 3
7 0 0 3
Player.cpp
#include "Player.h"
#include <iostream>
#include <iomanip>
using namespace std;
Player::Player()
{
Number = Hits = Walks = Outs = 0;
}
int Player::getNumber() const
{
return Number;
}
int Player::getHits() const
{
return Hits;
}
int Player::getWalks() const
{
return Walks;
}
int Player::getOuts() const
{
return Outs;
}
void Player::setNumber(int n)
{
Number = n;
}
void Player::setHits(int h)
{
Hits = h;
}
void Player::setWalks(int w)
{
Walks = w;
}
void Player::setOuts(int o)
{
Outs = o;
}
// support for assignments
const Player& Player::operator=(const Player & p)
{
// bypass self assignment
if (this != &p)
{
Number = p.Number;
Hits = p.Hits;
Walks = p.Walks;
Outs = p.Outs;
}
return *this;
}
// support for output to the monitor
ostream& operator<<(ostream& out, const Player & p)
{
out << setw(2) << p.Number << " "
<< setw(2) << p.Hits << " "
<< setw(2) << p.Walks << " "
<< setw(2) << p.Outs;
return out;
}
Player.h
Player.h:
#ifndef PLAYER
#define PLAYER
#include <iostream>
using namespace std;
class Player
{
public:
Player();
int getNumber() const;
int getHits() const;
int getWalks() const;
int getOuts() const;
void setNumber(int);
void setHits(int);
void setWalks(int);
void setOuts(int);
const Player& operator=(const Player &);
friend ostream& operator<<(ostream&, const Player & );
private:
int Number, Hits, Walks, Outs;
};
#endif
Explanation / Answer
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
void INIT(void);
char repeat = 'n';
int N = 11;
int PosAssign[4][9] = {0};
struct player
{
int inFld;
int outFld;
};
player* P;
int main()
{
int y = 0;
int i=0,j=0;
int posNum = 0;
srand((unsigned)time(0));
cout << "Each run produces a full game lineup ";
do
{
cout << "Enter number of players: ";
cin >> N;
P = new player[N];
INIT();
y = rand()%N;
for(j=0; j<=5; j++)
{
PosAssign[0][j] = y;
P[y].inFld = 1;
y = (1+y)%N;
}
for(j=6; j<=8; j++)
{
PosAssign[0][j] = y;
P[y].outFld = 1;
y = (1 + y)%N;
}
for( i=1; i<4; i++)
{
y = PosAssign[i-1][6];
posNum = 0;
j = 0;
do
{
if( (PosAssign[i-1][0] != y)&&(PosAssign[i-1][posNum] != y)&&( P[y].inFld < 2) )
{
PosAssign[i][posNum] = y;
P[y].inFld++;
posNum++;
}
j++;// this is just to prevent infinite loop in case not all positions can be filled
y = (1 + y)%N;
}while( (posNum <= 5)&&(j < 100) );
j = 0;
posNum = 6;// now for positions 6 thru 8
do// assign outfield positions
{
if( (PosAssign[i-1][0] != y)&&(P[y].outFld < 1) )
{
PosAssign[i][posNum] = y;
P[y].outFld = 1;
posNum++;
}
y = (1 + y)%N;
j++;
}while( (posNum <= 8)&&(j < 100) );
}
for( i=0; i<4; i++)
{
cout << endl << endl <<"lineup for inning " << i+1 << " is ";
for( j=0; j<=8; j++)
cout << PosAssign[i][j] << " ";
}
cout << endl << endl << "# of in/outfield assigns for each player are " << endl;
for(j=0; j<N; j++)
cout <<P[j].inFld << P[j].outFld << " ";
delete [] P;
cout << endl << "repeat (y/n)? " << flush;
cin >> repeat;
}while( repeat=='y');
return 0;
}
void INIT(void)
{
int j=0, k=0;
for(j=0; j<4; j++)
for(k=0; k<9; k++)
PosAssign[j][k] = -1;
for(j=0; j<N; j++)
P[j].inFld = P[j].outFld = 0;
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.