Question: using System; using static System.Runtime.InteropServices.JavaScript.JSType; class HungryRabbit { public static void Main ( string [ ] args ) { int [ , ] garden =

using System;
using static System.Runtime.InteropServices.JavaScript.JSType;
class HungryRabbit
{
public static void Main(string[] args)
{
int[,] garden =
{
{10,15,9,7,6,4,3,2,1,12},
{12,2,3,4,1,6,7,8,10,0},
{0,1,7,6,2,5,3,6,0,23},
{4,9,0,5,12,14,2,4,4,2},
{22,21,6,1,16,2,5,5,0,6},
{9,8,5,3,7,6,12,6,12,3},
{10,2,0,6,8,9,14,22,13,15},
{9,6,5,4,1,10,12,10,2,1}
};
int totalCarrots = CalculateTotalCarrots(garden);
Console.WriteLine("Total carrots eaten by the rabbit are: "+totalCarrots);
}
public static int CalculateTotalCarrots(int[,] garden)
{
int rows = garden.GetLength(0);
int cols = garden.GetLength(1);
(int x, int y)= FindStartingPosition(garden, rows, cols);
int totalCarrots =0;
while (true)
{
totalCarrots += garden[x, y];
garden[x, y]=0;
(int nextX, int nextY)= GetNextPosition(garden, x, y);
if (nextX ==-1 && nextY ==-1)
break;
x = nextX;
y = nextY;
}
return totalCarrots;
}
private static (int, int) FindStartingPosition(int[,] garden, int rows, int cols)
{
int centerRow = rows /2;
int centerCol = cols /2;
int[] possibleCenters =
{
garden[centerRow, centerCol],
(rows %2==0 && cols %2==0)? garden[centerRow -1, centerCol] : int.MinValue,
(cols %2==0)? garden[centerRow, centerCol -1] : int.MinValue,
(rows %2==0 && cols %2==0)? garden[centerRow -1, centerCol -1] : int.MinValue
};
int maxCarrots =0;
int startX = centerRow, startY = centerCol;
for (int i =0; i < possibleCenters.Length; i++)
{
if (possibleCenters[i]> maxCarrots)
{
maxCarrots = possibleCenters[i];
if (i ==1) startX = centerRow -1;
if (i ==2) startY = centerCol -1;
if (i ==3)
{
startX = centerRow -1;
startY = centerCol -1;
}
}
}
return (startX, startY);
}
private static (int, int) GetNextPosition(int[,] garden, int x, int y)
{
int rows = garden.GetLength(0);
int cols = garden.GetLength(1);
int[,] directions =
{
{-1,0},// Up
{1,0},// Down
{0,-1},// Left
{0,1}// Right
};
int maxCarrots =0;
int nextX =-1, nextY =-1;
foreach (var dir in directions)
{
int newX = x + dir[0];
int newY = y + dir[1];
if (newX >=0 && newX < rows && newY >=0 && newY < cols && garden[newX, newY]> maxCarrots)
{
maxCarrots = garden[newX, newY];
nextX = newX;
nextY = newY;
}
}
return (nextX, nextY);
}
}
It is coming up with a CS0021 error at:
int maxCarrots =0;
int nextX =-1, nextY =-1;
foreach (var dir in directions)
{
int newX = x + dir[0];
int newY = y + dir[1];
if (newX >=0 && newX < rows && newY >=0 && newY < cols && garden[newX, newY]> maxCarrots)
{
maxCarrots = garden[newX, newY];
nextX = newX;
nextY = newY;
}
}
return (nextX, nextY);
error is:
Severity Code Description Project File Line Suppression State
Error (active) CS0021 Cannot apply indexing with [] to an expression of type 'int' HungryRabbit C:\Users\anime\source\repos\HungryRabbit\HungryRabbit\Program.cs 100
And that is for 4 errors it comes back as for that entire area. Please help! Code is in C#

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!