Home > Blockchain >  Format '%f' expects argument of type 'double', but argument 2 has type 'flo
Format '%f' expects argument of type 'double', but argument 2 has type 'flo

Time:12-14

I am new to c and I am trying to make a calculator that asks if you want to calculate the standard deviation of a set or the average of a set. However when I run the code I get the error seen in the picture. Error screen. I don't know how to correct it. I believe I have also made some mistakes throught the code so if you see anything else a miss would you mind pointing it out. Thank you.

#include <stdio.h>
#include <math.h>
float StandardDev(float data[]);
float Average(float data[]);

float Average(float data[]) {
    int n;
    float num[100], sum = 0.0, avg;

    avg = sum / n;
    return 0;
}

float StandardDev(float data[]){
    float sum = 0.0, mean, SD = 0.0;
    int i, n;
    for (i = 0; i < n;   i) {
        sum  = data[i];
    }
    mean = sum / n;
    for (i = 0; i < n;   i) {
        SD  = pow(data[i] - mean, 2);
    }
    return sqrt(SD / 10);
}

int main()
{  
    int n, i;
    float num[100], sum = 0.0, c;
    printf("Enter the numbers of elements: ");
    scanf("%d", &n);

    while (n > 100 || n < 1) {
        printf("Error! number should in range of (1 to 100).\n");
        printf("Enter the number again: ");
        scanf("%d", &n);
    }

    for (i = 0; i < n;   i) {
        printf("%d. Enter number: ", i   1);
        scanf("%lf", &num[i]);
        sum  = num[i];
    }
    printf("Do you want to calculate the Standard Deviation(1) of   a set or find the   Average(2) of a set? ");
    scanf("%u", &c);
    if(c==1){
        printf("The Standard Deviation of your set is %f", StandardDev);
    }
    else{
        printf("The average of your set is %f", Average);
    }
    return(0);
}

CodePudding user response:

You declared an array with the element type float

float num[100], sum = 0.0, c;

So you need to use the conversion specifier %f instead of %lf. The last is designed for objects of the type double

scanf("%f", &num[i]);

In this call of scanf

scanf("%u", &c);

you are using the conversion specifier %u designated for objects of the type unsigned int with an object of the type float

Declare the variable c like

unsigned int c;

In these calls of pruntf

    printf("The Standard Deviation of your set is %f", StandardDev);
    printf("The average of your set is %f", Average);

You are trying to output function pointers instead of results of the function calls.

It seems you mean something like the following

    printf("The Standard Deviation of your set is %f", StandardDev( num ));
    printf("The average of your set is %f", Average( num ) );

Pay attention to that the program will have undefined behavior at lwast because you are using uninitialized variables in the functions like for example

int n;
float num[100], sum = 0.0, avg;

avg = sum / n;
//...
  • Related