[英]C-Void functions won't execute
I need to print a set of random numbers in an array using an input between 0 and 50 to determine how many random ints to print. 我需要使用0到50之间的输入在数组中打印一组随机数,以确定要打印多少个随机整数。 Everything seems to be working but the void functions don't seem to be executing, leaving the array with all 0 values. 一切似乎都在工作,但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]);
}
}
You are not properly calling the function. 您没有正确调用该函数。 The way you have it, you are just providing a declaration of what the functions are, not actually calling them. 有了它,您只是在声明功能是什么,而不是实际调用它们。 You want to call them just as you call 您想像打电话一样打电话给他们
error = check_error(size);
Except with a void function, there is no return value to store in a variable. 除void函数外,没有返回值可存储在变量中。 Change 更改
void initialize_array(int list[], int size);
to 至
initialize_array(list, size);
and 和
void print_array(int list[], int size);
to 至
print_array(list, size);
Everything seems to be working but the void functions don't seem to be executing, leaving the array with all 0 values. 一切似乎都在工作,但void函数似乎未在执行,从而使数组具有所有0值。
There are a few issues with the code. 代码有一些问题。
1) Functions prototypes are not declared properly: 1)函数原型未正确声明:
int check_error();
void initialize_array();
void print_array();
Function declarations have to contain the return value and the type of the parameters: 函数声明必须包含返回值和参数的类型:
int check_error(int input);
void initialize_array(int list[],int listSize);
void print_array(int array1[],int arraySize);
2) The array functions are not called properly: 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) In the code you print list[0]
and list[2]
. 3)在代码中打印list[0]
和list[2]
。 If the input is less than 3
then the list[2]
does not contain the generated value. 如果输入小于3
则list[2]
不包含生成的值。
4) You may consider seeding your random generator. 4)您可以考虑为随机生成器播种。
srand (time(NULL));
Otherwise, the list
will will always get the the same numbers. 否则, list
将始终获得相同的数字。
The modified code: 修改后的代码:
#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]);
}
}
Output: 输出:
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.