Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Changing program execution - Algorithm Tweaks

Posted on 2011-03-06
Medium Priority
707 Views
Hello,

I developed the program below with help from another thread.

http://www.experts-exchange.com/Programming/Languages/C/Q_26855857.html

Now, I only want the program to calculate the likelihood of a team winning the first game in THREE game series...AFTER another three game series.

In other words...I want the program to scan through the results and detect all instances of two back-to-back 3-game series, and calculate results ONLY for those events.

Please let me know if any further clarification is needed.
Thanks

``````#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
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
• 16
• 16
• 2

LVL 46

Expert Comment

ID: 35048095
Ok.  :)

1.  We're going to add two variables.  (Actually, we'll just add one and rename one so that the program reads easier.)

int PreviousSeriesLength = 0;
int SeriesLength = 0;

2.  Everywhere you have the variable "count", replace it with "SeriesLength".

3.  Line 40 will test SeriesLength (you should have already changed the name in step 2.)
But instead of testing for non-zero, test for it being equal to 3.  (perhaps great than or equal to 3 in case of a 4-game series?)

4.  After Line 41 (the bracket), test for PreviousSeriesLength > 3 (or >= 3).

5.  After Line 48, set PreviousSeriesLength to SeriesLength.

That should just about do it.  (I suggest a couple of print statements along the way to make sure that it's doing what you want.)  :)

Good Luck,
Kent

0

Author Comment

ID: 35049514
Thanks Kent.  But I placed a print statement (that's not printing) shown below.

``````if(SeriesLength == 3)
{
printf("Prev series: %d\tSeries: %d", PreviousSeriesLength, SeriesLength);
if(PreviousSeriesLength == 3)
{
puts("if win");
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;

SeriesLength = 0;
}
}
``````
0

LVL 46

Expert Comment

ID: 35050392
Hi Trip,

Move the printf up above the if() statement so that you can see every line go past.  Oh, and the line probably needs a newline.

printf("Prev series: %d\tSeries: %d\n", PreviousSeriesLength, SeriesLength);

Kent

0

Author Comment

ID: 35051197
I had some print statements above that if condition. - Everything prints above that line.
0

LVL 46

Expert Comment

ID: 35051373
That's a good sign that the if() statement is always false.  That is, the program never hits that if() statement when SeriesLength == 3.

Can you post the entire program?

Kent
0

Author Comment

ID: 35051419
Sure, no problem 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,
SeriesLength = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0,
PreviousSeriesLength = 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)
SeriesLength++;   //team is the same as prev line
else
{
if(SeriesLength == 3)
{
printf("Prev series: %d\tSeries: %d", PreviousSeriesLength, SeriesLength);
if(PreviousSeriesLength == 3)
{
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;

SeriesLength = 0;
}
}
strcpy(prevTeam, Team);
PreviousSeriesLength = SeriesLength;
}
}
}
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 46

Expert Comment

ID: 35051613
Ok.

After line 55, we need to reset the SeriesLength variable.  Since we're now on the first line of a new series, setting it to 1 is appropriate.  It's being reset at line 51, which isn't correct --  either in that spot or value.  :)

SeriesLength = 1;

If that doesn't do it, move the print statement at line 43 after line 40.

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,
SeriesLength = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0,
PreviousSeriesLength = 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)
SeriesLength++;   //team is the same as prev line
else
{
if(SeriesLength == 3)
{
printf("Prev series: %d\tSeries: %d", PreviousSeriesLength, SeriesLength);
if(PreviousSeriesLength == 3)
{
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
}
}
strcpy(prevTeam, Team);
PreviousSeriesLength = SeriesLength;
SeriesLength = 1;
}
}
}
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: 35052033
Ok good stuff.  I'm now getting numbers coming up.  But, I don't think it's counting my results properly.
I'll show you an example.  I got 1 game won and 1 game lost for the data below.

