Home > Cpp > C++ Program to maintain a Cricket Team by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q9

C++ Program to maintain a Cricket Team by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q9


Q9. Program to maintain a Cricket Team:

Make a Class Cricket_Team with following attributes:
– Team Name
– Player Name
– Batting Average
Read the details various players from different team in random.
Now print the team wise list of players in the sorted order of their batting averages.

Use functions:
– To read the player detail
– To arrange the players team wise
– To sort the batting average
– To Print the details

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>

class CCricket{
	private:
		char country[20], player[20];
		int avg;
	public:
		void getTeams();
		void compTeams(CCricket *, const int);
		void dispTeams(const CCricket *, const int);
	};

void CCricket :: getTeams(){
	cout<<"\n Enter the Name of a Country: ";
	cin>>country;
	cout<<"\n Enter a Player Name: ";
	cin>>player;
	cout<<"\n Enter the Batting Average: ";
	cin>>avg;
	}

void CCricket :: compTeams(CCricket *Ock, const int t_pls){
	int i, j;
	CCricket Otemp;
	// Sorting By Players Name.
	for(i=0; i<=t_pls; i++){
		for(j=i+1; j<=t_pls; j++){
			if(Ock[i].avg < Ock[j].avg){
				Otemp = Ock[i];
				Ock[i] = Ock[j];
				Ock[j] = Otemp;
				}
			}
		}
	// Sorting By Country Name.
	for(i=0; i<=t_pls; i++){
		for(j=i+1; j<=t_pls; j++){
			if(strcmp(Ock[i].country, Ock[j].country) > 0){
				Otemp = Ock[i];
				Ock[i] = Ock[j];
				Ock[j] = Otemp;
				}
			}
		}
	}

void CCricket :: dispTeams(const CCricket *Ock, const int t_pls){
	int i, j;
	char t_c_name[10];
	// Display Players.
	cout<<"\n\n Players Sorted According to their Country and 
  Average:- \n";
	cout<<"\n COUNTRY \t TEAM \t AVERAGE"<<endl;
	for(i=1; i<=t_pls; i++){
		if(strcmp(t_c_name, Ock[i].country) != 0){
			cout<<"\n "<<Ock[i].country;
			strcpy(t_c_name, Ock[i].country);
			}
		cout<<"\n\t\t"<<Ock[i].player<<"  -  "
    <<setw(5)<<Ock[i].avg<<endl;
		}
	}

void main(){
	int i=0;
	char ch;
	CCricket Ock[30], Otemp;

	while(1){
		clrscr();
		Ock[i].getTeams();
		i++;
		cout<<"\n Do you want to Enter next Entry (y/n) ? : ";
		cin>>ch;
		if(ch == 'n')
			break;
		} // End of while Loop.

	cout<<"\n\n Total Players Entered: "<<i<<endl;

	// Sort Teams.
	Otemp.compTeams(Ock, i);

	// Display Teams.
	Otemp.dispTeams(Ock, i);

	getch();
	}

 

Output:

Enter the Name of a Country: India

Enter a Player Name: Sachin

Enter the Batting Average: 70

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Australia

Enter a Player Name: Ponting

Enter the Batting Average: 50

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Bang

Enter a Player Name: Zahid

Enter the Batting Average: 30

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: India

Enter a Player Name: Rahul

Enter the Batting Average: 45

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Australia

Enter a Player Name: Gilcrist

Enter the Batting Average: 75

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: India

Enter a Player Name: Harbhjan

Enter the Batting Average: 50

Do you want to Enter next Entry (y/n) ? : n

Total Players Entered: 6

Players Sorted According to their Country and Average:-

COUNTRY TEAM

Australia
Gilcrist – 75

Ponting – 50

Bang
Zahid – 30

India
Harbhjan – 50

Rahul – 45

Sachin – 70


Advertisement
Categories: Cpp Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: