Reputation: 79
I have a text file that is storing a baseball player id and that players rbi (runs batted in) totals. The ids are all four digits and the rbis range from 60 to 110. It looks like this.
5554 102 87 63 90 5553 66 68 90 102 etc...
I wrote some code to store the ids in a set, calculate the average of the four rbi totals from the text file, and output the results to the console. My homework assignment says that I have to store the player id and rbi average to a map pair, not a set. I thought I read on that the syntax for the mapped pair is
typedef pair<const Key, T> value_type;
but I am having trouble rewriting this code with a mapped pair. Any thoughts?
#include <iostream>
#include <fstream>
#include <set>
using namespace std;
int main()
{
ifstream input("filepath\\*.txt");
multiset<int> values;
// Read data from file
for(unsigned int j = 1; j <= 4; j++)
{
int player;
(input >> player);
int rbi;
double total = 0.0;
double average = 0.0;
for(unsigned int i = 1; i <= 4; i++)
{
// Compute the average.
(input >> rbi);
values.insert(rbi);
total += rbi;
average = total/4;
}
//Output totals to console
cout << player;
cout << " " << average << endl;
}
system("Pause");
return 0;
}
Upvotes: 3
Views: 5756
Reputation: 42964
First, I'd like to share with you some notes on your code you may find useful.
You use redundant parentheses when reading data from file:
int player (input >> player);
But the following is just fine:
int player;
input >> player; // No need for (...)
Moreover, you may want to fix your indentation style. Instead of:
int rbi; double total = 0.0; double average = 0.0; for(unsigned int i = 1; i <= 4; i++) { // Compute the average. (input >> rbi); values.insert(rbi); total += rbi; average = total/4; }
Make it:
int rbi;
double total = 0.0;
double average = 0.0;
for (unsigned int i = 1; i <= 4; i++)
{
// <--------- Indent here, INSIDE loop body
// Compute the average.
input >> rbi;
values.insert(rbi);
total += rbi;
average = total/4;
}
Moreover, instead of using "magic numbers" like 4
, consider using constants like kPlayerCount
. This will make your code easier to read and maintain.
Regarding your particular problem, if you want to store pairs of (player ID, average RBI) you can just use a std::map
.
Assuming that player IDs are stored in int
s, and the averages RBI in double
s, you can use:
std::map<int, double> result;
(A more elegant solution could be to use typedef
instead of "raw" types, e.g. typedef int PlayerID;
and std::map<PlayerID, ...
.)
Moreover, to compute the average you store the values in a std::multiset
. In general, if you want to store a sequence of values, the first choice should be a simple std::vector
; you can use its push_back()
method to add data to it, and the vector will automatically resize to accommodate new data.
But in this case you don't need to store the read data somewhere (and then throwing away the container with the read data): you just need to compute the average. So you just need to accumulate the read data in a total, and then compute the average with a division.
Here is a possible rewrite of your code, following the aforementioned notes:
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
int main()
{
static const int kPlayerCount = 4;
static const int kRbiCount = 4;
// Store pairs (PlayerID, RBI average)
map<int, double> result;
// Read data from file
ifstream input("data.txt");
for (int currPlayer = 1; currPlayer <= kPlayerCount; currPlayer++)
{
int playerId;
input >> playerId;
// Read RBIs and compute average
int rbi;
input >> rbi;
double total = rbi;
for (int i = 2; i <= kRbiCount; i++)
{
input >> rbi;
total += rbi;
}
const double average = total / kRbiCount;
// Store player ID and RBI average in the map
result[playerId] = average;
}
// Output results to console
for (auto it = result.begin(); it != result.end(); ++it)
{
cout << "Player: " << it->first << endl;
cout << "Average RBI: " << it->second << endl;
cout << "--------" << endl;
}
}
With an input file containing this data:
5554 102 87 63 90 5553 66 68 90 102 5560 67 77 99 100 5540 88 77 100 102
the output is:
Player: 5540 Average RBI: 91.75 -------- Player: 5553 Average RBI: 81.5 -------- Player: 5554 Average RBI: 85.5 -------- Player: 5560 Average RBI: 85.75 --------
Upvotes: 3
Reputation: 2229
I think they want you to store the results in std::map. It is a container for key-value pairs in C++ (std::pair). You can insert values to the map by using this syntax: map[key] = value;
for example.
std::map<int, int> baseball_players;
for (...)
{
// Calculate average
baseball_players[player] = average;
}
Upvotes: 3