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

given a 2d array(row, col) determine if the element in the array is satiesfies o

ID: 3567010 • Letter: G

Question

given a 2d array(row, col) determine if the element in the array is satiesfies or not, The element is satiesfied if the surrounding elements are similar and greater than threshold percentage. Assume the element in the array are X,O and ' '. So if an element is x at a perticular row and col, you look in the surrounding elemets if there are enough x that the element is satiesfied. If the elemet is blank it is already satiesfied. If the element is all alone on its own it is not satiesfied.

//tissue is the array that is being passed in

//row and col are also being passed in

// threshold is the percentage of like agents that must surround the agent to be satisfied

}

//then check the entire cell if it satiesfies

}

Explanation / Answer


//have to mention this two variable which is maximum rows and columns in the tissue array
   int maxRows;
   int maxCols;

public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold)
{
  
   char elem;
   int val = 0;


  
   elem = tissue[row][col];

   if(elem == ' ')
       return true;

   if((row-1)>=0)
   {
       if(tissue[row-1][col] == elem)
           val++;

       if((col-1) >= 0)
           if(tissue[row-1][col-1] == elem)
               val++;

       if((col+1) <= maxCols)
           if(tissue[row-1][col+1] == elem)
               val++;
   }

   if((col+1)<= maxCols)
   {
       if(tissue[row][col+1] == elem)
           val++;


       if((row+1) <= maxRows)
           if(tissue[row+1][col+1] == elem)
               val++;
   }

   if((col-1) >= 0)
   {
       if(tissue[row][col-1] == elem)
           val++;


       if((row+1) <= maxRows)
           if(tissue[row+1][col-1] == elem)
               val++;
   }

   if((row+1) <= maxRows)
           if(tissue[row+1][col] == elem)
               val++;


   if(val >= threshold)
       return true;
   else
       return false;


}

public static boolean boardSatisfied(char[][] tissue, int threshold){

   for(int i=0; i<maxRows; i++)
   {
       for(int j=0; j<maxCols; j++)
       {
           cout<<"For Row: "<<i <<" and Col: "<<j<<" satisfied? ==>"<<isSatisfied(tissue, i, j, threshold)<<" ";
       }
   }

}