Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 7 (16 points): Assume the pey player 6 points): complete the code struct

ID: 3751741 • Letter: P

Question

Problem 7 (16 points): Assume the pey player 6 points): complete the code struct playert typedee player struct is defined as follows: as char name: int score: player Complete the function calc aqv score to each player's name to stdout, each on its own line . returns th e average of all scores for all player structs in arr For r example, if the following code is executed: Mickey", 20), Pluto, 613 erstiGooty",nnie", 400 double average score tind avg score (players, Then, the following is printed: Goofy Minnie Mickey Pluto and average_score would be assigned to 126.5 // arr is an array of player structs // len is the length of arr double calc_avg_score (player arr, int len) t if (len

Explanation / Answer

Structure in C is a collection a one or more variables. Variables can be of different data types. We can access the variables in structure data type using dot (.) operator

Program for the above question is as follows

//########################## PGM START ####################################

#include<stdio.h>

typedef struct player{
   char *name;
   int score;
}player;
double calc_avg_score(player *arr,int len){
    int i;
    if(len<=0){
        return -1;
   }
   double average=0;  
  
  
//Iterate through the array of structure
   // Each struture variable is accessed through dot operator .
   // Add the score of each player to average and finally divide it by number of players
   for(i=0;i<len;i++){
       printf("%s ",arr[i].name);
       average=average+arr[i].score;
   }
   return average/len;
}
int main(){
    player players[]={{"Goofy",80},{"Minnie",400},{"Mickey",20}};
    double average_score=calc_avg_score(players,3);
    printf("Average_score=%lf ",average_score);
    return 0;
}

//############################## PGM END #######################################

OUTPUT
#######

Goofy
Minnie
Mickey
Average_score=166.666667