10/03/2010,@ BOS,L 4-8,L 112,O 9.5 111
10/02/2010,@ BOS,L 6-7,L 102,O 9.5 -101
10/02/2010,@ BOS,W 6-5,W -134,O 9.5 111
09/29/10,@ TOR,L 4-8,L 105,O 9 -116
09/28/10,@ TOR,W 6-1,W -181,U 8.5 -105
09/27/10,@ TOR,L 5-7,L -124,O 9.5 -114
09/26/10,BOS,W 4-3,W -183,U 9.5 -108
09/25/10,BOS,L 3-7,L 109,O 9 -105
09/24/10,BOS,L 8-10,L -175,O 9.5 -105
09/23/10,TB,L 3-10,L -136,O 8 106
09/22/10,TB,L 2-7,L -137,U 10.5 -121
09/21/10,TB,W 8-3,W -133,O 9.5 -105
09/20/10,TB,W 8-6,W -119,O 9.5 -120
09/19/10,@ BAL,L 3-4,L -164,U 9 -111
09/18/10,@ BAL,W 11-3,W -184,O 8 110
09/17/10,@ BAL,W 4-3,W -149,U 9.5 -116
09/15/10,@ TB,L 3-4,L 107,U 9.5 -115
09/14/10,@ TB,W 8-7,W 123,O 8.5 -107
09/13/10,@ TB,L 0-1,L 115,U 7.5 -117
09/12/2010,@ TEX,L 1-4,L 161,U 9.5 -112
09/11/2010,@ TEX,L 6-7,L -103,O 10.5 101
09/10/2010,@ TEX,L 5-6,L 129,O 9.5 -105
09/08/2010,BAL,W 3-2,W -243,U 10 106
09/07/2010,BAL,L 2-6,L -385,U 9 -109
09/06/2010,BAL,L 3-4,L -195,U 9.5 -119
0

LVL 46

Expert Comment

ID: 35052215
After line 40, insert this line:

printf("Prev %d - %s \tseries: %s - %d\n", prevTeam, PreviousSeriesLength, Team, SeriesLength);

That should show us what we're missing.

Also, line 61 should pass &second.

Kent
0

Author Comment

ID: 35052556
Thanks.  Almost there.
There's a small mistake in your print line, you inverted the first two variables.

It's counting all the right results, except for the last series in the set.

Example
With this data it's saying Won: 1 / Lost: 2 - it should be Lost 3.

10/03/2010,@ BOS,L 4-8,L 112,O 9.5 111
10/02/2010,@ BOS,L 6-7,L 102,O 9.5 -101
10/02/2010,@ BOS,W 6-5,W -134,O 9.5 111
09/29/10,@ TOR,L 4-8,L 105,O 9 -116
09/28/10,@ TOR,W 6-1,W -181,U 8.5 -105
09/27/10,@ TOR,L 5-7,L -124,O 9.5 -114
09/26/10,BOS,W 4-3,W -183,U 9.5 -108
09/25/10,BOS,L 3-7,L 109,O 9 -105
09/24/10,BOS,L 8-10,L -175,O 9.5 -105
09/23/10,TB,L 3-10,L -136,O 8 106
09/22/10,TB,L 2-7,L -137,U 10.5 -121
09/21/10,TB,W 8-3,W -133,O 9.5 -105
09/20/10,TB,W 8-6,W -119,O 9.5 -120
09/19/10,@ BAL,L 3-4,L -164,U 9 -111
09/18/10,@ BAL,W 11-3,W -184,O 8 110
09/17/10,@ BAL,W 4-3,W -149,U 9.5 -116
09/15/10,@ TB,L 3-4,L 107,U 9.5 -115
09/14/10,@ TB,W 8-7,W 123,O 8.5 -107
09/13/10,@ TB,L 0-1,L 115,U 7.5 -117
09/12/2010,@ TEX,L 1-4,L 161,U 9.5 -112
09/11/2010,@ TEX,L 6-7,L -103,O 10.5 101
09/10/2010,@ TEX,L 5-6,L 129,O 9.5 -105

----

By the way.  You can ignore that print statement.  The value of second is irrelevant.  It's just there to stop the program from terminating before I get a chance to read the output.
0

LVL 46

Expert Comment

ID: 35052818
The scanf() is fine for that purpose.  You're actually passing the value in second instead of the address of second.  Depending on this value, you might get a memory access violation.  :(

Note that the while loop exits as soon as it reaches EOF in the data file.  The program needs to perform the checks of SeriesLength and PreviousSeriesLength after reaching EOF to pick up the last series.

Three ways to do this.

1.  Copy the test and put it after the while() loop.
2.  Put the test in a function, then call the function from within the while loop and again after it.
3.  Change the while loop to a do/while loop.  Depending on your compiler version/options, this may not be possible.

I've made the change for option 3 and posted it below.

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,
SeriesLength = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0,
PreviousSeriesLength = 0;

FILE *fp;

fp = fopen("c:\\odds.txt", "r");

if(fp != NULL)
{
do
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
Line = strtok(NULL, ",");
overUnder = strtok(NULL, ",");

if(strcmp(Team, prevTeam) == 0)
SeriesLength++;   //team is the same as prev line
else
{
if(SeriesLength == 3)
{
printf("Prev series: %d\tSeries: %d", PreviousSeriesLength, SeriesLength);
if(PreviousSeriesLength == 3)
{
if(*Score == 'W')
winAfterSeries++;
else
lossAfterSeries++;
}
}
strcpy(prevTeam, Team);
PreviousSeriesLength = SeriesLength;
SeriesLength = 1;
}
} while(fgets(str, sizeof(str), fp) != NULL);
}
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: 35053331
In fixes 1 and 2, I'm not really sure what you mean by test.  I only have test as a declared and unused variable.

With #3 I get this error:  Unhandled exception at 0x5f6fd440 (msvcr100d.dll) in OddsLaptopTest.exe: 0xC0000005: Access violation reading location 0x00000000.
0

LVL 35

Assisted Solution

sarabande earned 120 total points
ID: 35055140
you make a do while and get tokens from str where you didn't make a successful fgets before.

you also should test on null pointers after each strtok and before dereference of variables.

Sara
0

LVL 35

Expert Comment

ID: 35055175
so you crash at

``````if(*Score == 'W')
``````

when Score is NULL what is very likely when you enter the loop initially with not-initialised str.

you also crash at

``````strcpy(prevTeam, Team);
``````

when Team is NULL.

Sara

0

LVL 46

Expert Comment

ID: 35056739
Hi Trip,

Sarabande is essentially correct, though there is a bit more work to do....

winAfterSeries and lossAfterSeries can't be incremented until we know that both series are 3 game affairs.  The Won/Lost status of the first game of each series needs to be captured separately from these counters.  Only when the program has confirmed that both series are 3 games can the correct counter be incremented.  (After all, that's why we're making program changes.....)

And Sara's especially correct about testing for NULL.  Some of the C functions do that for you, others do not.  Any place that a pointer is dereferenced must be checked.  A blank line is also a potential issue, but we'll assume that the data file format isn't an issue for now.

Kent

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

int main()
{
char str[1024] = "",
prevTeam[6] = "N/A",
*SourceLine;

char *Date, *Team, *Score, *Line, *overUnder;

int first = 1,
second = 0,
test = 0,
column = 0,
SeriesLength = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0,
FirstGameIsWin = 0,
PreviousSeriesLength = 0;

FILE *fp;

fp = fopen("c:\\odds.txt", "r");

if(fp != NULL)
{
do
{
SourceLine = fgets(str, sizeof(str), fp);
if (SourceLine)
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
Line = strtok(NULL, ",");
overUnder = strtok(NULL, ",");
}
else
Team = "n/a";  // Change "new" team

if (Team && strcmp (Team, prevTeam) == 0)
SeriesLength++;
else
{
if (SeriesLength == 3)
{
printf ("Prev series: %d - %s\tSeries: %d - %s\n",
PreviousSeriesLength, prevTeam, SeriesLength, Team);
if (PreviousSeriesLength == 3)
{
if (FirstGameIsWin)
winAfterSeries++;
else
lossAfterSeries++;
}
}
FirstGameIsWin = (Score && *Score == 'W');
strcpy (prevTeam, Team ? Team : "n/a");
PreviousSeriesLength = SeriesLength;
SeriesLength = 1;
}
} while(SourceLine);
}
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: 35059170
Thanks to both of you.  But it's not counting the last loss with the sample data below.

10/03/2010,@ BOS,L 4-8,L 112,O 9.5 111
10/02/2010,@ BOS,L 6-7,L 102,O 9.5 -101
10/02/2010,@ BOS,W 6-5,W -134,O 9.5 111
09/29/10,@ TOR,L 4-8,L 105,O 9 -116
09/28/10,@ TOR,W 6-1,W -181,U 8.5 -105
09/27/10,@ TOR,L 5-7,L -124,O 9.5 -114
09/26/10,BOS,W 4-3,W -183,U 9.5 -108
09/25/10,BOS,L 3-7,L 109,O 9 -105
09/24/10,BOS,L 8-10,L -175,O 9.5 -105
09/23/10,TB,L 3-10,L -136,O 8 106
09/22/10,TB,L 2-7,L -137,U 10.5 -121
09/21/10,TB,W 8-3,W -133,O 9.5 -105
09/20/10,TB,W 8-6,W -119,O 9.5 -120
09/19/10,@ BAL,L 3-4,L -164,U 9 -111
09/18/10,@ BAL,W 11-3,W -184,O 8 110
09/17/10,@ BAL,W 4-3,W -149,U 9.5 -116
09/15/10,@ TB,L 3-4,L 107,U 9.5 -115
09/14/10,@ TB,W 8-7,W 123,O 8.5 -107
09/13/10,@ TB,L 0-1,L 115,U 7.5 -117
09/12/2010,@ TEX,L 1-4,L 161,U 9.5 -112
09/11/2010,@ TEX,L 6-7,L -103,O 10.5 101
09/10/2010,@ TEX,L 5-6,L 129,O 9.5 -105
09/08/2010,BAL,W 3-2,W -243,U 10 106
09/07/2010,BAL,L 2-6,L -385,U 9 -109
09/06/2010,BAL,L 3-4,L -195,U 9.5 -119
09/05/2010,TOR,L 3-7,L -164,O 9.5 111
09/04/2010,TOR,W 7-5,W -191,O 10 -102
09/03/2010,TOR,W 7-3,W -135,O 9 -109
09/02/2010,OAK,W 5-0,W -208,U 8 -114
09/01/2010,OAK,W 4-3,W -133,U 9.5 -119
08/31/10,OAK,W 9-3,W -203,O 9 -105
08/30/10,OAK,W 11-5,W -125,O 9.5 104
08/29/10,@ CHW,W 2-1,W 116,U 9.5 106
08/28/10,@ CHW,W 12-9,W -134,O 8.5 -104
08/27/10,@ CHW,L 4-9,L -137,O 10 -110
08/25/10,@ TOR,L 3-6,L -133,P 9 -
08/24/10,@ TOR,W 11-5,W -104,O 10 -101
08/23/10,@ TOR,L 2-3,L 120,U 9 -118
08/22/10,SEA,W 10-0,W -315,O 8.5 -108
08/21/10,SEA,W 9-5,W -203,O 9 -118
08/20/10,SEA,L 0-6,L -145,U 8 105
08/19/10,DET,W 11-5,W -235,O 9.5 -107
08/18/10,DET,W 9-5,W -173,O 9.5 -101
08/17/10,DET,W 6-2,W -177,O 7.5 -101
08/16/10,DET,L 1-3,L -174,U 9 109
08/15/10,@ KC,L 0-1,L -210,U 10 -115
08/14/10,@ KC,W 8-3,W -236,O 9.5 -109
08/13/10,@ KC,L 3-4,L -182,U 10 107
08/12/2010,@ KC,W 4-3,W -240,U 9 -105
0

Author Comment

ID: 35059278
Also, I probably should have asked this earlier, but I'll increase my points accordingly.  ;)
If I only want to pay attention to my result IF (and only if) the first 3 game series was a sweep.

Could I just tweak this code? or would that require major reconstruction of code?
I'm thinking just an adjustment of the condition on line 55, right?
0

LVL 46

Expert Comment

ID: 35059370
A small adjustment to handle the case of a sweep.  Actually, only a small adjustment could let you handle all of these cases simultaneously.  :)

The last loss "that counts" is 8/25 @Toronto.

Kent
0

Author Comment

ID: 35059412
I'm sorry - I mean to say that it's counting one too many losses.
0

LVL 46

Expert Comment

ID: 35060155
Hi Trip.

I don't see it.....

I changed the code slightly so that messages could be better displayed.  It's posted below.

Note last column (wins, losses).  It changes on the first game of a series and only when both the series just checked and the previous series are 3 gamers.

It looks right to me, or am I missing something?

Kent

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

