需要在二维数组中生成随机数

时间:2015-01-27 20:11:21

标签: c arrays random

我正在制作一个程序,我必须生成具有随机数的N * N的二维数组而不重复,使得一个元素是字母表。

此外,如果矩阵大小为3,那么元素必须介于1和8之间,而不重复和单个字母。

类似于大小为4的矩阵,元素必须在1到15之间,不重复和单个字母。

有人可以帮忙吗?

[代码]

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,j,b,c,n ;

char ar[100][100];
char temp,ch;
printf("enter the size of the matrix : ");
scanf("%d",&n);


for(i=0;i<n;i++)
{
        printf("\n");
    for(j=0;j<n;j++)
    {

        scanf("%d",&ar[i][j]);
        printf("\t");


    }
}

ar[n-1][n-1] = 'E';

// for printing the array
    for(i=0;i<n;i++)

    {
        printf("\n");
    for(j=0;j<n;j++)

        {



            if(i==(n-1)&&j==(n-1))
        {
            printf("%c",ar[n-1][n-1]);
        }
            else
            printf("%d",ar[i][j]);

        printf("\t");



    }
}

printf("\n Press 'w' for shifting up 's' for down 'a' for left and     'd'       for right  : \n");

while(1)  //infinite loop
{

    for(i=0;i<n;i++)

    {

    for(j=0;j<n;j++)

        {

            if(ar[i][j]=='E')
            {
                b = i; //storing row
                c = j; // storing column
                printf("\t");
                ch = getch();

                switch(ch)

                {



                case 'w':




                if((i-1)>=0)
                {
                    system("CLS");
                    temp = ar[i-1][j];
                    ar[i-1][j] = ar[i][j];
                    ar[i][j] = temp;

                    for(i=0;i<n;i++)
                {
                        printf("\n");
                    for(j=0;j<n;j++)
                    {
                            if(i==(b-1)&&j==c)
        {
            printf("%c",ar[n-1][n-1]);
        }
            else
            printf("%d",ar[i][j]);

        printf("\t");

                    }
                }
                printf("\t");   
                }

                else
                {
                    printf("wrong move bro");
                }



                break;



                case 'a':




                if((j-1)>=0)
                {
                    system("CLS");
                    temp = ar[i][j-1];
                    ar[i][j-1] = ar[i][j];
                    ar[i][j] = temp;

                        for(i=0;i<n;i++)
                {
                    printf("\n");
                    for(j=0;j<n;j++)
                    {

                        if(i==b&&j==(c-1))
        {
            printf("%c",ar[n-1][n-1]);
        }
            else
            printf("%d",ar[i][j]);

        printf("\t");


                    }
                }
                printf("\t");   
                }

                else
                {


                printf("wrong move bro");
                }



                break;


                case 's' :

                if((i+1)<n)
                {
                    system("CLS");
                    temp = ar[i+1][j];
                    ar[i+1][j] = ar[i][j];
                    ar[i][j] = temp;

                        for(i=0;i<n;i++)
                {
                    printf("\n");
                    for(j=0;j<n;j++)
                    {

                        if(i==(b+1)&&j==c)
        {
            printf("%c",ar[n-1][n-1]);
        }
            else
            printf("%d",ar[i][j]);

        printf("\t");


                    }
                }
                printf("\t");   
                }

                else
                {


                printf("wrong move bro");
                }
                break;

                case 'd' :

                    if((j+1)<n)
                {
                    system("CLS");
                    temp = ar[i][j+1];
                    ar[i][j+1] = ar[i][j];
                    ar[i][j] = temp;

                        for(i=0;i<n;i++)
                {
                    printf("\n");
                    for(j=0;j<n;j++)
                    {

                        if(i==b&&j==(c+1))
        {
            printf("%c",ar[n-1][n-1]);
        }
            else
            printf("%d",ar[i][j]);

        printf("\t");


                    }
                }
                printf("\t");   
                }

                else
                {


                printf("\n wrong move bro");

                }
                break;










            }  //switch end





        }  //if end



























}  //for end


}  //for end


}  //while end

}   // main end

[/代码]

1 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <errno.h>

typedef struct no_dup_rand {
    size_t size;
    int *data;
} NDR;
NDR *NDR_new(int start, int end);
int NDR_rand(NDR *ndr);
void NDR_drop(NDR *ndr);

int main(void){
    int ar[100][100];
    int i, j, n;
    NDR *ndr;

    srand(time(NULL));
    printf("enter the size of the matrix : ");
    scanf("%d", &n);

    ndr = NDR_new(1, n*n-1);//specify the range
    for(i = 0; i < n; ++i){
        for(j = 0; j < n; ++j){
            if(i != n-1 || j != n-1)
                ar[i][j] = NDR_rand(ndr);//the one number of the specified range will be returned at random
        }
    }
    ar[n-1][n-1] = 'E';
    NDR_drop(ndr);//Disposal
    for(i = 0; i < n; ++i){
        for(j = 0; j < n; ++j){
            if(i != n-1 || j != n-1)
                printf("%d ", ar[i][j]);
            else
                printf("%c", ar[i][j]);
        }
        putchar('\n');
    }

    return 0;
}

NDR *NDR_new(int start, int end){//range : start <= r <= end
    int i,v;
    NDR *ret = malloc(sizeof(*ret));
    ret->size = end - start + 1;
    ret->data = malloc(ret->size * sizeof(int));
    for(i=0, v=start; v <= end; ++v)
        ret->data[i++] = v;
    return ret;
}

void NDR_drop(NDR *ndr){
    free(ndr->data);
    free(ndr);
}

int NDR_rand(NDR *ndr){
    if(ndr->size){
        size_t i = rand() % ndr->size--;//note : assert(RAND_MAX >= ndr->size)
        int ret = ndr->data[i];
        ndr->data[i] = ndr->data[ndr->size];
        return ret;
    }
    fprintf(stderr, "empty!\n");
    errno = ERANGE;
    return INT_MIN;
}