简体   繁体   English

我不确定为什么我的方差 function 返回 0.0000 0.0000

[英]I'm not sure why my variance function returns 0.0000 0.0000

My program runs, but my output should print the variance and the average.我的程序运行了,但我的 output 应该打印方差和平均值。 I was told as my input array sizes get larger and larger my average should get closer to 5000 and my variance should approach 8333333... Yet I am getting 0 for both.有人告诉我,随着我的输入数组大小变得越来越大,我的平均值应该接近 5000,而我的方差应该接近 8333333……但我得到的都是 0。 The only thing I can think of is that I'm filling my mydata array/memoryblock incorrectly, but I don't know another way to do it if not the way I have written.我唯一能想到的是我错误地填充了我的数据数组/内存块,但如果不是我写的那样,我不知道另一种方法。

#include <stdio.h> //printf
#include <stdlib.h> //malloc, calloc, realloc, free
#include <math.h> //pow

double average_variance(int data[], int numbers, double* varp){
    
    double sum = 0;
    //sum of array
    for(int i=0; i < numbers; i++){
        sum += data[i];
    }
    double average = sum/numbers;

    double squaresum = 0;
    //sum of squares
    for(int i=0; i < numbers; i++){
        squaresum += pow(data[i], 2);
    }

    //"hack" to return two values by using a pointer; otherwise you can't return two values in a function
    double variance = ((squaresum/numbers) - pow(average, 2));
    *varp = variance;
    
    return average;
}

int main(int argc, char **argv)
{
    int* mydata = NULL;
    int arraysize;
    
    while (1) 
    {
        printf("Enter array size (0 to end): "); scanf("%d", &arraysize);
        
        if (arraysize == 0)
        break;
        
        // 1. Add code to allocate memory and assign to the mydata // pointer. If mydata is already allocated, use realloc ...
        
if(arraysize != 0)
        {   
            //used calloc because in the next if statement I needed to see if values were stored in it or not 
            mydata = calloc(arraysize, sizeof(int));
            if (mydata == NULL) {return -1;}
            
            //if mydata array is already allocated use realloc to make a new memory block 
            if(mydata != 0)
            {
                mydata = realloc(mydata, arraysize * sizeof(int));
                if (mydata == NULL) {return -1;}
            }

        }
    
        // 2. Add code to put random numbers in the array
        for(; mydata < &mydata[-1]; mydata++)
        {
            int randnum = random() % 10000;
            mydata = &randnum;
        }
        
        
        //included
        double variance;
        double avg = average_variance(mydata, arraysize, &variance);
        printf("average = %f variance = %f\n", avg, variance);
    }
    free(mydata);

The array initialization code is broken:数组初始化代码被破坏:

        for(; mydata < &mydata[-1]; mydata++)
        {
            int randnum = random() % 10000;
            mydata = &randnum;
        }

Errors:错误:

  • mydata[-1] is not the last element of mydata (a'la Python) but it is an element just before the array. mydata[-1]不是mydata的最后一个元素(a'la Python),而是数组之前的一个元素。 This element does not exist, Undefined Behavior is invoked.该元素不存在,未定义的行为被调用。
  • the mydata is moved during the initiazation to a local variable in the loop. mydata在初始化期间移动到循环中的局部变量。 There is no hope for this construct to work.这个构造没有希望起作用。

Just do:做就是了:

    for (int i = 0; i < arraysize; ++i)
            mydata[i] = random() % 10000;

Moreover, the allocation code is needlessly convoluted.此外,分配代码是不必要的复杂的。 Just replace:只需更换:

if(arraysize != 0)
        {   
            //used calloc because in the next if statement I needed to see if values were stored in it or not 
            mydata = calloc(arraysize, sizeof(int));
            if (mydata == NULL) {return -1;}
            
            //if mydata array is already allocated use realloc to make a new memory block 
            if(mydata != 0)
            {
                mydata = realloc(mydata, arraysize * sizeof(int));
                if (mydata == NULL) {return -1;}
            }

        }

with:和:

    mydata = realloc(mydata, arraysize * sizeof *mydata);
    if (!mydata) return -1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么strtof总是输出0.0000? - Why does strtof always output 0.0000? 我的程序有问题吗? 我似乎只得到 0.0000 作为我的答案 - Is there something wrong with my program? I only seems to get 0.0000 as my answers 我的程序无限循环,我老实说不清楚为什么 - My program is looping infinitely, and I'm honestly not sure why 使用浮点数和双精度数时,c中的-0.0000是什么? - what is -0.0000 in c when using floats and double? 查找方差的函数返回0 - Function to find variance returns 0 C程序:编写了将int转换为float的代码,但这给了我0.0000,并将我的输入更改为一些奇怪的数字 - C program: Wrote code to convert int to float, but it's giving me 0.0000 and it's changing my input to some strange number 我尝试分解函数,但是不确定是否做对了 - I tried decomposing my function, but I'm not sure if I did it right 学校课程测试无法正确覆盖我的代码,我不确定为什么 - School program tests not covering my code correctly and I'm not sure why 我不确定为什么我在 C 中的代码会免费给我一个分段错误,有什么想法吗? - I'm not sure why my code in C is giving me a segmentation fault at free, any ideas? 我不确定为什么我的嵌套 while 循环在第一次迭代后停止 - I'm not sure why my nested while loop is stopping after the first iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM