Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part 1: Array Game: Develop a program in C++ that finds the winner of a game that is played amongst different elements of an
Part 1: Array Game: Develop a program in C++ that finds the winner of a game that is played amongst different elements of an array of distinct integers as per the following rule: A game will be played between the first two elements of the array (for example - arr[0] and arr[1], if the name of the array is arr[]). In each round of the game, compare arr[0] with arr[1], the larger integer wins and remains at position 0, and the smaller integer moves to the end of the array. The game ends when an integer wins 'k' consecutive rounds. It is guaranteed that there will be a winner of this game. Your program should ask the user to input an array of distinct integers, its size, and an integer 'k'. It should return the integer which will win the game. For instance, if the user enters an array consisting of the elements - 2,1,3,5,4,6,7 and the value of k as 2, then the following is the way the game runs: Round | 1 arr | [2,1,3,5,4,6,7] | 2 | winner | win_count 1 2 | [2,3,5,4,6,7,1] | 3 3 | [3,5,4,6,7,1,2] | 5 1 4 |[5,4,6,7,1,2,3] | 5 | 2 So, we can see that 4 rounds will be played and 5 is the winner because it wins 2 consecutive games. Sample Input/Output Example 1: Input: Enter the size of the array: 7 Enter the array elements: 2 1 3 5 4 6 7 Enter the value of k: 2 Output: The winner is 5
Step by Step Solution
★★★★★
3.38 Rating (160 Votes )
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started