Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: If the rover is at the edge, an instruction telling it to go further towar

ID: 3746657 • Letter: N

Question

Note: If the rover is at the edge, an instruction telling it to go further towards the edge should not have any effect. For example, the command list commands [RIGHT, UP, DOWN, LEFT, LEFT, DOWN, DOWN) and the matrix size is 4. The fo UP, and 4, LEFT, the moves would take the rover out of the matrix, so they are disregarded. The rover ends up in cell 12 A 4x4 matrix, labeled 0 1 2 3 4 56 7 81 9 10 11 12 13 14 15 Function Description Complete the function roverMove in the editor below. The function must return the label of the cell the rover occupies after roverMove has the following parameter(s): n: an integer that represents the size of the square matrix mds(cmds po ,..c.mds(m-1)): an array of strings that represent the commands Constraints * 2sns 20

Explanation / Answer

int roverMove(int n,String cmnds[])

{

int row=0,col=0;

String a="UP",b="DOWN",c="RIGHT",d="LEFT";

int l=cmnds.length(); //finding the number of commands given in the commands array.

for(int i=0;i<l;i++)

{

if(cmnds[i].equals(a))   

{

if((row-1)>=0) /*As it must traverse only if it is staying inside the matrix after the instruction,if it if in the 1st row it should not move in upward direction so it is limited to positive index for row */

{

row=row-1;

}

}

if(cmnds[i].equals(b))

{

if((row+1)<n) /*As it must traverse only if it is staying inside the matrix after the instruction,if it if in the last row it should not move in downward direction so it is limited to index less than the size of matrix for row */

{

row=row+1;

}

}

if(cmnds[i].equals(d))

{

if((col-1)>=0) /*As it must traverse only if it is staying inside the matrix after the instruction,if it if in the first coloumn it should not move in left direction so it is limited to positive index for coloumn */

{

col=col-1;

}

}

if(cmnds[i].equals(c))

{

if((col+1)<n) /*As it must traverse only if it is staying inside the matrix after the instruction,if it if in the last coloumn it should not move in right direction so it is limited to index less than the size of matrix for coloumn */

{

col=col+1;

}

}

}

return a[row][col]; /*Here we are returning the index of the cell where rover occupies after following the instructions. */

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote