[英]Void function that returns average,max,min,and takes as parameter an array and the number of inputs
So I'm trying to create a program that returns the average ,min and max from a void function.所以我正在尝试创建一个程序,该程序从 void 函数返回平均值、最小值和最大值。 I can't really see what is wrong with the code and I'm hoping someone can help.The compiler doesn't find any error or warning but when i run the program i get "Process exited with return value 3221225477".我真的看不出代码有什么问题,我希望有人能帮忙。编译器没有发现任何错误或警告,但是当我运行程序时,我得到“进程退出,返回值 3221225477”。 The problem seems to be in the function i created.问题似乎出在我创建的函数中。 Thanks in advance.提前致谢。
void emporeuma(double array[], int plithos, double* avg, double* max,
double* min, int* plit)
{
int j;
double sum;
avg=0;
sum=0;
*plit=plithos;
for(j=0;j<plithos-1;j++){
sum=sum + array[j];
}
*avg=sum/plithos;
*min=array[0];
*max=array[0];
for(j=1;j<plithos-1;j++)
{
if (array[j]>*max)
{
array[j]=*max;
}
if (array[j]<*min)
{
array[j]=*min;
}
}
avg=0;
...
*avg=sum/plithos;
crashes your program.使您的程序崩溃。 You probably want to write *avg = 0
.你可能想写*avg = 0
。
The loop is one too short, and the max and min tracking is back-to-front.循环太短了,最大和最小跟踪是从后到前的。 Here is a suggested edit of that part:这是该部分的建议编辑:
for(j = 1; j < plithos; j++) { // extend to the last element
if (array[j] > *max) {
*max = array[j]; // update the max
}
if (array[j] < *min) {
*min = array[j]; // update the min
}
sum += array[j]; // ready to calculate avg
}
avg = sum / plithos; // average
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.