Best way to contain / sort Baseball Statistics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Myxamatosis
    New Member
    • Feb 2008
    • 30

    Best way to contain / sort Baseball Statistics

    I need to find the best way to contain baseball statistics in this format
    Code:
    player's name (20 characters)
      player's team (3 characters)
      at bats (integer)
      hits (integer)
      doubles (integer)
      triples (integer)
      homeruns (integer)
    So a sample file might look something like this.

    Code:
    David Aardsma         CHW   0   0   0   0   0
      Bobby Abreu           NYY 605 171  40   5  16
      Jeremy Accardo        TOR   0   0   0   0   0
      Russ Adams            TOR  60  14   3   0   2
      Brian N. Anderson     CHW  17   2   1   0   0
    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
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by Myxamatosis
    I need to find the best way to contain baseball statistics in this format
    Code:
    player's name (20 characters)
      player's team (3 characters)
      at bats (integer)
      hits (integer)
      doubles (integer)
      triples (integer)
      homeruns (integer)
    So a sample file might look something like this.

    Code:
    David Aardsma         CHW   0   0   0   0   0
      Bobby Abreu           NYY 605 171  40   5  16
      Jeremy Accardo        TOR   0   0   0   0   0
      Russ Adams            TOR  60  14   3   0   2
      Brian N. Anderson     CHW  17   2   1   0   0
    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

    Comment

    • Myxamatosis
      New Member
      • Feb 2008
      • 30

      #3
      yes, or rather, is there an easier way to go about this?

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by Myxamatosis
        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

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          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.

          Comment

          • Myxamatosis
            New Member
            • Feb 2008
            • 30

            #6
            I'm having trouble reading each line of data individually, since I'd like to have my structure in this form

            Code:
            struct stats
            {
              char[20] name;
              char[3] team;
              int bats, doubles, triples, homeruns;
              real BA, SP;
            }
            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
            Code:
            count = 1;
            stats count;
            read 1, or count?

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              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).

              Comment

              • Myxamatosis
                New Member
                • Feb 2008
                • 30

                #8
                how do you check to see when the newline character is reached?

                Comment

                • gpraghuram
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1275

                  #9
                  Originally posted by Myxamatosis
                  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

                  Comment

                  • Myxamatosis
                    New Member
                    • Feb 2008
                    • 30

                    #10
                    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.

                    Comment

                    • Laharl
                      Recognized Expert Contributor
                      • Sep 2007
                      • 849

                      #11
                      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.

                      Comment

                      • oler1s
                        Recognized Expert Contributor
                        • Aug 2007
                        • 671

                        #12
                        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.

                        Comment

                        • Myxamatosis
                          New Member
                          • Feb 2008
                          • 30

                          #13
                          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

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

                          How do i create ever expanding structs?

                          Comment

                          • Laharl
                            Recognized Expert Contributor
                            • Sep 2007
                            • 849

                            #14
                            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];'.

                            [CODE=cpp]
                            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
                            [/CODE]

                            Comment

                            • Myxamatosis
                              New Member
                              • Feb 2008
                              • 30

                              #15
                              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

                              Code:
                              int main()
                              {
                              	struct stats
                              	{
                              		string name;
                              		string team;
                              		int bats, hits, doubles, triples, homeruns;
                              	};
                              
                              	vector<stats> myStats;
                              
                              	string stat = "Justin Verlander";
                              	string t = "DET";
                              	int b = 0, h = 0, d = 0, trip = 0, hr = 0;
                              
                              	myStats[0].name = stat;
                              	myStats[0].team = t;
                              	myStats[0].bats = b;
                              	myStats[0].hits = h;
                              	myStats[0].doubles = d;
                              	myStats[0].triples = trip;
                              	myStats[0].homeruns = hr;
                              }
                              so lets say I had a stats struct names "temp".

                              if I used "temp" instead of "myStats[0]" and then did
                              Code:
                              myStats.push_back(temp)
                              do i Then access the elements by this?
                              Code:
                              cout << myStats[0].doubles << endl;
                              is that the right syntax?

                              Comment

                              Working...