Function only working half of the time C -


i have written random path generator project , when works works intended. works sometimes. may lot of code go through bare me.

in main function have simple switch statement call 3 tasks project.

while(continue) {  switch(example_number)     {     default: printf("no such program exists.\n");              break;     case 1:  path();              break;     case 2:  caesar_cipher();              break;     case 3:  anagram();              break;     }      printf("would test another?(y/n)\n");     scanf("\n%c",&ch);     if(ch == 'y' || ch == 'y')     {         null;     }     else     {         continue = false;     } } 

when input 1 calls function witch creates array , calls other 2 functions.

void path(void) {     //creates array walk.     char walk[10][10] = {{'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'},                          {'.','.','.','.','.','.','.','.','.','.'}};  //creates randomly generated path travel along walk. generate_path(walk); print_array(walk); } 

the function generates path.

void generate_path(char walk[10][10]) { int move_n = 0; int row, column, i; const char alph[] = {'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; //array holds previous data. int move[3][25] = {0}; bool block = false;  //allows random variable. srand((unsigned) time(null));  row = rand() % 10; column = rand() % 10;  while(!block) {     walk[row][column] = 'a';     for(i = 0; < 25; i++)     {         //goto comes here         restart:         move_n = rand() % move_dir;         //if space open continue alph array 1 row below.         if(move_n == 0 && walk[row+1][column] == '.' && row+1 < 10)         {             row += 1;             move[0][i] = i;             move[1][i] = row;             move[2][i] = column;             walk[row][column] = alph[i];         }         else if(move_n == 1 && walk[row][column+1] == '.' && column+1 < 10) //if space open continue alph array 1 column right.          {             column += 1;             move[0][i] = i;             move[1][i] = row;             move[2][i] = column;             walk[row][column] = alph[i];         }         else if(move_n == 2 && walk[row-1][column] == '.' && row-1 >= 0) //if space open continue alph array 1 row above.         {             row -= 1;             move[0][i] = i;             move[1][i] = row;             move[2][i] = column;             walk[row][column] = alph[i];         }         else if(move_n == 3 && walk[row][column-1] == '.' && column-1 >= 0) //if space open continue alph array 1 column left.               {             column -= 1;             move[0][i] = i;             move[1][i] = row;             move[2][i] = column;             walk[row][column] = alph[i];         }         else if((walk[row][column-1] == '.') || (walk[row-1][column] == '.') || (walk[row][column+1] == '.') || (walk[row+1][column] == '.'))          {             if(i == 25)                 break;             goto restart;         }         else         {             //resets data point such path can continue.             row = move[1][i-1];             column = move[2][i-1];             i--;             walk[row][column] = '.';         }     }     block = true; } } 

and print array function.

void print_array(char walk[10][10]) { int = 0; int k = 0; for(i = 0; < 10; i++) {     for(k = 0; k < 10; k++)     {         printf(" %c", walk[i][k]);      }     printf("\n"); } } 

but reason upon inputting 1 switch statement works sometimes. calling other function works every time.

thank in feel though posted code readable.

most problem if statements have in generate_path().

you access walk[row+1][column], walk[row][column+1] etc. row , column generated rand()%10. row , column values can be 0 or 9 @ point , when accessing indexesrow-1 column+1 etc, it's going access outside of array bounds can cause sorts of problems.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -