Homework 4
Data Structures
Assignment Topic: Merge Processing files of struct
Write 5 different functions to calculate and output a summary of
l how many points did each team gain?
l how many games did each team won,
l the name of the team that has the most points,
l and the name of the team with the fewest points.
l any other interesting statistics.
Write a C++ (or similar programming language code: c, java, c#, or python) code that includes following functionalities:
I. Store at least three different data sets of sport teams each in a separate external text file or similar (csv files- 1 more point and json files- 2 more points are accepted). Each file consists of 10-15 different rows. Where each row consists of at least 4 different values.
Example of file structure:
Team Name, Team Play Against, First Team Score, Second Team Score
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Match
{
string nameTeam1;
string nameTeam2;
int points1;
int points2;
int win;
};
void PointsGained(vector<Match> matches)
{
for(int i = 0; i < matches.size(); ++i)
{
cout << "Team1: " << matches[i].nameTeam1 << " Points: " << matches[i].points1 << " Team2: "
<< matches[i].nameTeam2 << " Points: " << matches[i].points2 << endl;
}
}
void GamesWon(vector<Match> matches)
{
for(int i = 0; i < matches.size(); ++i)
{
cout << "Team1: " << matches[i].nameTeam1 << " Wins: " << matches[i].points1/3.0 << " Team2: "
<< matches[i].nameTeam2 << " Points: " << )matches[i].points2/3.0 << endl;
}
}
int main()
{
vector<Match> matches;
ofstream out;
Match m;
for(int i = 0; i < 3; ++i)
{
out.open("matches.txt");
if(out.is_open())
{
cout << "Enter name of first team: ";
cin >> m.nameTeam1;
out << m.nameTeam1 << "\n";
cout << "Enter name of second team: ";
cin >> m.nameTeam2;
out << m.nameTeam2 << "\n";
cout << "Enter points of first team: ";
cin >> m.points1;
out << m.points1 << "\n";
cout << "Enter points of second team: ";
cin >> m.points2;
out << m.points2 << "\n";
cout << "Enter who won(1 - first team, 2 - second team, 0 - draw): ";
cin >> m.win;
out << m.win << "\n";
matches.push_back(m);
}
else
exit(0);
out.close();
}
cout << endl << endl;
PointsGained(matches);
cout << endl << endl;
GamesWon(matches);
system("pause");
return 0;
}
Comments
Leave a comment