1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Fittoniya [83]
3 years ago
11

There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour).

Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day,. otherwise itbecomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive. Even after updating the cell state. consider its pervious state for updating the state of other cells. Update the cell informationof allcells simultaneously.
Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing te number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
Program:
int* cellCompete(int* cells,int days)
{
//write your code here
}
//function signature ends
Test Case 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
Test Case 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]
This is the problem statement given above for the problem. The code which I have written for this problem is given below. But the output is coming same as the input.
#include
using namespace std;
// signature function to solve the problem
int *cells(int *cells,int days)
{ int previous=0;
for(int i=0;i {
if(i==0)
{
if(cells[i+1]==0)
{
previous=cells[i];
cells[i]=0;
}
else
{
cells[i]=0;
}
if(i==days-1)
{
if(cells[days-2]==0)
{
previous=cells[days-1];
cells[days-1]=0;
}
else
{
cells[days-1]=1;
}
}
if(previous==cells[i+1])
{
previous=cells[i];
cells[i]=0;
}

else
{
previous=cells[i];
cells[i]=1;
}
}
}
return cells;
}
int main()
{
int array[]={1,0,0,0,0,1,0,0};
int *result=cells(array,8);
for(int i=0;i<8;i++)
cout< }
I am not able to get the error and I think my logic is wrong. Can we apply dynamic programming here If we can then how?

Computers and Technology
1 answer:
olya-2409 [2.1K]3 years ago
7 0

Answer:

I am writing a C++ program using loops instead of nested if statements.

#include <iostream> // to use input output functions

using namespace std; // to identify  objects like cin cout

void cells(int cells[],int days){ /* function that takes takes one array of integers cells, one integer days representing the number of days to simulate. */

int pos ,num=0; //declares variables pos for position of two adjacent cells and num to iterate for each day

int result[9]; //the updated output array

while (num< days)   { //this loop keeps executing till the value of num is less than the value of number of days

num++;

for(pos=1;pos<9;pos++)   //this loop has a pos variable that works like an index and moves through the cell array

    result[pos]=(cells[pos-1])^ (cells[pos+1]); //updated cell state determined by the previous and next cells (adjacent cells) by bitwise XOR operations

     for(pos=1;pos<9;pos++) //iterates through the array

         cells[pos]=result[pos];    } //the updated cells state is assigned to the cell array simultaneously

   for(pos=1;pos<9;pos++) //iterates through the array and prints the resultant array that contains the updated active and inactive cells values

        cout << result[pos]; }

int main() { //start of the main function body

int j,day;

int output[9];

*/the two cells on the ends (first and last positions of array) have single adjacent cell, so the other adjacent cell can be assumed to be always inactive i.e. 0 */

output[0]=output[9]=0;

for(j=1;j<9;j++) //takes the input array from user

cin >> output[j];

cin >> day;

cells(output,day); } //calls the function cells to print the array with active and inactive cells states.

Explanation:

The program is well explained in the comments mentioned with every statement of the program. I will explain with the help of example:

Suppose the user enters the array = [1,0,0,0,0,1,0,0] and days=1

The while loop checks if the value of num is less than that of days. Here num is 0 and days is 1 So this means that the body of while loop will execute.

In the body of while loop the value of num is incremented by 1. The first loop initializes a variable pos for position of adjacent cells. The statement is a recursive statement result[pos]=(cells[pos-1])^ (cells[pos+1]) that uses previous state for updating the state of other cells. The “^” is the symbol used for bitwise exclusive operator. In bitwise XOR operations the two numbers are taken as operands and XOR is performed on every bit of two numbers. The result of XOR is 1 if the two bits are not same otherwise 0. For example XOR of 1^0 and 0^1 is 1 and the XOR of 0^0 and 1^1 is 0. The second for loop update the cell information of all cells simultaneously. The last loop prints the updated cells states.

The main function takes the input array element from user and the value for the days and calls the cells function to compute the print the active and inactive cells state information.

The screenshot of the program along with its output are attached.

You might be interested in
What type of engineer works on cleaning up oil spills?
Margaret [11]

your answer is b. i know this because oil is a chemical.

7 0
3 years ago
Read 2 more answers
Which of these is a Microsoft certification for system engineers?
Fiesta28 [93]

MCSE is a Microsoft certification for system engineers

<u>Explanation:</u>

The MCSE Certification is a certification to enhance a system engineer. They are a set of exams that examine a person’s proficiency with various Microsoft technologies similar to Windows Server, SQL Server, System Center and Office 365. MSCE has different qualities and with various specifications. One can learn all the features in our Microsoft certification guide on the official MCSE page.

Becoming approved as a Microsoft Certified Software Engineer is not inferior, so it’s deserving is often questioned. However, getting the MCSE, like any other Microsoft Certification, is deserving of the payment.

6 0
3 years ago
Write a function called mul_time that takes a Time_Elapsed object and a number and returns a new Time_Elapsed object that contai
lisov135 [29]

# Write a function called mul_time that takes a Time object and a number and

# returns a new Time object that contains the product of the original Time and

# the number.

# Then use mul_time to write a function that takes a Time object that

# represents the finishing time in a race, and a number that represents the

# distance, and returns a Time object that represents the average pace (time

# per mile).

# Current Status: Complete

class Time(object):

   """ represents the time of day.

   attributes: hour, minute, second"""

time = Time()

time.hour = 3

time.minute = 0

time.second = 0

def time_to_int(time):

   minutes = time.hour * 60 + time.minute

   seconds = minutes * 60 + time.second

   return seconds

def int_to_time(seconds):

   new_time = Time()

   minutes, new_time.second = divmod(seconds, 60)

   time.hour, time.minute = divmod(minutes, 60)

   return time

def mul_time(time, multicand):

   time_int = time_to_int(time) * multicand

   new_time = int_to_time(time_int)

   if new_time.hour > 12:

       new_time.hour = new_time.hour % 12

#    print ("New time is: %.2d:%.2d:%.2d"

#    % (new_time.hour, new_time.minute, new_time.second))

   return new_time

# mul_time(time, 2)

def race_stats(time, distance):

   print ("The finish time was %.2d:%.2d:%.2d"

         % (time.hour, time.minute, time.second))

   print "The distance was %d miles" % (distance)

   average = mul_time(time, (1.0 / distance))

   print ("The average is: %.2d:%.2d:%.2d per mile"

         % (average.hour, average.minute, average.second))

race_stats(time, 3)

7 0
2 years ago
Write a program that will add up the series of numbers: 99, 98, 97… 3, 2, 1. The program should print the running total as well
Elanso [62]

Answer:

a = 99

b = 99

while(a > 0):

    b += a

    print(b)

    a -= 1

Explanation:

4 0
3 years ago
William found out that someone used his report on American culture without his permission. What is William a victim of?
Mnenie [13.5K]

He is a victim of plagiarism.

Plagiarism is when someone steals someone else's work, to help benefit themselves. Taking credit for the other person work.

Hope this helps!

3 0
3 years ago
Read 2 more answers
Other questions:
  • PLEASE HELP ASAP!!!!
    14·1 answer
  • Do you like PC? Why? (I need your opinion for research)
    10·2 answers
  • Write a for loop to verify that your function is correctly returning the expected output for the radius values between 0 and 11.
    14·1 answer
  • C++ CODE
    6·1 answer
  • How can i turn on my prinfer without getting up
    12·2 answers
  • How would you describe the relationship between blocks of code and commands?<br>HELP​
    10·1 answer
  • If you need some one to talk to you can talk to me
    13·2 answers
  • 69696969696969696969696969696969696969696969696969696969696969696969696969696969696969696969696969696969696969696969696969696969
    8·1 answer
  • Figure out what this says:<br><br> ?driew tib a kool ti seoD
    7·1 answer
  • What maintains data about various types of objects, events, people, and places?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!