473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Best way to contain / sort Baseball Statistics

I need to find the best way to contain baseball statistics in this format
Expand|Select|Wrap|Line Numbers
  1. player's name (20 characters)
  2.   player's team (3 characters)
  3.   at bats (integer)
  4.   hits (integer)
  5.   doubles (integer)
  6.   triples (integer)
  7.   homeruns (integer)
  8.  
So a sample file might look something like this.

Expand|Select|Wrap|Line Numbers
  1. David Aardsma         CHW   0   0   0   0   0
  2.   Bobby Abreu           NYY 605 171  40   5  16
  3.   Jeremy Accardo        TOR   0   0   0   0   0
  4.   Russ Adams            TOR  60  14   3   0   2
  5.   Brian N. Anderson     CHW  17   2   1   0   0
  6.  
and I need to be able to sort it by either team, hits, batting average, and slugging percentage.

I was thinking a giant table might be simple, but when you sort a table, all the columns move with the row, right?

is there a better way than a table? I guess thats the real question I'm asking
Apr 14 '08 #1
15 2947
gpraghuram
1,275 Expert 1GB
I need to find the best way to contain baseball statistics in this format
Expand|Select|Wrap|Line Numbers
  1. player's name (20 characters)
  2.   player's team (3 characters)
  3.   at bats (integer)
  4.   hits (integer)
  5.   doubles (integer)
  6.   triples (integer)
  7.   homeruns (integer)
  8.  
So a sample file might look something like this.

Expand|Select|Wrap|Line Numbers
  1. David Aardsma         CHW   0   0   0   0   0
  2.   Bobby Abreu           NYY 605 171  40   5  16
  3.   Jeremy Accardo        TOR   0   0   0   0   0
  4.   Russ Adams            TOR  60  14   3   0   2
  5.   Brian N. Anderson     CHW  17   2   1   0   0
  6.  
and I need to be able to sort it by either team, hits, batting average, and slugging percentage.

I was thinking a giant table might be simple, but when you sort a table, all the columns move with the row, right?

is there a better way than a table? I guess thats the real question I'm asking
The columns also has to move along with the row if u are sorting the table right?
R u asking how else u can store?

Raghuram
Apr 14 '08 #2
yes, or rather, is there an easier way to go about this?
Apr 14 '08 #3
gpraghuram
1,275 Expert 1GB
yes, or rather, is there an easier way to go about this?
You can store it in form of a structure.
this is advantageous when u want to sort it on the basis of different columns.
You have to write a comparison function for different columns and during run time yu can deceide which column to use for sorting.

Raghuram
Apr 14 '08 #4
oler1s
671 Expert 512MB
So, here's how you might analyze this issue. And there's no one answer to it.

You have a table of entries, in which you have a fixed number of columns and arbitrary number of rows. There is no table concept in C++, so you'll have to create your table either by programming an appropriate data structure or using something that fits in C++.

A very straightforward to look at this issue would be to create a structure in C++. It's a way to group various data types into some arbitrary giant datatype. And then work with that datatype as a whole. So you could represent a row with a structure. Store these rows in a vector.

When you want to sort the vector, you can rely on the sort function provided in <algorithm>. What you do is code a function that compares two rows in a certain method (by name, or a certain statistic, or a team, etc.) and then returns a value indicating less than or greater than. Pass this function into sort so that it can properly sort the rows.

You can look up all the C++ stuff I mentioned on Google to see real code and details.
Apr 14 '08 #5
I'm having trouble reading each line of data individually, since I'd like to have my structure in this form

Expand|Select|Wrap|Line Numbers
  1. struct stats
  2. {
  3.   char[20] name;
  4.   char[3] team;
  5.   int bats, doubles, triples, homeruns;
  6.   real BA, SP;
  7. }
  8.  
so how can I read each line of data (what I posted originally) into the structure, and then into the vector?

Using getline doesn't seem reasonable, since it reads the entire line as a string.

I was thinking of reading each character seperated by whitespace, but how does it know when the line has ended, and thus to create another structure?

Also, since multiple structures are being created, I was thinking of naming each structure " 1, 2, 3, ..." until the last data piece is read.

does something like
Expand|Select|Wrap|Line Numbers
  1. count = 1;
  2. stats count;
  3.  
read 1, or count?
Apr 16 '08 #6
Laharl
849 Expert 512MB
C++ doesn't have a 'real' builtin datatype. You probably mean 'double'. You should probably read a tutorial about how to use structs/classes (they're basically identical in C++).

As to your I/O questions, the newline character, \n, will tell you when you're at the end of a line. I'd probably use >> myself, since everything's going to be in the same order when you read it (I hope so, it'll make your life much, much easier).
Apr 16 '08 #7
how do you check to see when the newline character is reached?
Apr 16 '08 #8
gpraghuram
1,275 Expert 1GB
how do you check to see when the newline character is reached?
whenever u read a line using cin.getline it stops with the newline character

