[英]C-Void functions won't execute
我需要使用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]);
}
}
您没有正确调用该函数。 有了它,您只是在声明功能是什么,而不是实际调用它们。 您想像打电话一样打电话给他们
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);
一切似乎都在工作,但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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.