将程序改编为 加入调用函数,出现问题
程序代码:
#include <stdio.h> #include <time.h> #include <stdlib.h> #define MAXX 11 #define MAXY 11 void generate_random_walk(char walk[][]); //问题点1 void print_array(char walk[][]); // int main() { char get[MAXX][MAXY]; generate_random_walk(walk); //问题点2 print_array(walk); // return 0; } void generate_random_walk(char walk[][]) //问题点3 { int i =0, j = 0, count = 1; // char walk[MAXX][MAXY]; const char str='A'; srand( (unsigned) time(NULL) ); for (i=0; i<MAXX; i++) { for (j=0; j<MAXY; j++) { if(i == 0 || j == 0 || i == MAXX-1 || j == MAXY-1) walk[i][j] = ' '; else walk[i][j]='.'; } } i = 1; j = 1; walk[1][1] = str; while(1) { if(walk[i-1][j ] != '.' && walk[i+1][j ] != '.' && walk[i ][j-1] != '.' && walk[i ][j+1] != '.') break; switch (rand()%4) //0、1、2、3分别代表上、下、左、右 { case 0: if (i-1>=0 && walk[i-1][j ]=='.') { walk[i-1][j]=str+count; count++ ; i -= 1; } break; case 1: if (i+1<MAXX && walk[i+1][j ]=='.') { walk[i+1][j]=str+count; ++count; i += 1; } break; case 2: if (j-1>=0 && walk[i ][j-1]=='.') { walk[i][j-1]=str+count; ++count; j -= 1; } break; case 3: if (j+1<MAXY && walk[i ][j+1]=='.') { walk[i][j+1]=str+count; ++count; j += 1; } break; } if(count ==26) break; } } void print_array(char walk[][]) //问题点4 { int i, j; for(i=1;i<10;i++) { for(j=1;j<10;j++) printf("%c ",walk[i][j]); printf("\n"); } }
有疑惑的地方我都注明了。主要是函数调用那和下面新函数开始的那个地方。(程序是改编【生成一种10*10的字符数组的随机步法,从A走到Z。下图为原程序的一个输出】)[local]1[/local]原程序应该没什么问题,改编如要加入调用函数。虚心求教