简体   繁体   中英

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.

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.

You should use incount instead of the magic number.

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.

Also note that

int incount; incount = 5.0;

is not the right way to set initial value of gloval variables. It should be

int incount = 5.0;

Same thing applies to the global 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.

I just added

int i;

inside the function, and voila.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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