Hello, I\'m currently working on a programming assignment and I\'m quite stuck o
ID: 3843778 • Letter: H
Question
Hello,
I'm currently working on a programming assignment and I'm quite stuck on getting started. I'm going to be using a Stack class I created in a pervious assignment.
Here's the general idea of the assignment:
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.
Cuurently, I have a class called PlayerInfo with the functions:
void AddPlayer(string name, double batAvg), void printLow(), and void printHigh()
Any help/suggestions would be gladly appreciated!
Explanation / Answer
The program is as follows:
#include<iostream.h>
#include<string>
struct info {
string name;
double avg;
};
class stack {
private:
int array[50];
int top;
public:
stack(){
top = 0;
}
bool isempty(){
if (top == 0)
return true;
else
return false;
}
bool isfull(){
if (top == 50)
return true;
else
return false;
}
void push(int n){
if (!isfull()){
array[top] = n;
top++;
}
else {
cout << "Stack is Full" << endl;
}
}
int pop(){
if (!isempty()){
top = top - 1;
return array[top];
}
else{
cout << "Stack empty" << endl;
return -1;
}
}
}
class PlayerIinfo {
private:
info list[100];
stack st_high;
stack st_low;
int count;
public:
PlayerInfo(){
count = 0;
}
void AddPlayer(string name, double batAvg){
list[count].name = name;
list[count].avg = batAvg;
count++;
}
void printHigh(){
float max;
max = list[0].avg;
for (int i =0; i<count; i++){
if (list[i].avg > max)
max = list[i].avg;
}
for (int i =0; i<count; i++){
if (list[i].avg == max)
st_high.push(i);
}
while (!st_high.isempty()){
cout << list[st_high.pop()].name << " " << list[st_high.pop()].avg <<endl;
}
}
void printLow(){
float min;
min = list[0].avg;
for (int i =0; i<count; i++){
if (list[i].avg < min)
min = list[i].avg;
}
for (int i =0; i<count; i++){
if (list[i].avg == min)
st_low.push(i);
}
while (!st_low.isempty()){
cout << list[st_low.pop()].name << " " << list[st_low.pop()].avg <<endl;
}
}
};
void main(){
PlayerInfo pl;
ifstream infile;
string name;
double avg;
infile.open("Input.txt"); // Assuming the name of input file is "Input.txt"
while (infile >> name >> avg){
pl.AddPlayer(name, avg);
}
infile.close();
pl.printHigh();
pl.printLow();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.