Question

In: Computer Science

1. Define a struct for a soccer player that stores their name, jersey number, and total...

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

Solutions

Expert Solution

#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;
}


Related Solutions

Python Please Define a class that will represent soccer players as objects. A soccer player will...
Python Please Define a class that will represent soccer players as objects. A soccer player will have as attributes, name, age, gender, team name, play position on the field, total career goals scored. The class should have the following methods: 1. initializer method that will values of data attributes arguments. Use 0 as default for career goals scored. 2. str method to return all data attributes as combined string object. 3. addToGoals that will accept an argument of int and...
1.A soccer player kicks a soccer ball of mass 0.45 kg that is initially at rest....
1.A soccer player kicks a soccer ball of mass 0.45 kg that is initially at rest. The player's foot is in contact with the ball for 1.70 × 10-3 s, and the force of the kick is given by F(t) = [(6.56 × 105)t - (3.86 × 108)t2] N for 0≤t≤1.70×10-3s, where t is in seconds. Find the magnitudes of the following: (a) the impulse on the ball due to the kick, (b) the average force on the ball from...
Activity 1: How far can a soccer player kick a soccer ball down field? Through the...
Activity 1: How far can a soccer player kick a soccer ball down field? Through the application of a linear function and a quadratic function and ignoring wind and air resistance one can describe the path of a soccer ball. These functions depend on two elements that are within the control of the player: velocity of the kick (v k ) and angle of the kick (?). A skilled high school soccer player can kick a soccer ball at speeds...
1. An SRJC student: "I saw a woman in the gym wearing an SRJC soccer jersey....
1. An SRJC student: "I saw a woman in the gym wearing an SRJC soccer jersey. Thus I can assume she's on the SRJC soccer team". a) questionable analogy b) hasty conclusion c) equivocation d) fallacious appeal to authority 2. Welfare programs for the poor have destroyed the lives of millions of children. This is a system of child abuse. People on welfare are like thieves who pick your pockets. a) questionable analogy b) small sample c) invincible ignorance d)...
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name...
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name (size 50) and the salary (double) of an employee. The student_record struct will have the name (size 30), Id number (int) of a student, and the grade (int). Include these two structs in a union called 'person'. Using file redirection called input.txt, at first, ask the user for the employee's information and print them from the main() and the size of the union. Then...
1) Define a C struct that can be used to represent an ingredient in a recipe....
1) Define a C struct that can be used to represent an ingredient in a recipe. You must include the ingredient, the amount to use, and the unit of measurement. When allocated, the struct must be self-contained; do not rely on information being stored anywhere else in memory. 2) Define a C function that will print an array of ingredients to the standard output stream, one per line. You must use the struct definition from the first part. 3) Define...
Define a struct pet with properties name, age, weight, and type. Use appropriate data types for the different properties.
use C source code to complete the following:Define a struct pet with properties name, age, weight, and type. Use appropriate data types for the different properties. You may assume that the strings will never be longer than 19 chars. Don’t forget to add an extra char for the NULL character that terminates the strings.
1. A soccer player will kick a ball 80 times during practice. Assume that the kicks...
1. A soccer player will kick a ball 80 times during practice. Assume that the kicks are independent of each other, and the probability that he scores is 0.6 (60% chance that the ball goes into the goalpost and 40% chance that the ball does not go into the goalpost). Let X be the number of successful goals (number of scores) out of the 80 kicks. (a) What is the distribution of X? (b) Write the pmf f(x) and name...
We plan to develop customer’s database that stores customer’s number (ID), first name, last name and...
We plan to develop customer’s database that stores customer’s number (ID), first name, last name and balance. The program will support three operations: (a) reading and loading customer’s info from the text file, (b) entering new customer’s info, (c) looking up existing customer’s info using customer’s number(ID), (d) deleting the customer’s info and (e) the updated database will be stored in the text file after the program terminate. Customer’s database is an example of a menu-driven program. When the program...
Write a program in C language 1- Define a struct for students with the aforementioned attributes,...
Write a program in C language 1- Define a struct for students with the aforementioned attributes, test it by populating one initialized struct variable with arbitrary input and take screenshots of the output. 2- For each student, struct add an array of number grades for a class the students are enrolled in such as S E 185. Then write functions which find the max, average, and minimum score for a specified assignment identified by a number, for example, Assignment 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT