jeudi 1 novembre 2018

How to create map with random numbers inside in c

I have task about printing board where you should input dimension and program will create a board with 2 numbers inside each cell: 1'st number is random from 1-3 and second one should be zero. I can make it only with zero but when i tried to make a random number everything went wrong way.. Maybe anyone knowns what it wrong with it?

Function calls uploading map:

int randfunc(int i, int n);

    int uploadmap(int m,int n){
    int a[m][n];
    int i,j,k;
    // time_t t;
    //srand((unsigned)time(&t));
    //int randnum =  rand() % 3 + 1;
    for(i = 0; i < m;i++){
        printf("+---");

    }
    printf("+\n");
    memset(a,0,sizeof(a));
    for(i = 0;i < m;i++){
        for(j = 0; j < n;j++){
            printf("|%d %d",randfunc(i,n),a[i][j]);
        }
        printf("|\n");
        for(k = 0;k < m;k++){
            printf("+---");

        }
        printf("+\n");
    }
    return 0;
}

function which calls random numbers from 1 to 3:

int randfunc(int i, int n) {

    time_t t;

    srand((unsigned) time(&t));

    for( i = 0 ; i < n ; i++ ) {
        printf("%d\n", rand() % 3 + 1);
    }

    return 0;
}

Main function :

int main(int argc, const char * argv[]) {
    int m,n;
    printf("Enter dimension: \n");
    scanf("%d %d",&m, &n);
    printf("Map has been uploaded %d\n",uploadmap(m,n));

    return 0;
}




Aucun commentaire:

Enregistrer un commentaire