int main()
{
char str[1024] = "",
prevTeam[6] = "N/A",
currTeam[6] = "N/A",
*SourceLine;

char *Date, *Team, *Score, *Line, *overUnder;

int first = 1,
second = 0,
test = 0,
column = 0,
SeriesLength = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0,
FirstGameIsWin = 0,
PreviousSeriesLength = 0;

FILE *fp;

fp = fopen("c:\\odds.txt", "r");

if(fp != NULL)
{
do
{
SourceLine = fgets(str, sizeof(str), fp);
if (SourceLine)
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
Line = strtok(NULL, ",");
overUnder = strtok(NULL, ",");
}
else
Team = "n/a";  // Change "new" team

if (Team && strcmp (Team, currTeam) == 0)
SeriesLength++;
else
{
if (SeriesLength == 3)
{
if (PreviousSeriesLength == 3)
{
if (FirstGameIsWin)
winAfterSeries++;
else
lossAfterSeries++;
}
}
FirstGameIsWin = (Score && *Score == 'W');
strcpy (prevTeam, currTeam ? currTeam : "n/a");
PreviousSeriesLength = SeriesLength;
SeriesLength = 1;
if (Team)
strcpy (currTeam, Team);
}
printf ("%c %s\tPrev: %d - %s\tCurr: %d - %s\t%d,%d\n",
Score ? *Score : '-', Team,
PreviousSeriesLength, prevTeam, SeriesLength, currTeam,
winAfterSeries, lossAfterSeries);
} while(SourceLine);
}
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: 35060364
Hmm.  It looks like it is right.  I'm sorry about that.  Perhaps my eyes are getting impatient.  :)

Ok - Just one more issue.  Any ideas on how to make it calculate only when the first series is a sweep?
(And by first series - I mean the first series in every comparison)

i.e. - I only care about the result of the 1st game in the 2nd series....when the 1st series is a sweep.
0

LVL 46

Expert Comment

ID: 35060412
Yeah.  Just a couple of lines of code should do.

There's enough "stuff" going on that it may be time to consider changing the program to keep all of the rows in a table and just scan the table.

Kent
0

Author Comment

ID: 35060527
An array of char pointers then?
0

LVL 46

Accepted Solution

Kent Olsen earned 1480 total points
ID: 35062054
There were no sweeps (3 games all wins) in the data, but here's the code anyway.  Dummy up some test data and try it.

We can build this into tables later.  :)

Kent

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

int main()
{
char str[1024] = "",
prevTeam[6] = "N/A",
currTeam[6] = "N/A",
*SourceLine;

char *Date, *Team, *Score, *Line, *overUnder;

int first = 1,
second = 0,
test = 0,
column = 0,
SeriesLength = 0,
wins = 0,
losses = 0,
winAfterSeries = 0,
lossAfterSeries = 0,
FirstGameIsWin = 0,
Sweep = 0,
PreviousSweep = 0,
PreviousSeriesLength = 0;

FILE *fp;

fp = fopen("c:\\odds.txt", "r");

if(fp != NULL)
{
do
{
SourceLine = fgets(str, sizeof(str), fp);
if (SourceLine)
{
Date = strtok(str, ",");
Team = strtok(NULL, ",");
Score = strtok(NULL, ",");
Line = strtok(NULL, ",");
overUnder = strtok(NULL, ",");
}
else
Team = "n/a";  // Change "new" team

if (Team && strcmp (Team, currTeam) == 0)
{
SeriesLength++;
Sweep += (Score && *Score == 'W' ? 1 : -1);
}
else
{
if (SeriesLength == 3)
{
if (PreviousSeriesLength == 3 && PreviousSeriesLength == PreviousSweep)
{
if (FirstGameIsWin)
winAfterSeries++;
else
lossAfterSeries++;
}
}
FirstGameIsWin = (Score && *Score == 'W');
PreviousSweep = Sweep;
Sweep = FirstGameIsWin ? 1 : -1;
strcpy (prevTeam, currTeam ? currTeam : "n/a");
PreviousSeriesLength = SeriesLength;
SeriesLength = 1;
if (Team)
strcpy (currTeam, Team);
}
printf ("%c %s\tPrev: %d - %s\tCurr: %d - %s\t%d,%d\n",
Score ? *Score : '-', Team,
PreviousSeriesLength, prevTeam, SeriesLength, currTeam,
winAfterSeries, lossAfterSeries);
} while(SourceLine);
}
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: 35062166
I'm still getting zeros.  In lines 48 and 65 (where you're using the AND operator)
are you sure that the return value is going to be the same as their comparatives?
0

LVL 46

Expert Comment

ID: 35063162

Testing any variable (without a condition or comparator) tests the value for zero/non-zero.

if (intvar)
// value is non-zero
else
// value is zero

if (pointervar)
//  value is non-NULL
else
//  value is NULL

