【问题标题】:Printing random elements of an array in C在C中打印数组的随机元素
【发布时间】:2017-01-31 23:45:06
【问题描述】:

我编写了一个包含 M*N 个元素的程序,其中变量 M 和 N 由用户输入。该程序打印 M x N 表中的元素,并为每个元素分配随机值。当我尝试编译程序时,不断弹出两个错误。第一条消息说在第 12 行有一个函数“PopulateRandom”的隐式声明。第二条消息说在第 25 行的“int”之前有一个缺少的表达式。

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

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

    while(row <= M && column <= N){
    for(row = 0; row <= M; row++){
        for(column = 0; column <= N; column++){
            array[i] = PopulateRandom(array[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 array[int M * int N]){
    int i;

    while(i = 0; i < M*N; i++){
    array[i] = rand() % (M*N);
    }
    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);
    }
}

【问题讨论】:

  • PopulateRandom 没有函数原型(前向引用),因此编译器只能对其参数和返回类型进行假设。
  • ... 和 array[int M... 不正确。你的意思是int PopulateRandom(int array[M * N])
  • 对不起,你指的是哪一行?
  • @Shoaib Ahmed 二维数组在哪里?:)

标签: c arrays function output


【解决方案1】:

编译器抱怨函数 PopulateRandom 的隐式声明,因为您在之前声明/定义它之前调用它。在PrintArray2D 中调用它之前,只需定义PopulateRandom。 对此,您可能会发现 this 很有趣。 第二个问题可能是由这行代码引起的:

while(i = 0; i < M*N; i++)

如果您想连接更多条件,您应该使用逻辑与 (&&) 或/和 逻辑或 (||)。

您使用此 while 循环的方式不正确。 while 循环不是 for 循环,语法不同。

for循环语法:

for (init; condition; increment)

while循环语法:

while (cond1 && cond2 || cond3)

我能看到的其他问题是:

  • 开关盒: 您应该发送default,以防万一。 此外,您总是必须破案。你没有破坏case 1

switch statement 的一般语法:

switch  (x)
{
      case 1 :
             break;

      case n :
              break;

      default:
              break;
}
  • PopulateRandom:

    int array[int M * int N]

    应该是

    int array []

    int *array

【讨论】:

    【解决方案2】:

    在这个函数中

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

    变量rowcolumni未初始化

    int array[M*N], row, column, i;
                    ^^^^^^^^^^^^^^
    

    然而,它们在循环中相应地使用

    while(row <= M && column <= N){
    

    在这个表达式语句中

    array[i] = PopulateRandom(array[M * N]);
    

    此外,在此语句中,在尚未声明的函数调用后缀表达式中使用了名称 PopulateRandom。此外,在这个表达式array[M * N] 中,试图访问数组之外​​的内存。

    不清楚这个神奇的数字 4 在这个语句中的含义

    if(column == 4){
                ^^^
    

    另外,这个带有未初始化变量i的return语句的含义是完全不清楚的

    return array[i];
    

    这个函数声明

    int PopulateRandom(int array[int M * int N]){
        int i;
    
        while(i = 0; i < M*N; i++){
        array[i] = rand() % (M*N);
        }
        return array[i];
    }
    

    无效。你可以写例如

    int PopulateRandom(int M, int N, int array[M * N]){
    //...
    

    同样不清楚return语句的目的

    return array[i];
    

    试图返回一个不存在的数组元素。

    在标签 case 1: 的 switch 语句中,您应该附加 break 语句。

    循环可以写成例如下面的方式

    do
    {
        printf("If you would like to search an array, enter 1 \n: ");
        //                                  ^^^ 
        printf("If you would like to exit, enter 0 \n: ");
    
        option = 0;
        scanf("%d", &option);
    
        switch( option )
        {
            case 1: 
            {
                printf("Enter two numbers M and N: ");                          
                scanf("%d %d", &M, &N);
                int a[M][N];
                PrintArray2D( M, N, a );
                break;
            }
        }
    } while ( option != 0 );
    

    函数本身可以写成如下演示程序所示

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void PopulateRandom( int m, int n, int a[static const  m * n] )
    {
        const int N = m * n;
    
        srand( ( unsigned int )time( NULL ) );
    
        for( int i = 0; i < N; i++ ) a[i] = rand() % N; 
    }
    
    void PrintArray2D( int m, int n, int a[static const m * n] )
    {
        PopulateRandom( m, n, a );
    
        for ( int i = 0; i < m; i++ )
        {
            for ( int j = 0; j < n; j++ )
            {
                printf( "%3d ", a[i * n + j] );
            }
            printf( "\n" );
        }
    }
    
    int main(void) 
    {
        int m = 2;
        int n = 3;
        int a[m * n];
    
        PrintArray2D( m, n, a );
    
        return 0;
    }
    

    程序输出可能看起来像

      1   2   2 
      3   5   0 
    

    【讨论】:

      【解决方案3】:

      这是我在这个论坛的第一个答案。对我的解释或更正要温柔。祝你今天过得愉快 ! (只是评论 prog 更简单)

      #include <stdio.h>
      #include <stdlib.h>
      /*
      -You need to use prototype or just set function "int PopulateRandom" before "int PrintArray2D"
      -Try to change your way to process at this application, it's really so complicated.
      -Try to use debug for understand what is going on your program.
      */
      
      int PrintArray2D(int M, int N){
          int array[M*N], row, column, i; // Use "static int" for row, column and i for auto init at 0, or do it manually. 
      
          while(row <= M && column <= N){ // Like i have said in top, you use variable but they're not initialized
          for(row = 0; row <= M; row++){
              for(column = 0; column <= N; column++){
                  array[i] = PopulateRandom(array[M * N]); // use directly "array[i] = (int) rand();" for change with random value
                  printf("%d", array[i]); // you can directly print a random value, you save value, but when you leave this function, your array is destroy.
                  if(column == 4){
                      break;
                  }
              }
              column = 0;
              printf("\n");
              }
          }
          return array[i]; // return is useless for your usage, becaus you don't catch return value in your main. (void PrintArray2D if no return)
      }
      
      /*
      -Your function need other params : int array[], int M, int N (don't forget to send M and N when you call this function)
      -You need to change your algo, because this function take M and N for change just ONE element of this array.
      -This function is useless in this context, check comment in "PrintArray2D" for more informations
      */
      int PopulateRandom(int array[int M * int N]){
          int i;
      
          while(i = 0; i < M*N; i++){ // It's a FOR and not WHILE
          array[i] = rand() % (M*N);
          }
          return array[i];
      }
      
      /*
      -Duplicate code isn't good, you can use printf and scanf just one time, just after your while, and set option = 1 before.
      -Donc use case, but just an if, because your condition in your while check the value.
      */
      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);
          }
      }
      

      【讨论】:

      • 函数前有注释,SWITCH没用,只能用IF。 while (option != 0){ if (option == 1){
      • 好吧,我的错。我今天早上很早就读到了,有点困惑!我要删除我的评论,因为它对任何人都没有用
      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 2019-01-25
      • 2021-12-22
      • 1970-01-01
      • 2018-07-15
      • 2013-06-17
      相关资源
      最近更新 更多