I'm trying to put what i've learned into a little program that calculates batting average.
I want to store the batting averages of the players in an array and printf the best average at the end by name for example Dennis Ritchie had best batting average at .600.
I'm having trouble understanding how and what type of array to use.
Looking for a little guidance, also if I could have done something smarter or easier please let me know, much appreciated.
Code:
int main(int argc, const char * argv[])
{
char playerName[20];
int totalPlayers, atBats, totalHits, x;
float battingAvg;
// Get total number of batters
printf("How many batters?\n");
scanf(" %i", &totalPlayers);
// Check averages of all batters
for ( x = 0; x < totalPlayers; x++ )
{
printf("Players name?\n");
scanf(" %s", *&playerName);
// Total at bats
printf("at bats?\n");
scanf(" %i", &atBats);
// Total hits
printf("hits?\n");
scanf(" %i", &totalHits);
// Hits divided by at bats = bAvg
battingAvg = totalHits / (float)atBats;
printf ("%s batting average is %.3f\n\n", playerName, battingAvg);
}
return 0;
}