I got all zero on the last data that you posted, but the trace suggested that it should work fine when there is a sweep in a 3 game series.

Kent
0

Author Comment

ID: 35066164
Yeah, I know about non-conditional evaluations, I was just checking whether those particular lines were working properly.

I changed some of the series to sweeps, but still nothing.

10/03/2010,@ BOS,W 4-8,L 112,O 9.5 111
10/02/2010,@ BOS,W 6-7,L 102,O 9.5 -101
10/02/2010,@ BOS,W 6-5,W -134,O 9.5 111
09/29/10,@ TOR,W 4-8,L 105,O 9 -116
09/28/10,@ TOR,W 6-1,W -181,U 8.5 -105
09/27/10,@ TOR,W 5-7,L -124,O 9.5 -114
09/26/10,BOS,W 4-3,W -183,U 9.5 -108
09/25/10,BOS,L 3-7,L 109,O 9 -105
09/24/10,BOS,L 8-10,L -175,O 9.5 -105
09/23/10,TB,L 3-10,L -136,O 8 106
09/22/10,TB,L 2-7,L -137,U 10.5 -121
09/21/10,TB,W 8-3,W -133,O 9.5 -105
09/20/10,TB,W 8-6,W -119,O 9.5 -120
09/19/10,@ BAL,W 3-4,L -164,U 9 -111
09/18/10,@ BAL,W 11-3,W -184,O 8 110
09/17/10,@ BAL,W 4-3,W -149,U 9.5 -116
09/15/10,@ TB,L 3-4,L 107,U 9.5 -115
09/14/10,@ TB,W 8-7,W 123,O 8.5 -107
09/13/10,@ TB,L 0-1,L 115,U 7.5 -117
09/12/2010,@ TEX,W 1-4,L 161,U 9.5 -112
09/11/2010,@ TEX,L 6-7,L -103,O 10.5 101
09/10/2010,@ TEX,L 5-6,L 129,O 9.5 -105
09/08/2010,BAL,W 3-2,W -243,U 10 106
09/07/2010,BAL,L 2-6,L -385,U 9 -109
09/06/2010,BAL,L 3-4,L -195,U 9.5 -119
09/05/2010,TOR,L 3-7,L -164,O 9.5 111
09/04/2010,TOR,W 7-5,W -191,O 10 -102
09/03/2010,TOR,W 7-3,W -135,O 9 -109
09/02/2010,OAK,W 5-0,W -208,U 8 -114
09/01/2010,OAK,W 4-3,W -133,U 9.5 -119
08/31/10,OAK,W 9-3,W -203,O 9 -105
08/30/10,OAK,W 11-5,W -125,O 9.5 104
08/29/10,@ CHW,W 2-1,W 116,U 9.5 106
08/28/10,@ CHW,W 12-9,W -134,O 8.5 -104
08/27/10,@ CHW,L 4-9,L -137,O 10 -110
08/25/10,@ TOR,L 3-6,L -133,P 9 -
08/24/10,@ TOR,W 11-5,W -104,O 10 -101
08/23/10,@ TOR,L 2-3,L 120,U 9 -118
08/22/10,SEA,W 10-0,W -315,O 8.5 -108
08/21/10,SEA,W 9-5,W -203,O 9 -118
08/20/10,SEA,W 0-6,L -145,U 8 105
08/19/10,DET,W 11-5,W -235,O 9.5 -107
08/18/10,DET,W 9-5,W -173,O 9.5 -101
08/17/10,DET,W 6-2,W -177,O 7.5 -101
08/16/10,DET,L 1-3,L -174,U 9 109
08/15/10,@ KC,L 0-1,L -210,U 10 -115
08/14/10,@ KC,W 8-3,W -236,O 9.5 -109
08/13/10,@ KC,L 3-4,L -182,U 10 107
08/12/2010,@ KC,W 4-3,W -240,U 9 -105
0

LVL 46

Expert Comment

ID: 35069158
Hi Trip,

I saved your data file and ran the program against it.  It shows 2 wins and 1 loss after sweeps.

I haven't change the program since last posting it.  Are you sure that you saved the data file to the correct place?

Kent
0

LVL 46

Assisted Solution

Kent Olsen earned 1480 total points
ID: 35069493
Hi Trip,

Remember the suggestion to load the games into an array (tables)?

Here's the program to do that.  Now it's a lot easier to compute the things that you want, and the things that you're likely to add.