Raghuram
Apr 16 '08 #9
however if I use getline, it will just give me the line of data in a string, which doesn't easily enable me to separate the line into pieces, so that I can put those pieces into a struct.

I was thinking that since the data is formatted, I could use the get function to obtain the name, since it contains spaces that the >> couldn't get. I could then use >> to place the rest of the items into the assigned struct element.

However by using this method, I don't know how to get the program to recognize a new line, so the loop can restart, and a new struct item can be made.
Apr 16 '08 #10
Laharl
849 Expert 512MB
Remember that if you cin to a string (or char*), it's whitespace-delimited. Any whitespace, be it a tab, space, or newline (or the end of the file, for that matter). Also, since you're using C++, use std::string rather than C-strings.
Apr 16 '08 #11
oler1s
671 Expert 512MB
You're all making this way too hard. Myxamatosis, yes, getlines gets you a giant string. But you can always parse this string (tokenize, if you will) into individual strings, numbers, and so on. Stringstreams are a useful solution, although verbose. You should google stringstreams and various combinations like stringstreams convert or stringstreams tokenize and see what you get. You'll see plenty of code samples out there that show you how to deal with tokenizing a string.
Apr 16 '08 #12
Alright, the stringstream thing worked wonders for the program.

The problem now is with the structures. since I don't know the number of data pieces in the file, how do i go about naming each structure?

I had an idea of doing something like

Expand|Select|Wrap|Line Numbers
  1. struct stats myStats[100] = {0};
  2.  
so that way each struct could be named in the form
Expand|Select|Wrap|Line Numbers
  1. myStats[i].(member)
  2.  
however this gives me an error saying
"ISO C++ forbids assignment of arrays"

How do i create ever expanding structs?
Apr 17 '08 #13
Laharl
849 Expert 512MB
You can use a vector for that, rather than an array. Vectors are dynamically sized arrays that have protective code so that it's harder to do some of the nasty array/pointer screwups. If you really want to use the array, don't set it to null. Just use 'myStats stats[100];'.

Expand|Select|Wrap|Line Numbers
  1. vector<myStats> stats; //stats can be as large as your memory can fit, rather than limited by your size...the syntax with [] is the same as arrays
  2.  
Apr 17 '08 #14
here's the code I'm using to test out if assigning each structure element works. I know it doesn't though, and I think I'm getting confused by the syntax

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     struct stats
  4.     {
  5.         string name;
  6.         string team;
  7.         int bats, hits, doubles, triples, homeruns;
  8.     };
  9.  
  10.     vector<stats> myStats;
  11.  
  12.     string stat = "Justin Verlander";
  13.     string t = "DET";
  14.     int b = 0, h = 0, d = 0, trip = 0, hr = 0;
  15.  
  16.     myStats[0].name = stat;
  17.     myStats[0].team = t;
  18.     myStats[0].bats = b;
  19.     myStats[0].hits = h;
  20.     myStats[0].doubles = d;
  21.     myStats[0].triples = trip;
  22.     myStats[0].homeruns = hr;
  23. }
  24.  
so lets say I had a stats struct names "temp".

if I used "temp" instead of "myStats[0]" and then did
Expand|Select|Wrap|Line Numbers
  1. myStats.push_back(temp)
  2.  
do i Then access the elements by this?
Expand|Select|Wrap|Line Numbers
  1. cout << myStats[0].doubles << endl;
  2.  
is that the right syntax?
Apr 17 '08 #15
Laharl
849 Expert 512MB
That is the proper syntax. However, you cannot define a struct inside main() (or any other function). You must move it outside the function.
Apr 17 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Doug | last post by:
I am looking for PHP5 web hosting and many companies have not moved up to using PHP5 yet. Is there any one who has had any experiences good or bad with any company using PHP5? -d
3
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
4
by: P Adhia | last post by:
Hello, If the explain shows that DB2 needs to sort the result of a cursor, does that always happen? i.e. if the resultset of the cursor is empty, does the sort have any overhead? It appears...
2
by: Lucas Tam | last post by:
Hi all, I have an application which logs a considerable amount of data. Each day, we log about 50,000 to 100,000 rows of data. We like to report on this data... currently I'm using a stored...
8
by: Fredrik Melin | last post by:
I have a "Inventory" Object that contains the product and all its fields. The problem is that I am getting soooooo many functions under main Inventory class so it becames impossible to initalize...
6
by: Mudcat | last post by:
Hi, I am trying to build a tool that analyzes stock data. Therefore I am going to download and store quite a vast amount of it. Just for a general number - assuming there are about 7000 listed...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
5
by: gw7rib | last post by:
I'm writing a program which has "notes" - these can appear on the screen as windows with text in. It is possible to create an "index note" - at present, this will contain a list of the titles (or...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.