【问题标题】:C-Void functions won't executeC-Void 函数不会执行
【发布时间】:2018-02-26 03:45:00
【问题描述】:

我需要使用 0 到 50 之间的输入在数组中打印一组随机数,以确定要打印多少随机整数。一切似乎都在工作,但 void 函数似乎没有执行,使数组的值全部为 0。

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

int check_error();
void initialize_array();
void print_array();

int main(void){
        int list[50];   //initializing arrays
        int size,error; //initializing size

        printf("Enter the size of an input");
        scanf("%d",&size);
        error = check_error(size);
        while(error == 0){
                printf("Invalid input enter the size of the input: ");
                scanf("%d",&size);
                error = check_error(size);
                }
        void initialize_array(int list[], int size);

        printf("%d",list[2]);

        void print_array(int list[], int size);

        printf("\n\nDONE %d",list[0]);

        return 0;
        }


int check_error(int input){
        if(input < 1 || input > 50)
                return 0;
        else
                return 1;
        }

void initialize_array(int list[],int listSize){
        int i;
        for(i=0;i<=listSize;++i){
                list[i] = (rand()%10);
                }
         }


void print_array(int array1[],int arraySize){
        int i;
        for(i=0;i<=arraySize;++i){
                printf("%d, ",array1[i]);
                }
        }

【问题讨论】:

  • 那是因为你没有给他们打电话?
  • void initialize_array(int list[], int size); 不是调用,而是函数的声明。将其与您使用 printfscanf 的方式进行比较。

标签: c arrays void


【解决方案1】:

您没有正确调用该函数。你拥有它的方式,你只是提供函数是什么的声明,而不是实际调用它们。 你想打电话给他们,就像你打电话一样

error = check_error(size);

除了 void 函数,没有返回值存储在变量中。 改变

void initialize_array(int list[], int size);

initialize_array(list, size);

void print_array(int list[], int size);

print_array(list, size);

【讨论】:

    【解决方案2】:

    似乎一切正常,但 void 函数似乎没有执行,数组中的值全部为 0。

    代码存在一些问题。

    1) 函数原型没有正确声明:

    int check_error();
    void initialize_array();
    void print_array();
    

    函数声明必须包含返回值和参数类型:

    int check_error(int input);                  
    void initialize_array(int list[],int listSize);
    void print_array(int array1[],int arraySize);
    

    2) 数组函数调用不正确:

    //void initialize_array(int list[], int size); // this is the function declaration
    initialize_array(list, size);                  // this is the function call
    
    //...
    //void print_array(int list[], int size);     // this is the function declaration
    print_array(list, size);                      // this is the function call
    

    3) 在代码中打印list[0]list[2]。如果输入小于3,则list[2] 不包含生成的值。

    4) 您可以考虑播种随机生成器。

    srand (time(NULL));
    

    否则,list 将始终获得相同的数字。

    修改后的代码:

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    // function declarations have to contain return value and type of the parameters
    int check_error(int input);                  
    void initialize_array(int list[],int listSize);
    void print_array(int array1[],int arraySize);
    
    int main(void){
        int list[50];   //initializing arrays
        int size,error; //initializing size
    
        //srand (time(NULL));   // consider seeding the random generator
    
        printf("Enter the size of an input: \n");
        scanf("%d",&size);
    
        error = check_error(size);           // OK!
    
        while(error == 0){
                printf("Invalid input enter the size of the input: \n");
                scanf("%d",&size);
                error = check_error(size);
        }
    
        //void initialize_array(int list[], int size); // this is the function declaration
        initialize_array(list, size);                  // this is the function call
    
        printf("list[2]= %d\n",list[2]);
    
        //void print_array(int list[], int size);     // this is the function declaration
        print_array(list, size);                      // this is the function call
    
        printf("\n\nDONE: list[0]= %d\n",list[0]);
    
        return 0;
    }
    
    int check_error(int input){
        if(input < 3 || input > 50)   // since you print list[2] input should be 3 
            return 0;
        else
            return 1;
    }
    
    void initialize_array(int list[],int listSize){
        int i;
        for(i=0;i<listSize;++i){                  //  style: in the loop typically we use < not <= operator 
            list[i] = (rand()%10);
        }
    }
    
    void print_array(int array1[],int arraySize){
        int i;
        printf("List: ");
        for(i=0;i<arraySize;++i){
            printf("%d, ",array1[i]);
        }
    }
    

    输出:

    3
    Enter the size of an input: 
    list[2]= 7
    List: 3, 6, 7, 
    
    DONE: list[0]= 3
    
    4
    Enter the size of an input: 
    list[2]= 7
    List: 3, 6, 7, 5, 
    
    DONE: list[0]= 3
    

    【讨论】:

      猜你喜欢
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 2022-06-19
      • 2011-09-24
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多