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...
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...
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...
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,...
Can you give me a turing machine for the language c* n b*2n a* n+2 for...
Can you give me a turing machine for the language c* n b*2n a* n+2 for n>=0 . Please give the entire diagram with states, transition function, alphabet. Can use either one-tape or two-tape (both infinite). Describe the logic used to build the machine. Run the TM that accepts for any string of length > 1. Also run for string cbba.
A 5 kg pully has a string round around it and a force of 1.0 N...
A 5 kg pully has a string round around it and a force of 1.0 N pulls down on the string. The pully is frictionless, 10 cm in radius, and is a uniform disk. The pully is initially at rest. A) What is the torque of the pully? B) What is the angular acceleration of the pully? C) How many times will the pully rotate in 3.0 seconds? D) What will the pully's angular velocity be after 3.0 seconds?
Be sure to use only C for the Programming Language in this problem. Before we start...
Be sure to use only C for the Programming Language in this problem. Before we start this, it is imperative that you understand the words “define”, “declare” and “initialize” in context of programming. It's going to help you a lot when following the guidelines below. Let's begin! Define two different structures at the top of your program. be sure to define each structure with exactly three members (each member has to be a different datatype). You may set them up...
C++ Language Problem 2 (Save and Get Info) : Write a program that asks for the...
C++ Language Problem 2 (Save and Get Info) : Write a program that asks for the user's name, phone number, and address. The program then saves all information in a data file (each information in one line) named list.txt. Finally, the program reads the information from the file and displays it on the screen  in the following format: Name: User's Name   Phone Number: User's Phone Number   Address: User's Street Address User's City, State, and Zip Code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT