Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
after compiling my code I am getting the following error

Index was outside the bounds of the array.


the code is as follows:
C#
using  System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;


namespace CW2
{
    class LinkedNode
    {
        public char val;
        public LinkedNode next;
        public LinkedNode prev;
        public LinkedNode top;
        public LinkedNode bottom;



        //Default Constructor
        public LinkedNode()
        {
            val = ' ';
            next = null;
            prev = null;
            top = null;
            bottom = null;

        }
        //Constructor with parameter
        public LinkedNode(char value)
        {
            val = value;
            next = null;
            prev = null;
            top = null;
            bottom = null;
        }

        static void Main(string[] args)
        {

            //path to the file. 
            string path = @"C:\Users\Bradleigh\source\repos\MazeGame\\4-general-7x8.txt";
            string[] lines = System.IO.File.ReadAllLines(path);

            //First line holds row and column information
            string[] line = lines[0].Split(' ');
            int row = Convert.ToInt32(line[0]);
            int col = Convert.ToInt32(line[1]);
            //holding 0s 1s and starting point 's' and exit point 'o'
            char[,] maze_board = new char[row, col];

            for (int i = 1; i < lines.Length; i++)
            {
                //String to char array
                char[] arr = lines[i].ToCharArray();

                for (int j = 0; j < arr.Length; j++)
                    maze_board[i - 1, j] = arr[j];
            }
            //Display the maze now
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                    Console.Write(maze_board[i, j] + " ");
                Console.WriteLine();
                //Console.WriteLine();
            }

            //An array of nodes to create a four-way linked list for traversal of the maze
            LinkedNode[,] node = new LinkedNode[row, col];

            //Holds the start coords of the maze entrance
            int startX = -1, startY = -1;

            //Nested for loops that index and populate a 2d dimensional array which will be the maze
            for (int i = 0; i < lines.Length - 1; i++)
            {
                //Converts the maze file to an array of characters
                char[] arr = lines[i + 1].ToCharArray();
                for (int j = 0; j < arr.Length; j++)
                {
                    //Sets the array of nodes to be the number of tiles in the maze
                    node[i, j] = new LinkedNode(arr[j]);
                    //If the start of the maze is found, store the array coordinates
                    if (node[i, j].val == 's' || node[i, j].val == 'o')
                    {
                        startX = i;
                        startY = j;
                    }
                    //Checks that the left side of the maze tile is inside the array bounds
                    if (j - 1 >= 0)
                    {
                        //creates a link between the previous and next nodes
                        node[i, j].prev = node[i, j - 1];
                        node[i, j - 1].next = node[i, j];
                    }
                    //Does the same but with the top and bottom tiles
                    if (i - 1 >= 0)
                    {
                        node[i, j].top = node[i - 1, j];
                        node[i - 1, j].bottom = node[i, j];
                    }
                }
                Stack stack = new Stack();

                new Stack<linkednode>();
                //adds starting square to stack
                stack.Push(node[startX, startY]);
                //while the stack count is more than 0 pop current
                while (stack.Count > 0)
                {
                    var current = stack.Pop();
                    //marks visited with v 
                    node[startX, startY].val = 'v';
                    

                    //Queue q = new Queue();

                }
            }
        }
    }
}


the expected output is along the lines of this:
Quote:
Please enter a maze file: 4x6-maze.txt

01010e
110101
011101
11s000

Press Enter to continue...

01010e
110101
011101
11sv00

Press Enter to continue...

01010e
110101
011101
11svv0

...

0101ve
1101v1
0111v1
11svvv

Press Enter to continue...
Goal reached...

The path is shown using ‘3’...

01013e
110131
011131
11s33v


What I have tried:

I don't know what to try as this error is unexpected.
Posted
Updated 9-Jan-19 8:04am
v2
Comments
F-ES Sitecore 9-Jan-19 13:36pm    
You get that error when you access an element in an array that doesn't exist. If you have an array with two things

a[0] = 1
a[1] = 2

and you try and access a[2] then you'll get that error. Use the debugger to find out what line it occurs on and the issue is either that you are accessing data that isn't there but should be (you access a[2] as there should be three items in the array but there isn't), or you are accessing data that you shouldn't be accessing (you access a[i] and "i" is unexpectedly 2). Once you've worked that out step through the code to see what it is doing and work out what the issue is. We can't really do that for you.

The error means that you are looking for the value of an array member that has not be given a memory assignment.

For example:
C#
int data[3];
has members data[0], data[1], data[2];
If you try to access data[3] or data[4] (or data[-1]) your index is outside the bounds of the array.

You have a a 6x4 array, so make sure you don't exceed 5 in one direction and 3 in the other (i.e., 0-5, 0-3).

Finally, if you follow this in your debugger you can actually see the index value and thus watch where you don't protect against exceeding it
 
Share this answer
 
Comments
Member 14112901 9-Jan-19 13:59pm    
//adds starting square to stack
stack.Push(node[startX, startY]);

that is the line its because the variable is -1 and the array as you say is not within that range.
Member 14112901 9-Jan-19 14:00pm    
any idea on how I could fix this issue as i need to start at x,y as it is a maze?
W Balboos, GHB 9-Jan-19 14:09pm    
Check how you are initializing the value - it should be zero. What have you done to make it otherwise? You should be controlling the indices values, possibly in a loop. How is your loop initialized? If this is the basic start of the application than you need to make sure you do not assume any of the initial values for your symbols. Test them !

The debugger is your friend.
Quote:
after compiling my code I am getting the following error

If you are a little curious, around the message, you are also told where is the problem (line number). That help to understand what is the problem.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Quote:
any idea on how I could fix this issue as i need to start at x,y as it is a maze?

Simple: don't start with startX and startY outside the maze!
C#
//Holds the start coords of the maze entrance
int startX = -1, startY = -1;
Can't be the entrance, it isn't inside the maze area!
 
Share this answer
 
Comments
Member 14112901 9-Jan-19 14:55pm    
Thanks for your help :)
OriginalGriff 10-Jan-19 4:23am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900