简体   繁体   中英

Why does this recursion crash?

I want to generate random numbers between 0-4 in a matrix, check(x) checks wether ur allowed to assign x to that position, if not then -3 is assigned instead. while loop at the bottom is crashing my code, any ideas to fix this?

int Creat(int T[7][7],int i,int j){

    int x=rand()%5;
        switch(j){
            case 0: case 6:
                if(i==0||i==6){
                    if(T[i-1][j]==0||T[i][j-1]==0){
                        T[i][j]=(check(1))?1:-3;}
                    else{
                        T[i][j]=(check(x%2))?x%2:-3;
                        }
                    }
                else{
                    if(T[i-1][j]==0||T[i][j-1]==0){
                        T[i][j]=(check(x%2+1))?x%2+1:-3;}
                    else{
                        T[i][j]=(check(x%3))?x%3:-3;}
                }
            break;
            case 1:case 2:case 3:case 4:case 5:
                if(i==0||i==6){
                    if(T[i-1][j]==0||T[i][j-1]==0){
                        T[i][j]=(check(x%2+1))?x%2+1:-3;}
                    else{
                        T[i][j]=(check(x%3))?x%3:-3;}
                    }
                else{
                    if(T[i-1][j]==0||T[i][j-1]==0){
                        T[i][j]=(check(x%4+1))?x%4+1:-3;}
                    else{
                        T[i][j]=(check(x))?x:-3;}
                    }
            break;
            default: return rand()%5;
        }while(T[i][j]==-3){T[i][j]=Creat(T,i,j);}
    return T[i][j];
}

ty @jonathan Leffler,@enhzflep the while loop is free-standing, replacing it with if(T[i][j]==-3){...;} fixes the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM