Question

In: Computer Science

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 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

#include <stdio.h>

int main() {
        int n,t;
        scanf("%d%d",&n,&t);
        int arr[n];
        int i;
        for(i=0;i<n;i++){
          scanf("%d",&arr[i]);
        }
        int j;
        for(i=0;i<t;i++){
          int sum = 0;
           
          int tempArr[n]; // Create a temporary array to store the current state
          for(j=0;j<n;j++){
            sum += arr[j];
            tempArr[j] = arr[j];
          }
          for(j=0;j<n;j++){
            arr[j] = 2*tempArr[(j+1)%n] + 2*tempArr[(j-1+n)%n] - sum;  // New element = prev[i+1]+prev[i-1] - sum(all elements excluding the two just added)
          }
           
        }
        for(j=0;j<n;j++){
          printf("%d ",arr[j]);
        }
        printf("\n");
        
}


Explanation:

This code is based on the calculations I did to figure out the formula of getting a round back.

It is basically this.

PrevRoundElement[i] = CurrRoundElement[i+1] + CurrRoundElement[i-1] - SumOfAllCurrRoundElements(excluding (i+1) and (i-1) elements)

If you have doubt you can ask in the comments below


Related Solutions

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...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
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...
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...
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...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
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,...
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?
Question 1 a) Determine whether the language {a n b m c n | n >...
Question 1 a) Determine whether the language {a n b m c n | n > 0} is regular or not using pumping Lemma. b) Prove that the language {(ai bn | i, n > 0, i = n or i = 2n} is not regular using the Pumping Lemma.
Using C programming make one for loop Description For this problem you will be figuring out...
Using C programming make one for loop Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT