In: Computer Science
1. Define a struct for a soccer player that stores their name, jersey number, and total points scored.
2. Using the struct in #1, write a function that takes an array of soccer players and its size as arguments and returns the average number of points scored by the players.
3. Using the struct in #1, write a function that takes an array of soccer players and its size as arguments and returns the index of the player who scored the most points.
4. Using the struct in #1, write a function that sorts an array of soccer players by name.
5. Using the struct in #1, write a function that takes an (unsorted) array of soccer players and its size and a number as arguments, and returns the name of the soccer player with that number. It should not do any extra unnecessary work.
in c++ please
#include<iostream>
#include<string>
using namespace std;
//struct for a soccer player that stores their name, jersey
number, and total points scored
typedef struct player {
string name;
int jNo;
int points;
} Player;
// function that takes an array of soccer players and its size
as arguments and returns the average number of points scored by the
players.
double averagePts(Player players[], int n) {
double sum = 0.0;
//adding points of each player
for(int i = 0 ; i < n ; i ++) {
sum += players[i].points;
}
return sum/n;
}
//function that takes an array of soccer players and its size as
arguments and returns the index of the player who scored the most
points.
int maximumScore(Player players[], int n) {
int maxPoints = players[0].points;
int index = 0;
for(int i = 1 ; i < n ; i ++) {
//checking if previous max is less than the current
element
if(maxPoints <
players[i].points) {
maxPoints =
players[i].points;
index = i;
}
}
return index;
}
//function that sorts an array of soccer players by name.
void sort(Player players[], int n) {
//using bubble sort to sort the array of players by
name
for(int i=1; i<n; i++)
{
for(int j=1; j<n; j++)
{
//checking
condition on the name
if(players[j-1].name > players[j].name)
{
Player temp = players[j-1];
players[j-1] = players[j];
players[j] = temp;
}
}
}
}
//function that takes an (unsorted) array of soccer players and
its size and a number as arguments, and returns the name of the
soccer player with that number
string nameOfPlayer(Player players[], int n, int jNo) {
for(int i = 0 ; i < n ; i ++) {
if(players[i].jNo == jNo){
return
players[i].name;
}
}
return "Player Not Found!!";
}
int main() {
int n = 4;
Player players[n];
//initialising the array
players[0] = {"Shashank", 10, 50};
players[1] = {"Surbhi", 11, 50};
players[2] = {"Shivangi", 55, 54};
players[3] = {"Mohit", 5, 50};
cout << "Average points: " <<
averagePts(players, n) << endl << endl;
cout << "Maximum points: " <<
players[maximumScore(players, n)].name << endl <<
endl;
cout << "Player with Jersey number 50: "
<< nameOfPlayer(players, n, 50) << endl <<
endl;
sort(players, n);
cout << "Priting Sorted Players by name: "
<< endl;
for(int i = 0 ; i < n ; i ++) {
cout << players[i].name
<< " " << players[i].points << endl;
}
return 0;
}