Question 1. 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 and both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumed to be always inactive. Even after updating the cell state. Consider its previous state for updating the state of other cells. Update the cell information of all cells simultaneously. Write a function cell Compete which takes one 8 element array of integer’s cells representing the current state of 8 cells and one integer days representing the number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
Existing Program
int* cellCompete(int* cells,int days)
{/
/write your code here
}
//function signature ends
Test Cases:
TESTCASES 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]
TESTCASE 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]
#include<iostream>
using namespace std;
int cellCompete(int *cells ,int day) {
//write your code here
for (int i=0;i<day;i++) {
cells[-1]=0; //assumptions
cells[8]=0;//assumptions
int u[8]; //another array to copy value
for (int i=-1;i<9;i++) {
u[i]=cells[i];
}
for(int j=0;j<8;j++) {
if(u[j-1]==u[j+1]) { //comparing the value of the neighbouring cells of u[]
cells[j]=0; //changing value of cell according to condition
}
else cells[j] = 1;
}
}
for (int i=0;i<8;i++) {
cout<<cells[i];
}
return 0;
}
int main() { //main function
int days,cells[]={1,0,0,0,0,1,0,0}; //array to pass through function
int *cellsptr=cells; //creating array values to pointer
cout << “enter days:”; //for days
cin >> days;
cout << “n[1,0,0,0,0,1,0,0]n”;
cellCompete(cellsptr, days); //passing to function
return 0;
}
Question 2.
Mooshak the mouse has been placed in a maze. There is a huge chunk of cheese somewhere in the maze. The maze is represented as a two-dimensional array of integers, where 0 represents walls. 1 represents paths where Mooshak can move and 9 represents the huge chunk of cheese. Mooshak starts in the top left corner at 0.
Write a method is Path of class Maze Path to determine if Mooshak can reach the huge chunk of cheese. The input to is Path consists of a two dimensional array and for the maze matrix. The method should return 1 if there is a path from Mooshak to the cheese. And 0 if not Mooshak is not allowed to leave the maze or climb on walls.
EX: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.
1 0 1 1 1 0 0 1
1 0 0 0 1 1 1 1
1 0 0 0 0 0 0 0
1 0 1 0 9 0 1 1
1 1 1 0 1 0 0 1
1 0 1 0 1 1 0 1
1 0 0 0 0 1 0 1
1 1 1 1 1 1 1 1
Test Cases:
Test case 1:
Input:[[1,1,1,][9,1,1],[0,1,0]]
Expected return value :1
Explanation:
The piece of cheese is placed at(1,0) on the grid Mooshak can move from (0,0) to (1,0) to reach
it or can move from (0,0) to (0,1) to (1,1) to (1,0)
Test case 2:
Input: [[0,0,0],[9,1,1],[0,1,1]]
Expected return value: 0
Explanation:
Mooshak cannot move anywhere as there exists a wall right on (0,0)
Existing Program
/*include header files needed by your program some library functionality may be restricted define any function needed
function signature begins, this function is required*/
Int isPath(int **grid,int m,int n)
{/*
write your code here
*/}
//function signature ends