Question

In: Computer Science

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

/* C program to play a cicrcular game*/                                                           

/* include necessary header files */
#include <stdio.h>
#include <stdlib.h>
/* Driver method */
int main(){
        
        /* declare variable(s) */
        int n, t, i, j, sum;
        
        /* Take input(s) */
        scanf("%d%d",&n,&t);
        
        /* declare array of size n*/
        int end[100] = {0};
        /* declare temporary array of size n*/
        int temp[100] ={0};
        
        /* take input of array */
        for(i=0; i<n ; i++)
                scanf("%d",&end[i]);
        
        // calculate the values in each step
        // in backward manner
        i = 1;
        while(i <= t){
                sum = 0;
                for(j = 0; j<n ;j++){
                        /* double the values */
                        end[j] = 2 * end[j];
                        sum += end[j];
                }
                // divide by 2
                sum = sum/2;

                /* calculate the value in previous step */
                for(j = 0; j<n ;j++){
                        if(j - 1<0)
                                temp[j] = sum - (end[(j+1)%n]+end[n - 1]);
                        else
                                temp[j] = sum - (end[(j+1)%n]+end[(j-1)%n]);
                }

                /* copy data from temp array to end array*/     
                for(j = 0; j<n ;j++)
                        end[j] = temp[j];
                
                // increment value
                i = i + 1;
        }
        
        /* display array */
        for(j = 0; j<n ;j++)
                printf("%d ",end[j]);
                
        return 0;
}

___________________________________________________________________


___________________________________________________________________

3 2
3 1 2
6 -2 2

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


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 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...
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...
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
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...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
Design an algorithm to prompt user to enter a number. Determine the number is odd or...
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
How to make this problem? in C language Build an algorithm that, upon receiving as input...
How to make this problem? in C language Build an algorithm that, upon receiving as input the ID and salary of the professors of a university, determines the following: -Total payroll of teachers. -The average salaries of teachers. -ID of the employee with the highest salary as well as what's his salary thanks
c++ is the language Code an O(nlog(n)) algorithm to count the number of inversions DO NOT...
c++ is the language Code an O(nlog(n)) algorithm to count the number of inversions DO NOT ADD OR EDIT FUNCTIONS USE THESE ONLY DO NOT EDIT THE TEST FILE EITHER countInv.cpp #include <vector> #include <algorithm> using namespace std; int mergeInv(vector<int>& nums, vector<int>& left, vector<int>& right) { // You will need this helper function that calculates the inversion while merging // Your code here } int countInv(vector<int>&nums) { // Your code here } countInv_test.cpp #include <iostream> #include <vector> using namespace std;...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT