Array and Strings – Strings – Problem #6
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
Consider a repeated number game between 2 players A and B. In each round, a player will randomly generate a positive integer within certain range. The rule of the game is as follows. • The player who generates an integer having more number of digits wins that round of the game. His points will be equal to the number of digits in the number he generated. If the two player generates numbers having the same number of digits, then the number having more sum of digits generated by the player wins, and his point will be equal to the sum of the digits. So if the 2 generated numbers by A and B are 12 and 124 respectively, then B wins and his point is 3 (A’s point is 2). If the generated numbers are 567 and 612, then A wins and his point is 18 (B’s point is 9). • In each round, the winner player gets a badge of 1, and the defeated player gets a badge of 0. After all rounds are completed, the final score will be computed as the sum of total points in each round, multiplied by the number of times the player gets badge 1. The player having the maximum final score, wins the game. Your task will be to simulate the game for n number of rounds, show the results of intermediate rounds, and compute the final score to declare the winner. For your convenience, the random number generator code is provided. Include the following headers in your code: #include<time.h>, #include<stdlib.h> Add this at the beginning of main( ) function: srand(time(0)); Then the following line of code generates a random number between 5 to 1000, and stores in num: int num = (rand() % (1000 - 5 + 1)) + 5; Example: Input: Number of rounds: 3 Output: Round-1 --> A’s number: 6 B’s number: 64 A’s point: 1 B’s point: 2 A’s badge: 0 B’s badge: 1 -- B wins Round 1 -- Round-2 --> A’s number: 127 B’s number: 266 A’s point: 10 B’s point: 14 A’s badge: 0 B’s badge: 1 -- B wins Round 2 -- Round-3 --> A’s number: 792 B’s number: 115 A’s point: 18 B’s point: 7 A’s badge: 1 B’s badge: 0 -- A wins Round 3 -- Final result => A’s total score: 29, B’s total score: 46 --- B wins the game ---