Void函数返回average,max,min,并将数组和输入数作为参数

问题描述 投票:-2回答:2

所以我试图创建一个程序,从void函数返回平均值,最小值和最大值。我无法真正看到代码有什么问题,我希望有人可以提供帮助。编译器没有发现任何错误或警告,但是当我运行程序时,我得到“进程退出,返回值为3221225477”。问题似乎在于我创建的功能。提前致谢。

    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;
         }

         }
c void
2个回答
0
投票
    avg=0;
    ...
    *avg=sum/plithos;

崩溃你的程序。你可能想写*avg = 0


0
投票

循环太短,最大和最小跟踪是从前到后的。以下是该部分的建议编辑:

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
© www.soinside.com 2019 - 2024. All rights reserved.