# Baseball algorithm

Page 1 of 1

## 4 Replies - 2151 Views - Last Post: 13 May 2009 - 08:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=104589&amp;s=e2b97924c9873fc6a342f27cc6607a18&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 alexKnight

Reputation: 0
• Posts: 11
• Joined: 11-May 09

# Baseball algorithm

Posted 11 May 2009 - 12:37 PM

Hey wassup guys I found this program online on some teachers website and I thought I would try it out for some practice. The program basically reads in a file with 9 innings showing the hits per inning. Then my job is to take that information and tally up the runs with the given hits per inning. I started working on it yesterday and I found myself stuck on an algorithm for tallying up the runs per inning for a team. I thought an algorithm for this would be to divide the number of hits by 4, which is an int and thats the runs scored. But thats not correct due to the fact that when an homerun is hit everyone on base including the hitter accounts for a run. I would appreciate it if someone can give me a hit on how to go about calculating the amount of runs per inning.

thanks!!

Is This A Good Question/Topic? 0

## Replies To: Baseball algorithm

### #2 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: Baseball algorithm

Posted 11 May 2009 - 12:41 PM

Do you think you could post what you have coded so far? Are you creating a class for this project?

### #3 alexKnight

Reputation: 0
• Posts: 11
• Joined: 11-May 09

## Re: Baseball algorithm

Posted 11 May 2009 - 12:49 PM

Thanks for the response!
But no apw5020 its not for a class project, its just summer and i want to get some practice writting code so I get alittle better.
But heres my code..
[code]
int readVistorAndTally( FILE *ptrToFile, char inning[][MAX_LENGTH]){
int i;
int runningHits= 0;
int same = 0;

for(i = 0;; i++){
fscanf( ptrToFile, "%s", &inning[i]);
if(strcmp(END_OF_VISTOR, inning[i]) == same){
printf("\n");
break;
}
if(strcmp(SINGLE_HIT, inning[i]) == same){
runningHits += 1;
}
else if(strcmp(DOUBLE_HIT, inning[i]) == same){
runningHits += 2;
}
else if(strcmp(TRIPLE_HIT, inning[i]) == same){
runningHits += 3;
}
else if(strcmp(HOMERUN, inning[i]) == same){
runningHits += 4;
}
else {
runningHits += 0;
}
}

return runningHits;
}

All this is is basically a function which job is to read the file (each inning) than return runs scored..but i cant seem to get a correct algorthim..thanks again guys!

### #4 apw5020

Reputation: 78
• Posts: 666
• Joined: 26-March 09

## Re: Baseball algorithm

Posted 11 May 2009 - 01:06 PM

Sorry, I meant if you are creating a class in C++...

```class someClass
{
public:

private:
};

```

Now I see you are using functions. I will take a look over it and get back to you soon.

By the way, you forgot the closing BBCode block after your code.

This post has been edited by apw5020: 11 May 2009 - 01:06 PM

### #5 alexKnight

Reputation: 0
• Posts: 11
• Joined: 11-May 09

## Re: Baseball algorithm

Posted 13 May 2009 - 08:44 AM

Yea im still stuck thinking about a scoring algorthim for the scoring in baseball!!
It's not just about adding all of the hits because the chronological order is critical.
It's been bothering me for quite some time.
I mean i know i can put an algorithm which specifies every single scenario but that doesn't seem every efficient.
Can somebody please give me a hint!
Thanks