0% found this document useful (0 votes)
24 views6 pages

Code

Uploaded by

Wajiha Asif
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)
24 views6 pages

Code

Uploaded by

Wajiha Asif
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/ 6

Sure, let's go through the simplified C++ code step by step to understand how it works:

```cpp

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <string>

using namespace std;

class Player {

public:

string playerName;

int totalScore;

int ballsPlayed;

Player() : totalScore(0), ballsPlayed(0) {}

void setName(string name) {

playerName = name;

};

int main() {

srand(time(0)); // Seed for random number generation


const int numPlayers = 11;

string playerNames[numPlayers] = {

"Player1", "Player2", "Player3", "Player4", "Player5",

"Player6", "Player7", "Player8", "Player9", "Player10", "Player11"

};

Player players[numPlayers];

// Initialize players with names

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

players[i].setName(playerNames[i]);

// Each player takes turn to bat

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

cout << "Turn of " << players[i].playerName << ":\n";

while (true) {

int run = -1 + rand() % 8; // Generate number between -1 and 6 (excluding 5)

if (run == 5) continue; // Invalid score

if (run == -1) {

cout << "Ball " << players[i].ballsPlayed + 1 << ": OUT\n";

players[i].ballsPlayed++;

break;

} else {
cout << "Ball " << players[i].ballsPlayed + 1 << ": " << run << " runs\n";

players[i].totalScore += run;

players[i].ballsPlayed++;

cout << "Total balls played by " << players[i].playerName << ": " << players[i].ballsPlayed << "\n\n";

// Display match summary

cout << "Match Summary:\n";

Player* manOfTheMatch = &players[0];

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

cout << "Player: " << players[i].playerName << ", Score: " << players[i].totalScore << ", Balls Played: "
<< players[i].ballsPlayed << "\n";

if (players[i].totalScore > manOfTheMatch->totalScore) {

manOfTheMatch = &players[i];

// Display man of the match

cout << "\nMan of the Match: " << manOfTheMatch->playerName << " with " << manOfTheMatch-
>totalScore << " runs\n";

return 0;

```
### Explanation:

1. **Include Directives:**

- `#include <iostream>`: Includes the input-output stream library for console input and output.

- `#include <cstdlib>`: Includes the C standard library for functions like `rand()`.

- `#include <ctime>`: Includes the C time library for functions like `time()`.

- `#include <string>`: Includes the string library for string manipulation.

2. **Using Namespace std:**

- `using namespace std;`: Allows us to use names from the standard library without the `std::` prefix.

3. **Player Class:**

- `class Player { ... };`: Defines a `Player` class to store each player's information.

- `public:`: All members following this are publicly accessible.

- `string playerName;`: Stores the player's name.

- `int totalScore;`: Stores the total score of the player.

- `int ballsPlayed;`: Stores the number of balls played by the player.

- `Player() : totalScore(0), ballsPlayed(0) {}`: Constructor initializes `totalScore` and `ballsPlayed` to 0.

- `void setName(string name) { playerName = name; }`: Method to set the player's name.

4. **Main Function:**

- `int main() { ... }`: Entry point of the program.

- `srand(time(0));`: Seeds the random number generator with the current time to ensure different
random numbers each run.

- `const int numPlayers = 11;`: Defines the number of players.


- `string playerNames[numPlayers] = { ... };`: Array storing the names of the players.

- `Player players[numPlayers];`: Array of `Player` objects.

5. **Initialize Players with Names:**

- `for (int i = 0; i < numPlayers; ++i) { players[i].setName(playerNames[i]); }`: Loop to assign names to
each player.

6. **Each Player Takes Turn to Bat:**

- `for (int i = 0; i < numPlayers; ++i) { ... }`: Loop through each player.

- `while (true) { ... }`: Loop until the player is out.

- `int run = -1 + rand() % 8;`: Generate a random number between -1 and 6 (excluding 5).

- `if (run == 5) continue;`: Skip the invalid score 5.

- `if (run == -1) { ... }`: If the player is out, print "OUT" and break the loop.

- `else { ... }`: Otherwise, add the score to `totalScore` and increment `ballsPlayed`.

7. **Display Match Summary:**

- `cout << "Match Summary:\n";`: Print the match summary header.

- `Player* manOfTheMatch = &players[0];`: Initialize the pointer for "Man of the Match".

- `for (int i = 0; i < numPlayers; ++i) { ... }`: Loop through each player to print their summary and
determine "Man of the Match".

8. **Display Man of the Match:**

- `cout << "\nMan of the Match: " << manOfTheMatch->playerName << " with " << manOfTheMatch-
>totalScore << " runs\n";`: Print the name and score of the "Man of the Match".

### Summary:

- The program initializes 11 players with names.


- Each player takes turns to bat, with scores generated randomly.

- The match summary and "Man of the Match" are displayed at the end based on the players'
performance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy