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

please transle the code from c++ to java. Thank you // out of the 8 possible pat

ID: 3686215 • Letter: P

Question

please transle the code from c++ to java. Thank you

// out of the 8 possible paths, there are 5 forward moves, which i numbered as 1 to 5.. It can be changed //according to convenience.. just the number in the case statements need to be changed

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void intialise(int** a, int rows, int columns)
{
   for(int i = 0;i<rows;i++)
   {
       for(int j = 0; j<columns; j++)
           a[i][j] = 0;
   }
   a[0][0] = 1;
}
int main() {
   int rows,columns, i = 0, j = 0,count = 1;
   bool flag = false;
   cout<<"Enter the no of rows : "; cin>>rows;
   cout<<endl<<"Enter the no of columns : ";cin>>columns; cout<<endl;
   int** p = new int* [rows];
   for(i = 0; i < rows; i++)
       p[i] = new int[columns];
   intialise(p,rows,columns);
   i = 0, j = 0;
   srand (time(NULL));
   while(!((i==rows-1)&&(j == columns-1)))
   {
   //cout<<"hello ";
       int temp = rand()%8;
       switch(temp)
       {
           case 1: i--;j++;break;
           case 2: j++;break;
           case 3: i++;j++;break;
           case 4: i++;break;
           case 5: i++;j--;break;
           default: i = 0; j = 0;count = 1;flag = true; intialise(p,rows,columns); break;
       }
       if(!flag)
       {
              if((i>=rows)||(j>=columns)||(i<0)||(j<0)) {i = 0;j=0;count = 1; intialise(p,rows,columns);}
              else {count++;p[i][j] = count;}
        }
        flag = false;
   }
   cout<<"count: "<<count<<endl;
   cout<<"path "<<endl;
   for(i = 0;i<rows;i++)
   {
       for(j = 0; j<columns;j++)
           cout<<p[i][j]<<" ";
       cout<<endl;
   }
   return 0;
}

Explanation / Answer

Program:

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class Testing {
   public static void main(String str[]){
       Random randomGenerator = new Random();
       Scanner sca=new Scanner(System.in);
       int rows,columns, i = 0, j = 0,count = 1;
       boolean flag = false;
       System.out.println("Enter the no of rows : ");
       rows=sca.nextInt();
       System.out.println("Enter the no of columns : ");
       columns=sca.nextInt();
       int [][]p=new int[rows][columns];
       p[0][0]=1;
       while(!((i==rows-1)&&(j == columns-1)))
       {
       int temp = randomGenerator.nextInt(8);
       switch(temp)
       {
       case 1: i--;j++;break;
       case 2: j++;break;
       case 3: i++;j++;break;
       case 4: i++;break;
       case 5: i++;j--;break;
       default:
           i = 0;
           j = 0;
           count = 1;
           flag = true;
           for(int k=0;k<rows;k++)
           Arrays.fill(p[k],0);
           p[0][0]=1;
           break;
       }
       if(!flag)
   {
   if((i>=rows)||(j>=columns)||(i<0)||(j<0)){
       i = 0;j=0;
       count = 1;
       for(int k=0;k<rows;k++)
               Arrays.fill(p[k],0);
       p[0][0]=1;
   }
   else {
       count++;
       p[i][j] = count;
   }
   }
   flag = false;
   }
       System.out.println("count: "+count);
       System.out.println("path ");
       for(i = 0;i<rows;i++)
       {
       for(j = 0; j<columns;j++)
       System.out.print(p[i][j]+" ");
       System.out.println("");
       }
      
   }
   }

Result:

Enter the no of rows :
5
Enter the no of columns :
6
count: 11
path
1 0 0 5 0 0
2 3 6 0 0 0
0 0 0 7 8 0
0 0 0 0 9 0
0 0 0 0 10 11