c - 需要生成二维数组中的随机数

标签 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

[/代码]

最佳答案

#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;
}

关于c - 需要生成二维数组中的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28179110/

相关文章:

c - 使用 strtok_r 时我不应该释放 char

python - 使用 Python 从 C/C++ DLL 调用方法

c++ - 我的 ddwrt 路由器没有数据

arrays - 如何在vba中对数组中的日期进行排序?

c - 将数组值传递给另一个函数时出现缓冲区溢出问题

java - Java中如何生成正数和负数

c++ - %*.*d 在 printf() 中如何工作?

c - 如何在C中对数字数组进行排序

python - random.choice() 在同一秒返回相同的值,如何避免呢?

java - 从坐标获取随机位置