简体   繁体   English

我写了一个 C 程序来计算数组的标准偏差。 我的代码给出了错误的答案。 我究竟做错了什么?

[英]I wrote a C program to calculate the standard deviation of an array. My code is giving the wrong answer. What am I doing wrong?

The function that is commented out is my function, the other is the one I picked from internet.注释掉的函数是我的函数,另一个是我从网上挑的。 There are no errors.没有错误。 The code just asks for 5 numbers, then asks to operate one of 4 operations, here (s) being for standard deviation, and then runs that function.代码只要求输入 5 个数字,然后要求操作 4 个操作之一,这里 (s) 是标准差,然后运行该函数。

From what I've troubleshooted, it isn't reading the elements of the array.根据我的故障排除,它没有读取数组的元素。 It isn't adding, removing, or doing anything with them.它不是添加、删除或对它们做任何事情。 It just prints out a zero when asked to print something else in that function.当要求在该函数中打印其他内容时,它只是打印出一个零。

I'm a newbie, as is implied by the type of question I'm solving, but it'd be great if somebody could help me out here.我是一个新手,正如我正在解决的问题类型所暗示的那样,但如果有人能在这里帮助我,那就太好了。

Thanks!谢谢!



#include<stdio.h>
#include<math.h>

int arr[5];
    int curnum, i; i = 0;
    char op;

int incount; incount = 5.0;


int main();

int main(){

    for (i=0; i<incount; i++)
    {
        printf("Enter a number : ");
        scanf("%d", &arr[i]);
    }


    printf("What would you like to calculate? Average(a), Max(M), Min(m), Std. Dev.(s) : ");
    scanf(" %c", &op);

    float average(int arr[]);
    float std(int arr[]);

    switch (op){
        case 'a':
            printf("The average is %f \n", average(arr)); break;
        case 'M':
            printf("The Max number is %d \n", max(arr)); break;
        case 'm':
            printf("The Min num is %d \n", min(arr)) ; break;
        case 's':
            printf("the Standard Deviation is %f \n", std(arr)); break;
        default:
            printf("Invalid function \n"); break;
    }

    return 0;

}

float average(int arr[]){

    float netsum;
    netsum = 0;

    for (i=0; i<incount; i++)
    {
        netsum = netsum + arr[i];
    }

    return (netsum/5);

}

int max(int arr[]){

    int maxnum;
    maxnum = arr[0];


    for (i=0; i<incount; i++)
    {
        if (arr[i] > maxnum) maxnum = arr[i];        
    }

    return maxnum;
}

int min(int arr[]){

    int minnum;
    minnum = arr[0];

    for (i=0; i<incount; i++)
    {
        if (arr[i] < minnum) minnum = arr[i];        
    }

    return minnum;
}

// float std(int arr[]){

//     float currsq, sqsum, stdeviation; currsq = 0; sqsum = 0;

//     for (i=0; i<incount; i++){
//         currsq = arr[i] - average(arr);
//         currsq = currsq*currsq;
//         sqsum = sqsum + currsq;

//     }

//     stdeviation = pow (sqsum/incount, 0.5);

//     return stdeviation;

// }

float std(int arr[]) {
    float sum = 0.0, mean, SD = 0.0;
    int i;
    for (i = 0; i < 10; ++i) {
        sum += arr[i];
    }
    mean = sum / 10;
    for (i = 0; i < 5; ++i)
        SD += pow(arr[i] - mean, 2);
    return sqrt(SD / 10);
}

You are trying to dealing with 10 elements in your function std() while there are only 5.您正在尝试处理函数std() 10 个元素,而只有 5 个。

You should use incount instead of the magic number.您应该使用incount而不是幻数。

float std(int arr[]) {
    float sum = 0.0, mean, SD = 0.0;
    int i;
    for (i = 0; i < incount; ++i) {
        sum += arr[i];
    }
    mean = sum / incount;
    for (i = 0; i < incount; ++i)
        SD += pow(arr[i] - mean, 2);
    return sqrt(SD / incount);
}

Magic number is also used in the function average , but it didn't cause harm because 5 is used.函数average也使用了幻数,但没有造成危害,因为使用了5

Also note that还要注意的是

int incount; incount = 5.0;

is not the right way to set initial value of gloval variables.不是设置 gloval 变量初始值的正确方法。 It should be它应该是

int incount = 5.0;

Same thing applies to the global i .同样的事情适用于全局i

I'm sorry, absolutely noobie mistake.对不起,绝对是菜鸟错误。 As I said from my testing, it wasn't taking any values from the array.正如我在测试中所说,它没有从数组中获取任何值。 That was because I didn't create the Variable (i) inside the function that was being used in the for loop.那是因为我没有在 for 循环中使用的函数内部创建变量 (i)。

I just added我刚加

int i;

inside the function, and voila.在函数内部,瞧。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM