在C语言中为数组的元素分配随机值

在C语言中为数组的元素分配随机值,c,arrays,C,Arrays,我编写了一个程序,要求用户输入两个数字M和N。将有一个数组,其中有M*N个元素,需要分配随机值。这些值随后显示在M x N表格中。问题是,当我运行程序时,第一个元素的赋值总是0或一些奇怪的数字,如-2147197728,其余元素总是0。有人能帮我吗 #include <stdio.h> #include <stdlib.h> int PopulateRandom(int M, int N); int PrintArray2D(int M, int N); // Pri

我编写了一个程序,要求用户输入两个数字M和N。将有一个数组,其中有M*N个元素,需要分配随机值。这些值随后显示在M x N表格中。问题是,当我运行程序时,第一个元素的赋值总是0或一些奇怪的数字,如-2147197728,其余元素总是0。有人能帮我吗

#include <stdio.h>
#include <stdlib.h>

int PopulateRandom(int M, int N);
int PrintArray2D(int M, int N);

// Prints elements of array in a M x N table
int PrintArray2D(int M, int N){
int array[M*N], row, column, i = 0;

while(row <= M && column <= N){
    for(row = 1; row <= M; row++){
        for(column = 1; column <= N; column++){
            array[i] = PopulateRandom(M, N);
            printf("%d  ", array[i]);
            if(column == 4){
                break;
            }
        }
        column = 0;
        printf("\n");
    }
}
return array[i];
}

//Assigns elements of the array "array" random values
int PopulateRandom(int M, int N){
int i, array[M * N];

for(i = 0; i < M*N; i++){
    array[i] = rand() % (M*N) - 1;
}
return array[i];
}

int main(void){
int option, M, N;

printf("If you would like to search ann array, enter 1 \n: ");
printf("If you would like to exit, enter 0 \n: ");
scanf("%d", &option);

while(option != 0){
    switch(option){
        case 1: if(option == 1){
                printf("Enter two numbers M and N: ");                          
                scanf("%d %d", &M, &N);
                PrintArray2D(M, N);
        }
        case 0: if(option == 0){
            break;
        }
    }
    printf("If you would like to search ann array, enter 1 \n: ");
    printf("If you would like to exit, enter 0 \n: ");
    scanf("%d", &option);
}
}
#包括
#包括
int PopulateRandom(int M,int N);
int printary2d(int M,int N);
//打印M x N表中的数组元素
int printary2d(int M,int N){
int数组[M*N],行,列,i=0;

当(row返回数组外部索引中的值时

   int PopulateRandom(int M, int N){
       int i, array[M * N];

       for(i = 0; i < M*N; i++){
           array[i] = rand() % (M*N) - 1;
       }
       return array[i];
   }
intpopulaterandom(intm,intn){
int i,数组[M*N];
对于(i=0;i

循环结束时,i将等于M*N,因为数组的大小是M*N,所以数组[i]将位于数组之外。

此处返回的是数组之外索引中的值

   int PopulateRandom(int M, int N){
       int i, array[M * N];

       for(i = 0; i < M*N; i++){
           array[i] = rand() % (M*N) - 1;
       }
       return array[i];
   }
intpopulaterandom(intm,intn){
int i,数组[M*N];
对于(i=0;i

循环结束时,i将等于M*N,数组[i]将位于数组之外,因为数组的大小为M*N。

是未初始化的变量。请注意,C中的数组通常从
0
索引。在这些情况下(很少)当方便从
1
索引数组时,数组必须大1个元素,这样上限就不会被打破。您的
PopulateRandom
函数将返回一个超出数组边界的值。顺便说一句,它实际上并不使用数组。该函数还可以包含唯一的语句
return rand()%(M*N)-1;
I在for循环中初始化了“行”和“列”。这是在
while(行)之后,
printary2d
函数也不需要数组,该函数中使用的全部是
array[0]
是未初始化的变量。请注意,C中的数组通常从
0
索引。在这些情况下(很少)当方便从
1
索引数组时,数组必须大1个元素,这样上限就不会被打破。您的
PopulateRandom
函数将返回一个超出数组边界的值。顺便说一句,它实际上并不使用数组。该函数还可以包含唯一的语句
return rand()%(M*N)-1;
我在for循环中初始化了'row'和'column'。那是在
之后,而(row)
printary2d
函数也不需要数组,在该函数中使用的只是
数组[0]