Kent

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

#define  MAX_GAME 170
#define  MAX_SERIES 170

typedef struct
{
char  *Source,
*Date,
*Opponent,
*Score,
*Line,
*OverUnder;
int   Home,
Win;
} game_t;

typedef struct
{
char  *Opponent;
int   GameStart,
GameEnd,
FirstWin,
Wins,
Losses;
} series_t;

game_t   GameList[MAX_GAME];
series_t SeriesList[MAX_SERIES];

int      GameCount;
int      SeriesCount;
//----------------------------------------------------------------------------
char str[1024] = "",
prevTeam[6] = "N/A",
currTeam[6] = "N/A",
*SourceLine;

char *Date, *Team, *Score, *Line, *overUnder;

int second = 0,
SeriesLength = 0,
winAfterSeries = 0,
lossAfterSeries = 0,
FirstGameIsWin = 0,
Sweep = 0,
PreviousSweep = 0,
PreviousSeriesLength = 0;
//----------------------------------------------------------------------------

int main()
{
int idx;
FILE *fp;
game_t *G;
series_t *S;

//  Load all of the games from the data file

fp = fopen("c:\\odds.txt", "r");
if (fp != NULL)
{
while (fgets (str, sizeof (str), fp))
{
G = &GameList[GameCount];
G->Source = strdup (str);
G->Date = strtok (GameList[GameCount].Source, ",\n");
G->Opponent = strtok (NULL, ",\n");
G->Score = strtok (NULL, ",\n");
G->Line = strtok (NULL, ",\n");
G->OverUnder = strtok (NULL, ",\n");
G->Home = G->Opponent[0] != '@';
G->Win = G->Score[0] == 'W';
GameCount++;
}
fclose (fp);
}

//  Compute series information

idx = 0;
while (idx < GameCount)
{
G = &GameList[idx];
S = &SeriesList[SeriesCount];
S->GameStart = idx;
S->GameEnd = idx;
S->FirstWin = G->Win;
S->Opponent = G->Opponent;
while (idx < GameCount && strcmp (G->Opponent, GameList[idx].Opponent) == 0)
{
G = &GameList[idx];
if (G->Win)
S->Wins++;
else
S->Losses++;
S->GameEnd = idx;
idx++;
}
printf ("Series:  %s\t First Win: %c\tWins = %d\t Losses = %d\n",
S->Opponent, S->FirstWin ? 'Y' : 'n', S->Wins, S->Losses);
SeriesCount++;
}

//  Compute Statistics

//  printf(" First game of series won %d\n", winAfterSeries);
//  printf(" First game of series lost %d\n", lossAfterSeries);

scanf("Waiting %d", &second);

return 0;
}
``````
0

Author Comment

ID: 35084314
The newer version however, still gives me zeroes.
0

LVL 46

Assisted Solution

Kent Olsen earned 1480 total points
ID: 35085538
Hi Trip,

That last code doesn't do any computations.  It simply builds the Game and Series records (which do contain quite a bit of useful values!)

After line 107, insert this:

for (idx = 1; idx < SeriesCount; SeriesCount++)
if (SeriesList[idx].Wins + SeriesList[idx].Losses == 3
&& SeriesList[idx-1].Wins == 3
&& SeriesList[idx-1].Losses = 0
&& SeriesList[idx].FirstWin != 0)
printf ('Game 1 win after series sweep:  %s -> %s\n",
SeriesList[idx-1].Opponent, SeriesList[idx].Opponent);
0

Author Comment

ID: 35087743
Oh duh!  How could I miss that??? lol
I was so concerned with checking the changes, I forgot about functionality!

Anyways, I don't think SeriesList[..] is a modifiable value.  Wasn't it defined as const?
0

LVL 46

Expert Comment

ID: 35087802

MAX_SERIES is a constant.  That's that maximum number of series that the program will track.  That could always be made dynamic, but that adds complexity to the program that I'll leave to you.  :)

SeriesList is just a list of pointers to series_t structures.

Kent
0

Author Comment

ID: 35097788
Thanks for all your help Kent, you're a life saver!
0

## Featured Post

Question has a verified solution.

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

Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which hasÂ 42949672960Â pages. We also have a table of contents.Â Now we want to read the content on pâ€¦
Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory. Â  You should implement aâ€¦
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201â€¦
###### Suggested Courses
Course of the Month7 days, 13 hours left to enroll