--TripWire--
asked on
Problem with algorithm - Calculating odds
Hello,
I am designing a program that reads a text file (csv comma separated) or sporting results of a particular team. See sample table below.
Column 1 - Date, Col 2 - Opponent, Col 3 - Result, etc. Any further columns are existent, but irrelevant (for now).
10/03/2010,PHI,W 8-7
10/02/2010,PHI,L 0-7
10/01/2010,PHI,L 5-11
09/29/10,FLA,W 5-1
09/28/10,FLA,W 3-2
09/27/10,FLA,W 2-1
09/26/10,@ WAS,L 2-4
09/25/10,@ WAS,W 5-0
09/24/10,@ WAS,L 3-8
Here is my code.
At the line //test for streak - I want to calculate the odds of a team winning the game FOLLOWING a series with a different team. For example, in the first 3 lines, you will see that this particular team played Philidelphia in a series, I want to know what are the odds of them beating the NEXT team they face, (which in this case is Florida).
I am designing a program that reads a text file (csv comma separated) or sporting results of a particular team. See sample table below.
Column 1 - Date, Col 2 - Opponent, Col 3 - Result, etc. Any further columns are existent, but irrelevant (for now).
10/03/2010,PHI,W 8-7
10/02/2010,PHI,L 0-7
10/01/2010,PHI,L 5-11
09/29/10,FLA,W 5-1
09/28/10,FLA,W 3-2
09/27/10,FLA,W 2-1
09/26/10,@ WAS,L 2-4
09/25/10,@ WAS,W 5-0
09/24/10,@ WAS,L 3-8
Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main()
{
[indent]char str[1024], team[6], chkConsecTeam[6], prevTeam[6]="N/A";
char *pch;
FILE *fp;
int first=1, second=0, test=0, column=0, count=0, consec=0;
bool isSeries=false; /*isConsecSeries=false;*/
fp=fopen("c:\\odds.txt", "r");
[indent]if (fp != NULL)
{
[indent]while (fgets(str, sizeof(str), fp) != NULL)
{
[indent]pch=strtok(str, ",");
column=0;
while(pch)
{
[indent]if((column++)==1)
{
[indent]strcpy(team, pch);
if(test=strcmp(team, prevTeam))
count++;
else if(count>1)
//test for streak
break;[/indent]
}[/indent]
[/indent]
pch=strtok(NULL, ",");
}
}
isSeries=false;
}
[/indent]
scanf("Waiting %d", second);
fflush(fp);
fclose(fp);[/indent]
return 0;
[/indent]
}
At the line //test for streak - I want to calculate the odds of a team winning the game FOLLOWING a series with a different team. For example, in the first 3 lines, you will see that this particular team played Philidelphia in a series, I want to know what are the odds of them beating the NEXT team they face, (which in this case is Florida).
It's pretty close already. I don't see you updating prevTeam anywhere. That should be done right after you get done testing.
Something like this is closer I think.
Something like this is closer I think.
if(0==strcmp(team, prevTeam))
{
count++;
}
else
{
count = 1;
if(count>3)
{
//A series just ended here
}
}
strcpy(prevTeam, team);
ASKER
Thank for the reply Tommy.
As far as I know, you cant say "if(0==" because 0 isn't a modifiable value.
My main question has to do with, what do I put at the commented line?
As far as I know, you cant say "if(0==" because 0 isn't a modifiable value.
My main question has to do with, what do I put at the commented line?
Hi Trip,
A couple of things need to happen before you can get to calculating the odds. :) High on the list is to actually store the data that is being read.
There are several ways to enhance your program, here's one. Still a lot of work to do, but this simplifies the general layout.
Kent
A couple of things need to happen before you can get to calculating the odds. :) High on the list is to actually store the data that is being read.
There are several ways to enhance your program, here's one. Still a lot of work to do, but this simplifies the general layout.
Kent
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main()
{
char str[1024],
team[6] = "",
prevTeam[6]="N/A";
char *Date, *Team, *Score;
FILE *fp;
fp=fopen("c:\\odds.txt", "r");
if (fp != NULL)
{
while (fgets(str, sizeof(str), fp) != NULL)
{
Date = strtok (str, ",");
Team = strtok (NULL, ",");
Score = strtok (NULL, ",");
if (strcmp (team, Team) == 0)
{
// team is the same as the previous line
}
else
{
// team changed this line
}
}
scanf("Waiting %d", second);
fclose(fp);
return 0;
}
ASKER
Thanks for the advice Kent. Question - why do I have to store the data, if what I'm reading is already coming from a txt file on the hard drive?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
He's not telling you to store all the data, just the whole current line (so you can get at the Ws and Ls etc)
I'm thinking ahead. :) As your program progresses, you'll probably want to do more than just check the previous or next game(s). if you store each game in the file into a table, you can easily scan the table to compute all kinds of statistics.
Kent
Change his line 24 to
if (strcmp (team, prevTeam) == 0)
He made a typo
if (strcmp (team, prevTeam) == 0)
He made a typo
I agree that you should eventually put the data in an array so you can run more stats on it, but I'd leave that for later.
ASKER
Ok thanks...
So, this is what I have so far...not quite sure where to go to next.
Especially with the line that goes if('W' ==
How do I skip to the next column?
So, this is what I have so far...not quite sure where to go to next.
Especially with the line that goes if('W' ==
How do I skip to the next column?
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main()
{
char str[1024],
team[6] = "",
prevTeam[6] = "N/A";
char *Date, *Team, *Score;
int first = 1,
second = 0,
test = 0,
column = 0,
count = 0,
winAfterSeries = 0,
lossAfterSeries = 0;
FILE *fp;
fp = fopen("c:\\odds.txt", "r");
if(fp != NULL)
{
while(fgets(str, sizeof(str), fp) != NULL)
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
if((column++) == 1)
strcpy(team, Team);
if(column>3)
{
if(strcmp(team, prevTeam) == 0)
count++; //team is the same as prev line
else
if('W' == )
winAfterSeries++;
else
lossAfterSeries++;
}
strcpy(prevTeam, team);
}
}
scanf("Waiting %d", second);
fclose(fp);
return 0;
}
Hi Trip,
What does the program need to do next?
Kent
What does the program need to do next?
Kent
ASKER
Look @ line 41. I don't have the criteria for my IF statement.
I just want the tally for wins vs. losses right now. I'll worry about the calculations later.
I just want the tally for wins vs. losses right now. I'll worry about the calculations later.
You have these lines
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
So what does Score mean? You might need to parse them to get the score. Is there a column that has a W or an L? If so, just keep using strtok to get that column.
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
So what does Score mean? You might need to parse them to get the score. Is there a column that has a W or an L? If so, just keep using strtok to get that column.
Something like
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
WinLoss= strtok(str, ",");
Then the if would look like
If(0==strcmp("W", WinLoss)) or something.
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
WinLoss= strtok(str, ",");
Then the if would look like
If(0==strcmp("W", WinLoss)) or something.
ASKER
Hmm, I'm not sure I know what you mean.
The score is in the third column. It has the result (1 char) / a space / and then the numerical score.
For example
W 8-7
L 0 - 7
L 5 - 11
How do I traverse to this column to get that result if I'm still evaluating the team column?
The score is in the third column. It has the result (1 char) / a space / and then the numerical score.
For example
W 8-7
L 0 - 7
L 5 - 11
How do I traverse to this column to get that result if I'm still evaluating the team column?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry -- line 36 should be:
if (*Score == 'W')
Kent
if (*Score == 'W')
Kent
ASKER
Thanks Kent, that works beautifully...however, I'm noticing it's adding one more to the Loss tally.
Try changing it to
if(*Score == 'W')
winAfterSeries++;
else if(*Score == 'L')
lossAfterSeries++;
That should fix the extra line being read issue.
if(*Score == 'W')
winAfterSeries++;
else if(*Score == 'L')
lossAfterSeries++;
That should fix the extra line being read issue.
What's probably happening is it was reading a junk last line with just a newline on it or something. Explicitly checking for the L will prevent it from being seen as a loss.
ASKER
Nevermind. I had a count a few times. Thank you so much!
Kdo, nice trick by the way of just casting the string to a char to check for the W or L.
ASKER
Yeah, I forgot about pointer properties.
ASKER
Actually, that wasn't quite what I was looking for, now that I see it.
Right now, it's giving me the total wins and losses. I'm only concerned with the amount of times a team wins AFTER they played the previous series.
I've changed the code slightly starting at line 32 of Kent's answer.
I think its sooo close right now. Could you please advise?
Right now, it's giving me the total wins and losses. I'm only concerned with the amount of times a team wins AFTER they played the previous series.
I've changed the code slightly starting at line 32 of Kent's answer.
if(strcmp(team, prevTeam) == 0)
test ++;
else
{
[indent]
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
strcpy(prevTeam, team);
}
}
[/indent]
}
I think its sooo close right now. Could you please advise?
if(strcmp(team, prevTeam) == 0)
test ++;
else
{
strcpy(prevTeam, team);
if(test > 3)
{
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
}
test = 0;
}
Hi Trip,
That should only count the first game of each series.
strcmp (Team, prevTeam) == 0
That tests for a "team change" in the list. If the team is the same as the previous team, the test for *Score == 'W' isn't done so there is no increment.
Note that the FIRST line in the file is counted because going from Team == NULL to any value looks like a team change. So the first game of each series is test. That may not be quite what you want.
Kent
That should only count the first game of each series.
strcmp (Team, prevTeam) == 0
That tests for a "team change" in the list. If the team is the same as the previous team, the test for *Score == 'W' isn't done so there is no increment.
Note that the FIRST line in the file is counted because going from Team == NULL to any value looks like a team change. So the first game of each series is test. That may not be quite what you want.
Kent
The check for previous team could just as easily look like the code below.
Actually, it's easier to read.
Kent
if(strcmp (Team, prevTeam) != 0)
{
if('W' == )
winAfterSeries++;
else
lossAfterSeries++;
strcpy(prevTeam, team);
}
You still need the else to increment the counter to check for a series.
If they play only one game against a team and then play another game with a different team, that's not a series. So the counter needs to be there.
ASKER
There are two other columns in my text file. Do I have to apportion pointers for them as well?
Such as these....?
Date = strtok (str, ",");
Team = strtok (NULL, ",");
Score = strtok (NULL, ",");
Such as these....?
Date = strtok (str, ",");
Team = strtok (NULL, ",");
Score = strtok (NULL, ",");
Hi Trip,
That split the original string into the columns noted by the commas. There are other ways to do this, and it probably needs some sanity checks. But it does work and it's easy to understand. :)
What else do you need to extract?
Kent
That split the original string into the columns noted by the commas. There are other ways to do this, and it probably needs some sanity checks. But it does work and it's easy to understand. :)
What else do you need to extract?
Kent
ASKER
I'm trying this code below. But I keep getting a zero count output on my print statements.
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main()
{
char str[1024],
team[6] = "",
prevTeam[6] = "N/A";
char *Date, *Team, *Score, *Line, *overUnder;
int first = 1,
second = 0,
test = 0,
column = 0,
count = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0;
FILE *fp;
fp = fopen("c:\\odds.txt", "r");
if(fp != NULL)
{
while(fgets(str, sizeof(str), fp) != NULL)
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
Line = strtok(NULL, ",");
overUnder = strtok(NULL, ",");
if(strcmp(team, prevTeam) == 0)
count++; //team is the same as prev line
else
{
if(count)
{
if(*Score == 'W')
{
winAfterSeries++;
}
else
lossAfterSeries++;
strcpy(prevTeam, team);
}
}
count = 0;
}
}
printf(" First game of series won %d\n", winAfterSeries);
printf(" First game of series lost %d\n", lossAfterSeries);
scanf("Waiting %d", second);
fclose(fp);
return 0;
}
Hi Trip,
The indention makes it a little tough to see, but line 54 needs to be moved after the bracket ('}') right below it.
Kent
The indention makes it a little tough to see, but line 54 needs to be moved after the bracket ('}') right below it.
Kent
ASKER
Sorry about that. Someone complained earlier about my un-indented code, which is understandable :)
I moved line 54, but still 0's on both tally's.
I moved line 54, but still 0's on both tally's.
Hi Trip,
Ahhhh, the joys of debugging code. :)
Try this one.
Kent
Ahhhh, the joys of debugging code. :)
Try this one.
Kent
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main()
{
char str[1024],
team[6] = "",
prevTeam[6] = "N/A";
char *Date, *Team, *Score, *Line, *overUnder;
int first = 1,
second = 0,
test = 0,
column = 0,
count = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0;
FILE *fp;
fp = fopen("c:\\odds.txt", "r");
if(fp != NULL)
{
while(fgets(str, sizeof(str), fp) != NULL)
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
Line = strtok(NULL, ",");
overUnder = strtok(NULL, ",");
if(strcmp(team, prevTeam) == 0)
count++; //team is the same as prev line
else
{
if(count)
{
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
strcpy(prevTeam, team);
count = 0;
}
}
}
}
printf(" First game of series won %d\n", winAfterSeries);
printf(" First game of series lost %d\n", lossAfterSeries);
scanf("Waiting %d", second);
fclose(fp);
return 0;
}
ASKER
Still nothing. Showing all zero's
ASKER
I've tried using a puts() function to display the contents of prevTeam and team.
There is nothing being stored in the team variable.
There is nothing being stored in the team variable.
Ok. One more time.
I didn't see that 'team' had snuck back into the program. The name that we're extracting from the string is in Team (capital 'T'). Since 'team' was never changed, the loop never detected a change. It was, in essence, working perfectly. :)
Kent
I didn't see that 'team' had snuck back into the program. The name that we're extracting from the string is in Team (capital 'T'). Since 'team' was never changed, the loop never detected a change. It was, in essence, working perfectly. :)
Kent
#include <stdio.h>
#include <stdlib.h>
#include <string>
int main()
{
char str[1024],
prevTeam[6] = "N/A";
char *Date, *Team, *Score, *Line, *overUnder;
int first = 1,
second = 0,
test = 0,
column = 0,
count = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0;
FILE *fp;
fp = fopen("c:\\odds.txt", "r");
if(fp != NULL)
{
while(fgets(str, sizeof(str), fp) != NULL)
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
Line = strtok(NULL, ",");
overUnder = strtok(NULL, ",");
if(strcmp(Team, prevTeam) == 0)
count++; //team is the same as prev line
else
{
if(count)
{
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
count = 0;
}
strcpy(prevTeam, Team);
}
}
}
printf(" First game of series won %d\n", winAfterSeries);
printf(" First game of series lost %d\n", lossAfterSeries);
scanf("Waiting %d", second);
fclose(fp);
return 0;
}
ASKER
THANK YOU! Works like a dream!!
ASKER
Hey guys, I've posted a follow-up question, because I have a few tweaks I want to make to this program.
I'd appreciate some more input from you. Thank you.
http://www.experts-exchang e.com/Prog ramming/La nguages/C/ Q_26867107 .html
I'd appreciate some more input from you. Thank you.
http://www.experts-exchang
ASKER