Solved

# Problem with algorithm - Calculating odds

Posted on 2011-03-01
645 Views
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.

``````#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).
0
Question by:--TripWire--
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 17
• 13
• 11

Author Comment

ID: 35009609
Please ignore the indent tags!  Placed there by mistake.
0

LVL 37

Expert Comment

ID: 35009996
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.
``````if(0==strcmp(team, prevTeam))
{
count++;
}
else
{
count = 1;
if(count>3)
{
//A series just ended here
}
}
strcpy(prevTeam, team);
``````
0

Author Comment

ID: 35010047

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?
0

LVL 45

Expert Comment

ID: 35010057
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

``````#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;
}
``````
0

Author Comment

ID: 35010092
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?
0

LVL 37

Assisted Solution

TommySzalapski earned 225 total points
ID: 35010104
The == means just test and return true if they are equal. You shouldn't use = in if statements at all (usually). You're supposed to use == for if statements.

I would set up two main counts
int winAfterSeries = 0;
int loseAfterSeries = 0;

Then
if(count>3)
{
//A series just ended here
if('W' == whateverColumnHasTheInfo)
winAfterSeries++;
else
lossAfterSeries++;
}

Then at the end your odds are
100.0*winAfterSeries/(winAfterSeries + lossAfterSeries)

The 100 is to format it as a percent. the .0 is to cast as a double so you don't get integer division.
0

LVL 37

Expert Comment

ID: 35010114
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)
0

LVL 45

Expert Comment

ID: 35010119

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
0

LVL 37

Expert Comment

ID: 35010127
Change his line 24 to
if (strcmp (team, prevTeam) == 0)
0

LVL 37

Expert Comment

ID: 35010135
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.
0

Author Comment

ID: 35010431
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' ==

``````#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;
}
``````
0

LVL 45

Expert Comment

ID: 35010458
Hi Trip,

What does the program need to do next?

Kent
0

Author Comment

ID: 35010528
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.
0

LVL 37

Expert Comment

ID: 35010573
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.
0

LVL 37

Expert Comment

ID: 35010586
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.
0

Author Comment

ID: 35010633
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?
0

LVL 45

Accepted Solution

Kent Olsen earned 275 total points
ID: 35010660
Hi Trip,

Try the program below.  :)

Kent

``````#include <stdio.h>
#include <stdlib.h>
#include <string>

int main()
{
char str[1024],
prevTeam[6] = "";

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(strcmp (Team, prevTeam) == 0)
count++; //team is the same as prev line
else
{
if('W' == )
winAfterSeries++;
else
lossAfterSeries++;
strcpy(prevTeam, team);
}
}
}
}
printf (" First game of series won = %d\n", winAfterSeries);
printf (" First game of series lost = %d\n", lossAfterSeries);
fclose(fp);

return 0;
}
``````
0

LVL 45

Expert Comment

ID: 35010677
Sorry -- line 36 should be:

if (*Score == 'W')

Kent
0

Author Comment

ID: 35010777
Thanks Kent, that works beautifully...however, I'm noticing it's adding one more to the Loss tally.
0

LVL 37

Expert Comment

ID: 35010816
Try changing it to
if(*Score == 'W')
winAfterSeries++;
else if(*Score == 'L')
lossAfterSeries++;

That should fix the extra line being read issue.
0

LVL 37

Expert Comment

ID: 35010829
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.
0

Author Comment

ID: 35010839
Nevermind.  I had a count a few times.  Thank you so much!
0

LVL 37

Expert Comment

ID: 35010845
Kdo, nice trick by the way of just casting the string to a char to check for the W or L.
0

Author Comment

ID: 35010884
Yeah, I forgot about pointer properties.
0

Author Comment

ID: 35010982
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.

``````if(strcmp(team, prevTeam) == 0)
test ++;
else
{
[indent]
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
strcpy(prevTeam, team);
}
}
[/indent]
}
``````

0

LVL 37

Expert Comment

ID: 35011041
``````if(strcmp(team, prevTeam) == 0)
test ++;
else
{
strcpy(prevTeam, team);
if(test > 3)
{
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
}
test = 0;
}
``````
0

LVL 45

Expert Comment

ID: 35011062
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
0

LVL 45

Expert Comment

ID: 35011083

The check for previous team could just as easily look like the code below.

Kent

``````if(strcmp (Team, prevTeam) != 0)
{
if('W' == )
winAfterSeries++;
else
lossAfterSeries++;
strcpy(prevTeam, team);
}
``````
0

LVL 37

Expert Comment

ID: 35011116
You still need the else to increment the counter to check for a series.
0

LVL 37

Expert Comment

ID: 35011126
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.
0

Author Comment

ID: 35012570
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, ",");
0

LVL 45

Expert Comment

ID: 35012668
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
0

Author Comment

ID: 35013001
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;
}
``````
0

LVL 45

Expert Comment

ID: 35014210
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
0

Author Comment

ID: 35019117
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.
0

LVL 45

Expert Comment

ID: 35020432
Hi Trip,

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;
}
``````
0

Author Comment

ID: 35020790
Still nothing.  Showing all zero's
0

Author Comment

ID: 35020888
I've tried using a puts() function to display the contents of prevTeam and team.
There is nothing being stored in the team variable.
0

LVL 45

Expert Comment

ID: 35021378
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

``````#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;
}
``````
0

Author Comment

ID: 35021607
THANK YOU!  Works like a dream!!
0

Author Comment

ID: 35048006
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-exchange.com/Programming/Languages/C/Q_26867107.html
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

An API detour question 7 107
Dice Roll Probabilities 3 153
Windows 2008 standard, Disk Cleanup, and Winsxs 30 58
.NET Core supports which cell phone platforms? 3 35
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp aâ€¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month5 days, 11 hours left to enroll