0

/*this is my ontroller and array object has to be converted to string type so that i can perform my action

namespace taskmvc.Controllers { public class dogController : Controller {

    public ActionResult dog()
    {
        string[,] array = new string[7, 14]
        {

            {"D","G","O","O","D","D","O","D","G","O","O","D","D","O"},
            {"O","D","O","O","G","G","G","D","O","D","G","O","G","G"},
            {"O","G","O","G","D","O","O","D","G","O","O","D","D","D"},
            {"D","G","D","O","O","O","G","G","O","O","G","D","G","O"},
            {"O","G","D","G","O","G","D","G","O","G","G","O","G","D"},
            {"D","D","D","G","D","D","O","D","O","O","G","D","O","O"},
            {"O","D","G","O","G","G","D","O","O","G","G","O","O","D"}
        };

        ViewData["f"] = array;



        return View(array);
    }

}

}

/* this is my view here you can see that my array is a string

@{
    ViewBag.Title = "dog";
}

<h2>dog</h2>

@{
    string [,] array = ViewData["f"];
    int i, j;
    var n = 7;
    var m = 14;
}


@for (i = 0; i <n;i++)
{
              for(j=0;j<m;j++)
                {
                    if (array[i, j] == "D")
                    {
                        if (j + 1 < m && array[i, j + 1] == "O")//
                        {
                            if(j + 1 < m && array[i,j+1]=="G")
                            {

                            }
                        }
                    }
                    else if (i+1<n && j + 1 < m && array[i + 1, j + 1] == "O")
                    {
                        if (i + 1 < n && j + 1 < m && array[i + 1, j + 1] == "G")
                         {

                         }
                    }
                    else if (i - 1 >0 && array[i - 1, j] == "O")//
                    {
                        if (i - 1 > 0 && array[i - 1, j] == "G")
                        {

                        }
                    }
                    else if (j-1>0 && array[i, j - 1] == "O")
                    {
                        if (j - 1 > 0 && array[i, j - 1] == "G")
                        {

                        }
                    }
                    else if(i + 1< n && array[i +1, j ] == "O")
                    {
                        if (i + 1 < n && array[i + 1, j] == "G")
                        {

                        }
                    }
                    else if(i+1<n && j-1>0 && array[i+1,j-1]=="O")
                    {
                        if (i + 1 < n && j - 1 > 0 && array[i + 1, j - 1] == "G")
                        {

                        }
                    }
                    else if (i-1>0&&j-1>0&&array[i-1,j-1]=="O")
                    {
                        if (i - 1 > 0 && j - 1 > 0 && array[i - 1, j - 1] == "G")
                        {

                        }
                    }
                    else if(i-1>0&&j+1<m&&array[i-1,j+1]=="O")
                    {
                        if (i - 1 > 0 && j + 1 < m && array[i - 1, j + 1] == "G")
                        {

                        }
                    }

                }
            }
1
  • Your for index limits are wrong. They should be 6 and 13. Indices start from 0. Commented Dec 27, 2016 at 8:04

3 Answers 3

1

j < 14, so max(j) = 13, j+1 = 14, array[i,j+1] is illegal. j+1 is out of range.

Check before:

if(j+1 < 14 && array[i,j+1]=="G")

...and in many other places. Also, replace 7 and 14 with variables. For example:

var n = 7;
var m = 14;

if(j+1 < m && array[i,j+1]=="G")
Sign up to request clarification or add additional context in comments.

1 Comment

thanks its working now but instead of printing value of a i want to print the location how can i get that for D O G when it finds it
0

Since you are checking array[i+1, j+1] , array[i - 1, j],array[i+1,j-1], then you should use the following loop :

 for (i = 1; i <6i++)
            {
                for(j=1;j<13;j++)
                {
...

Comments

0

You are trying to access array[i,j+1]

for last index j=13, and j+1 will be "14" So you are accessing array[i,14] As this memory location not allocated to to your array, You will get index out of range exception.

Try looping from i=0; j=0; and avoid situation like array[i-1,j-1]. (Boundary Conditions)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.