0% found this document useful (0 votes)
25 views5 pages

Assigement No 1 .CPP

Uploaded by

alish6403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Assigement No 1 .CPP

Uploaded by

alish6403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assigement No:1

Aleesha Kanwal
ID:BC230419288
Question:1
Q:1 You are required to write a C++ program to find the
performance of cricket players after a match. The detailed
requirements of assignment task are given below.
Solution:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

class Player {
private:
string playerName;
int totalScore;
int ballsPlayed;

public:
Player() {
totalScore = 0;
ballsPlayed = 0;
}

void setName(string name) {


playerName = name;
}
string getName() {
return playerName;
}

void setScore(int score) {


totalScore += score;
}

int getScore() {
return totalScore;
}

void setBalls(int balls) {


ballsPlayed = balls;
}

int getBalls() {
return ballsPlayed;
}

// Function to simulate a player's turn


void playTurn() {
srand(time(0)); // Seed the random number generator

while (true) {
int score = -1; // Initialize score to -1 (indicating OUT)

// Generate a score between -1 to 6 (except 5)


while (score == -1 || score == 5) {
score = rand() % 7 - 1;
}
ballsPlayed++; // Increment balls played

if (score == -1) {
cout << "OUT! " << playerName << " is out." << endl;
break;
} else {
totalScore += score;
cout << "Scored " << score << " runs." << endl;
}
}
}
};

int main() {
// Array of player names
string playerNames[11] = {"babar azam", "naseem shah",
"agha salman", "muhammad amir",
"shaheen afridi", "ahadab khan", "imad
wasim", "fakhar zaman",
"haris rauf", "muhammad rizwan", "iftikhar
ahmed"};

// Array of Player objects


Player players[11];

// Assign names to each player


for (int i = 0; i < 11; i++) {
players[i].setName(playerNames[i]);
}

// Simulate match and calculate scores for each player


for (int i = 0; i < 11; i++) {
cout << "Player: " << players[i].getName() << endl;
players[i].playTurn();
cout << "Total balls played: " << players[i].getBalls() << endl;
cout << endl;
}

// Determine the man of the match (player with highest


score)
int highestScore = -1;
string manOfTheMatch;

for (int i = 0; i < 11; i++) {


if (players[i].getScore() > highestScore) {
highestScore = players[i].getScore();
manOfTheMatch = players[i].getName();
}
}

// Display match summary


cout << "Match Summary:" << endl;
for (int i = 0; i < 11; i++) {
cout << "Player Name: " << players[i].getName() << ",
Score: "
<< players[i].getScore() << ", Balls Played: "
<< players[i].getBalls() << endl;
}

cout << endl << "Man of the Match: " << manOfTheMatch <<
endl;

return 0;
}

You might also like