c# - Error at BFS search .Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index -


i have function create bfs . have 1 function , code , :

static void breadth_first_search(queue<int> q, list<int > trace, int[,] grid, int start, int nodes)         {             int u;             list<int> visited = new list<int>(220000);                         q.enqueue(start);                         trace[start] =  -1;                         visited[start] = 1;                         {                 u = q.peek();                 q.dequeue();                 (int v = 1; v <= nodes; ++v)                 {                     if ((grid[u, v] == 1) && visited[v] == 0)                     {                         q.enqueue(v);                         trace[v] = u;                         visited[v] = 1;                     }                 }             } while (q.count != 0);         } 

the problem don't work. have error :

index out of range. must non-negative , less size of collection. parameter name: index

at here :

trace[start] =  -1;             visited[start] = 1; 

i called function in main :

static int main()         {              list<int> trace = new list<int>(220);             queue<int> q = new queue<int>();             int[,] grid = new int[582,582];                                        int nodes;             int vertices;             console.write("please input number of node : \n");              nodes = convert.toint32(console.readline());             vertices = 200;             read_input_from_user(grid, vertices);             int starting_position;             int finishing_position;             console.write("please input starting node : \n");                           starting_position = convert.toint32(console.readline());             console.write("please input finishing node : \n");             finishing_position = convert.toint32(console.readline());             breadth_first_search(q, trace, grid, starting_position, nodes);             trace_result(trace, starting_position, finishing_position, nodes);             console.readline();             return 0;         } 

please me..

thanks

when user enters greater 220 starting position, line

trace[start] =  -1; 

will throw exception, since start indeed out of trace bounds. therefore need force user enter can handle. this:

console.write("please input starting node : \n"); starting_position = convert.toint32(console.readline()); while (starting_position < 0 || starting_position >= trace.count) {     console.write("starting node invalid. should between 0 , 220. please enter 1  : \n");     starting_position = convert.toint32(console.readline()); } 

this idea, point - should think user input validation not break program.

update. did not realize trace in code above contains no entries @ all. means calling index produce error - if list not contain elements @ all, impossible call 1 or set value. while still true should have validation mechanism user input, main problem here how list treated. here ways resolve it:

  1. you can initialize list same value, 0:

    list<int> trace = enumerable.repeat(0, 220).tolist(); 
  2. or can use array instead of list:

    int[] trace = new int[220]; 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -