返回平均值,最大值,最小值并以数组和输入数量为参数的空函数

时间:2019-04-16 20:00:33

标签: c void

因此,我试图创建一个从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;
         }

         }

2 个答案:

答案 0 :(得分:0)

    avg=0;
    ...
    *avg=sum/plithos;

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

答案 1 :(得分: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
相关问题