I cannot figure out how to properly loop through. That is all I need to figure o
ID: 3732269 • Letter: I
Question
I cannot figure out how to properly loop through. That is all I need to figure out.
A child is located in the middle of a grid which is depicted as a rectangular A x B 2-dimensional array. The items in the array represent the number of chocolate pieces that exist in each location in the array. If the array does not contain a specific center, the child will begin in a location nearby the center with the highest number of chocolate pieces. In each iteration, the child will consume the chocolate pieces on the square where he is located and then moves up, down, left or right selecting the location which has the highest number of chocolate pieces. In case there are no chocolate pieces in the adjacent cells, the child will sleep.
Your task is to write a function which takes an array representing the locations and it should return the total number of chocolate pieces a child would eat. Test your program with the array below:
[[6, 8, 9, 7, 4]
[0, 0, 8, 0, 5]
[5, 7, 4, 5, 10]
[4, 2, 0, 6, 9]]
The child should consume 8+9+8+6 = 31 chocolate pieces.
Here is my function so far:
public static int myArr(int [][] myArray, int max) {
int newMax = 0;
for(int i = 2; i <= 3; i++) {
for(int j = 2; j<= 4; j++) {
if(myArray[i+1][j] > myArray[i-1][j] && myArray[i+1][j] > myArray[i][j-1] && myArray[i+1][j] > myArray[i][j+1]) {
max += myArray[i+1][j];
newMax = myArray[i+1][j];
myArray[i+1][j] = 0;
}
else if(myArray[i-1][j] > myArray[i+1][j] && myArray[i-1][j] > myArray[i][j+1] && myArray[i-1][j] > myArray[i][j-1]) {
max += myArray[i-1][j];
newMax = myArray[i-1][j];
myArray[i-1][j] = 0;
}
else if(myArray[i][j-1] > myArray[i+1][j] && myArray[i][j-1] > myArray[i-1][j] && myArray[i][j-1] > myArray[i][j+1]) {
max += myArray[i][j-1];
newMax = myArray[i][j-1];
myArray[i][j-1] = 0;
}
else if(myArray[i][j+1] > myArray[i][j-1] && myArray[i][j+1] > myArray[i-1][j] && myArray[i][j+1] > myArray[i][j-1]) {
max += myArray[i][j+1];
newMax = myArray[i][j+1];
myArray[i][j+1] = 0;
}
}
}
return myArr(myArray, max);
}
}
Here is my main:
public static void main(String[] args) {
int [][] myArray = {
{0,0,0,0,0,0},
{0,6,8,9,7,4,0},
{0,0,0,8,0,5,0},
{0,5,7,4,5,10,0},
{0,4,2,0,6,9,0},
{0,0,0,0,0,0}
};
myArr(myArray,max);
Thanks!
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
public class Chocolates {
public static void main(String[] args) {
int [][] myArray = {
{6,8,9,7,4},
{0,0,8,0,5},
{5,7,4,5,10},
{4,2,0,6,9},
};
int count = countChocolates(myArray);
System.out.println("The child will eat " + count + " chocolates");
}
public static int countChocolates(int[][] arr)
{
int rows = arr.length;
int cols = arr[0].length;
int r = rows/2, c = cols/2;
int count = 0;
//if there is no proper center, try to find the location with max chocolates near center
if(!(rows % 2 == 1 && cols % 2 == 1))
{
for(int i = r-1; i <= r+1; i++)
for(int j = c-1; j <= c+1; j++)
{
if(i >=0 && j >= 0 && i < rows && j < cols)
{
if(arr[i][j] > arr[r][c])
{
r = i;
c = j;
}
}
}
}
boolean done = false;
int max;
int maxR = 0, maxC = 0;
while(!done)
{
//System.out.println(arr[r][c]);
count += arr[r][c];
arr[r][c] = 0;
//find the next location to move , which has max chocolates
max = 0;
if(r >= 1 && arr[r-1][c] > max) // check upper cell
{
maxR = r-1;
maxC = c;
max = arr[maxR][maxC];
}
if(r < rows-1 && arr[r+1][c] > max) //check lower cell
{
maxR = r+1;
maxC = c;
max = arr[maxR][maxC];
}
if(c >= 1 && arr[r][c-1] > max) // check left cell
{
maxR = r;
maxC = c-1;
max = arr[maxR][maxC];
}
if(c < cols-1 && arr[r][c+1] > max) //check right cell
{
maxR = r;
maxC = c+1;
max = arr[maxR][maxC];
}
if(max == 0) //no more chocolates, stop
done = true;
else
{
r = maxR;
c = maxC;
}
}
return count;
}
}
output
=====
The child will eat 31 chocolates
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.