Question

In: Computer Science

Use C language Level 2 Problem description n (n is odd) people sitting around a round...

Use C language

Level 2

  • Problem description

n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two numbers, replace the number on his/her whiteboard with the average finally. The game terminates when any one has to write a non-integer number on his/her whiteboard.

Given the number of people, the number of passed rounds and the integer numbers on their whiteboards at this moment, you are asked the integer numbers on their whiteboards at the very beginning. The number of rounds increases by 1 when changing the number on the whiteboard and new numbers are all integers.

  • Input & output requirements

Input the number of people n and the number of rounds t on the first line. On the next line are numbers on their whiteboards after t rounds.

Output the integer numbers on their whiteboards at the very beginning.

  • Sample input

3 2

3 1 2

  • Sample output

6 -2 2

Solutions

Expert Solution

If you like the solution please give it a thumbs up. And if you have any query please let me know in the comments.

Solution :-

C code :-

#include <stdio.h>
int main() {
        
        int n = 3;
        int arr[3] = {6,-2,2};
        
        int level = 0;  // initially level 0
        
        while(1)
        {
            int allInteger = 1; // if all the values in array are integers
            int temp[n];    // temp array to store average values
            for(int i=0;i<n;i++)
            {
                int left = arr[((i-1)+n)%n];    // find value of left element
                int right = arr[(i+1)%n];       // find value of right element
                
                if((left+right)%2 == 1)         // if sum of both is odd then avg will be non-integer
                {
                    allInteger = 0;
                    break;
                }
                
                temp[i] = (left+right)/2;
            }
            
            // if non-integer terminate
            
            if(allInteger == 0)
            {
                break;
            }
            
            // if all are integers then copy average value to original array
            for(int i=0;i<n;i++)
                arr[i] = temp[i];
                
           // increment level
           level = level+1;
        }
        
        printf("level = %d \n", level);
        
        for(int i=0;i<n;i++)
            printf("%d ", arr[i]);
}

Sample Output :-

level = 2 
3 1 2 

Related Solutions

Using C language Problem description n (n is odd) people sitting around a round table playing...
Using C language Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two numbers, replace...
Using C language, and describe the algorithm design Q2 Problem description n (n is odd) people...
Using C language, and describe the algorithm design Q2 Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the...
Use C language Problem 1: Buy cheese (Level 1) • Problem description You have M dollors...
Use C language Problem 1: Buy cheese (Level 1) • Problem description You have M dollors and want to buy as much cheese as possible. There is a cheese shop with two kinds of cheese. The unit prices of the both cheese are p1 and p2, the total amount of them are a1 and a2. You can buy either cheese or both, but the amount you buy cannot exceed its total amount. Write a program to output the largest amount...
Problem Description There are bunnies standing in a line, numbered 1, 2, ...n. The odd bunnies...
Problem Description There are bunnies standing in a line, numbered 1, 2, ...n. The odd bunnies (1, 3, ..) have the normal 2 ears. The even bunnies (2, 4, ..) are genetic mutations and have 3 ears. Write a GUI program which uses a recursive method, earCount(), to calculate the number of "ears" in the bunny line. The user will supply the number of bunnies in the line. Specifications Your program will consist of 2 Textboxes and a Button, each...
Use c++language Use the pseudocode description below to write a program that uses an if, else...
Use c++language Use the pseudocode description below to write a program that uses an if, else if, else decision structure for a program that will determine if someone lives in Boston. 1. display message that describes what the program will do. 2. ask the user to input an answer to the question: Do you live in Boston?. 3. if they entered 'y', display a message confirming that they live in Boston. 4. if they entered 'n' , display a message...
Class object in C++ programming language description about lesson inheritance multi level example.
Class object in C++ programming language description about lesson inheritance multi level example.
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students to generate a linked list of playing card based on data read from a file and to write out the end result to a file. linkedList.h Create a header file name linkedList Include the following C header files: stdio.h stdlib.h string.h Create the following macros: TRUE 1 FACES 13 SUITS 4 Add the following function prototypes: addCard displayCards readDataFile writeDataFile Add a typedef struct...
Using C language, and describe the algorithm design All the sample is correct Q3 Problem description...
Using C language, and describe the algorithm design All the sample is correct Q3 Problem description Tim was born in a leap year, so he would like to know when he could have his birthday party. Could you tell him? Given a positive integers Y indicating the starting year, and a positive integer N, your task is to tell the N-th leap year from year Y. Note: if year Y is a leap year, then the 1st leap year is...
C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an...
C PROGRAMMING LANGUAGE PROBLEM TITLE : ARRAY usually, if people want to input number into an array, they will put it from index 0 until N - 1 using for. But, Bibi is bored to code like that. So, she didin't want to input the number that way. So Bibi challenged you to make a program that will read a sequence (represent index) that she made, then input the number to an array but input it with the same sequence...
Implement Hot Potato game A group of people, numbered 1 to N, are sitting in a...
Implement Hot Potato game A group of people, numbered 1 to N, are sitting in a circle. Starting at person 1, a hot potato is passed. After X passes, the person holding the hot potato is eliminated, the circle closes ranks, and the game continues with the person who was sitting after the eliminated person picking up the hot potato. The last remaining person wins. For example: if X = 0 and N = 5, players are eliminated in order,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT