the following programs reads a data set like the following Arribas 10 6 2 1 4 0
ID: 3631648 • Letter: T
Question
the following programs reads a data set like the followingArribas 10 6 2 1 4 0 6 3 1 8 7 5
Sanchez 7 4 9 10 2 0 10 8 5 0 4 6
Rooney 10 3 10 10 7 10 3 7 9 7 8 2 10 7 10 4 1 0
each set is a match. it averages the score of each player, and assigns a ranking for them.
Arribas played 2 matches ****
Sanchez played 2 matches *****
Rooney played 3 matches *******
modify the program so that is averages the third score of each line and prints the player with the highest third score.'
#include <stdio.h>
//main function
int main()
{
//declares variables
int i, j, num, sum, sets;
int count=0;
double avg;
char item[10];
char *tokens;
//declares file
FILE *Filein;
//opens the file
Filein = fopen("/ece/www/ism2008/hw5/statsHW5.dat", "r");
do
{
//check for letters in character string
if((isalpha(item[0])!=0))
{
if(count > 0)
{
//calculates the average of the scores;
avg = (double)sum / (double)(sets*6);
//Display the average value
printf(" played %d matches ", sets);
// adds a star for each ranking
if ( avg > .5)
{printf("*");}
if (avg > 1.5)
{printf("*");}
if (avg > 2.5)
{printf("*");}
if ( avg > 3.5)
{printf("*");}
if (avg > 4.5)
{printf("*");}
if (avg > 5.5)
{printf("*");}
if ( avg > 6.5)
{printf("*");}
if (avg > 7.5)
{printf("*");}
if (avg > 8.5)
{printf("*");}
if (avg > 9.5)
{printf("*");}
}
//starts off the variables at 0
i = j = num = sum = sets = 0;
avg = 0.0;
//displays the name of the player
printf(" %s ", item);
//increment the counter
count++;
}
else
{
//converts the character string to a number variable
num = atoi(item);
sum = sum + num;
// recognizes the numbers in each set and creates counter for sets
j++;
if(j==6)
{
sets++;
j=0;
}
}
}while ( fscanf(Filein, " %s", item) != EOF );
//calculates the average
avg = (double)sum / (double)(sets*6);
//Display the output for the final score
printf(" played %d matches ", sets);
//adds star for each rank
if ( avg > .5 )
{printf("*");}
if (avg > 1.5)
{printf("*");}
if (avg > 2.5)
{printf("*");}
if ( avg > 3.5 )
{printf("*");}
if (avg > 4.5)
{printf("*");}
if (avg > 5.5)
{printf("*");}
if ( avg > 6.5 )
{printf("*");}
if (avg > 7.5)
{printf("*");}
if (avg > 8.5)
{printf("*");}
if (avg > 9.5)
{printf("*");}
printf(" ");
//close the file
fclose(Filein);
return 0;
}
Explanation / Answer
#include #include #include //main function int main() { //declares variables int i, j, num, sum, sets; int count=0; double avg; char item[10]; char *tokens; //declares file FILE *Filein; //opens the file Filein = fopen("example.txt", "r"); do { //check for letters in character string if((isalpha(item[0])!=0)) { if(count > 0) { //calculates the average of the scores; avg = (double)sum / (double)(sets*6); //Display the average value printf(" played %d matches ", sets); // adds a star for each ranking if ( avg > .5) {printf("*");} if (avg > 1.5) {printf("*");} if (avg > 2.5) {printf("*");} if ( avg > 3.5) {printf("*");} if (avg > 4.5) {printf("*");} if (avg > 5.5) {printf("*");} if ( avg > 6.5) {printf("*");} if (avg > 7.5) {printf("*");} if (avg > 8.5) {printf("*");} if (avg > 9.5) {printf("*");} } //starts off the variables at 0 i = j = num = sum = sets = 0; avg = 0.0; //displays the name of the player printf(" %s ", item); //increment the counter count++; } else { //converts the character string to a number variable num = atoi(item); sum = sum + num; // recognizes the numbers in each set and creates counter for sets j++; if(j==6) { sets++; j=0; } } }while ( fscanf(Filein, " %s", item) != EOF ); //calculates the average avg = (double)sum / (double)(sets*6); //Display the output for the final score printf(" played %d matches ", sets); //adds star for each rank if ( avg > .5 ) {printf("*");} if (avg > 1.5) {printf("*");} if (avg > 2.5) {printf("*");} if ( avg > 3.5 ) {printf("*");} if (avg > 4.5) {printf("*");} if (avg > 5.5) {printf("*");} if ( avg > 6.5 ) {printf("*");} if (avg > 7.5) {printf("*");} if (avg > 8.5) {printf("*");} if (avg > 9.5) {printf("*");} printf(" "); //close the file fclose(Filein); return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.