Please write a C++ program that will process the result of the super bowls. You
ID: 3572977 • Letter: P
Question
Please write a C++ program that will process the result of the super bowls.
You will be using a data file called superbowl.dat. There will be three lines for each super bowl.
THE DATA IS LISTED BELOW
Create functions as described above (six total functions)!
1. read the datafile
2. calculate average points
3. calculate number of games with over 40 points
4. find winner and loser in game with most points
5. find winner and loser in game with least points
6. one function to output all results
The first line will contain the name of the winning team, the second line will contain the name of the losing team and the third line will contain the number of points scored by the winning team.
You are to read from the file and place the values in parallel arrays. There have been 47 super bowls.
The program should read into the arrays.
The program should then print out the arrays side by side in neat, labeled columns.
It should print out the average number of points scored by the winning team, the number of winning teams that scored more than 40 points, the winner and loser in the super bowl where the most points were scored and the winner and the loser in the super bowl where the least points were scored.
Each calculation should have a function of its own. NO output should be done in functions that do calculations. All output should be done in one output function. Remember to always pass the number of elements in the array with the array to functions.
Superbowl.dat:
Packers
Chiefs
35
Packers
Raiders
33
Jets
Colts
16
Chiefs
Vikings
23
Colts
Cowboys
16
Cowboys
Dolphins
24
Dolphins
Redskins
14
Dolphins
Vikings
24
Steelers
Vikings
16
Steelers
Cowboys
21
Raiders
Vikings
32
Cowboys
Broncos
27
Steelers
Vikings
35
Steelers
Rams
31
Raiders
Eagles
27
49ers
Bengals
26
Redskins
Dolphins
27
Raiders
Redskings
38
49ers
Dolphins
38
Bears
Patriots
46
Giants
Broncos
39
Redskins
Broncos
42
49ers
Bengals
20
49ers
Broncos
55
Giants
Bills
20
Redskins
Bills
37
Cowboys
Bills
52
Cowboys
Bills
30
49ers
Chargers
49
Cowboys
Steelers
27
Packers
Patriots
35
Broncos
Packers
31
Broncos
Falcons
34
Rams
Titans
23
Ravens
Giants
34
Patriots
rams
20
Buccaneers
Raiders
48
Patriots
Panthers
32
Patriots
Eagles
24
Steelers
Seahawks
21
Colts
Bears
29
Giants
Patriots
17
Steelers
Cardinals
27
Saints
Colts
31
Packers
Steelers
31
Giants
Patriots
21
Ravens
49ers
34
Seahawks
Broncos
43
Patriots
Seahawks
28
Broncos
Panthers
24
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdlib.h>
using namespace std;
class superbowl
{
vector<string> teamA;
vector<string> teamB;
vector<int> points;
public:
void readFile(void);
void findWL_mostPoints(void);
void findWL_leastPoints(void);
void output(void);
};
void superbowl::findWL_mostPoints(void)
{
vector<string>::iterator iter1=teamA.begin();
vector<string>::iterator iter2=teamB.begin();
int max=0;
string tmp1,tmp2;
for(vector<int>::iterator iter=points.begin();iter!=points.end();iter++,iter1++,iter2++){
if(max<*iter)
{
max=*iter;
tmp1.assign(*iter1);
tmp2.assign(*iter2);
}
}
cout<<"The winner and loser in the game with most points is:"<<endl;
cout<<tmp1<<"|"<<tmp2<<"|"<<max<<endl;
}
void superbowl::findWL_leastPoints(void)
{
vector<string>::iterator iter1=teamA.begin();
vector<string>::iterator iter2=teamB.begin();
vector<int>::iterator iter=points.begin();
int min=*iter;
string tmp1,tmp2;
tmp1.assign(*iter1);
tmp2.assign(*iter2);
for(vector<int>::iterator iter=points.begin();iter!=points.end();iter++,iter1++,iter2++){
if(min>*iter)
{
min=*iter;
tmp1.assign(*iter1);
tmp2.assign(*iter2);
}
}
cout<<"The winner and loser in the game with least points is:"<<endl;
cout<<tmp1<<"|"<<tmp2<<"|"<<min<<endl;
}
void superbowl::readFile(void)
{
ifstream ifs;
ifs.open("Superbowl.dat");
if(!ifs.is_open())
{
cout<<"Error in opening the file"<<endl;
return;
}
int i=0;
string tmp,tmp2;
while(!ifs.eof())
{
ifs>>tmp;
ifs>>tmp2;
ifs>>i;
if(tmp.empty()) break;
teamA.push_back(tmp);
teamB.push_back(tmp2);
points.push_back(i);
i=0;
tmp.clear();
tmp2.clear();
}
ifs.close();
}
void superbowl::output(void)
{
vector<string>::iterator iter1=teamA.begin();
vector<string>::iterator iter2=teamB.begin();
for(vector<int>::iterator itr=points.begin();itr!=points.end();itr++,iter1++,iter2++)
cout<<"|"<<*iter1<<"|"<<*iter2<<"|"<<*itr<<"|"<<endl;
}
int main(void)
{
superbowl sb;
sb.readFile();
sb.output();
sb.findWL_mostPoints();
sb.findWL_leastPoints();
return 0;
}
Output:
|Packers|Chiefs|35|
|Packers|Raiders|33|
|Jets|Colts|16|
|Chiefs|Vikings|23|
|Colts|Cowboys|16|
|Cowboys|Dolphins|24|
|Dolphins|Redskins|14|
|Dolphins|Vikings|24|
|Steelers|Vikings|16|
|Steelers|Cowboys|21|
|Raiders|Vikings|32|
|Cowboys|Broncos|27|
|Steelers|Vikings|35|
|Steelers|Rams|31|
|Raiders|Eagles|27|
|49ers|Bengals|26|
|Redskins|Dolphins|27|
|Raiders|Redskings|38|
|49ers|Dolphins|38|
|Bears|Patriots|46|
|Giants|Broncos|39|
|Redskins|Broncos|42|
|49ers|Bengals|20|
|49ers|Broncos|55|
|Giants|Bills|20|
|Redskins|Bills|37|
|Cowboys|Bills|52|
|Cowboys|Bills|30|
|49ers|Chargers|49|
|Cowboys|Steelers|27|
|Packers|Patriots|35|
|Broncos|Packers|31|
|Broncos|Falcons|34|
|Rams|Titans|23|
|Ravens|Giants|34|
|Patriots|rams|20|
|Buccaneers|Raiders|48|
|Patriots|Panthers|32|
|Patriots|Eagles|24|
|Steelers|Seahawks|21|
|Colts|Bears|29|
|Giants|Patriots|17|
|Steelers|Cardinals|27|
|Saints|Colts|31|
|Packers|Steelers|31|
|Giants|Patriots|21|
|Ravens|49ers|34|
|Seahawks|Broncos|43|
|Patriots|Seahawks|28|
|Broncos|Panthers|24|
The winner and loser in the game with most points is:
49ers|Broncos|55
The winner and loser in the game with least points is:
Dolphins|Redskins|14