"inf" was displayed at runtime

Asked 2 years ago, Updated 2 years ago, 26 views

I'd like to create a program that inputs the number of data, reads the number of points of data, and displays the number of points and deviation values.
The compilation was done correctly, but for some reason the deviation value is displayed as inf.Why?

source code

#include<stdio.h>
/* Enter element number size real number array a in order */
void readDoubleArray (double a[], int size)
{
    inti;
    for(i=0;i<size;i=i+1){
        printf("%dth?", i+1);
        scanf("%lf", & a[i]);
    }
}

/* Output real number array a of element number size in order */
void printDoubleArray (double a[], int size)
{
    inti;
    for(i=0;i<size;i=i+1){
        printf("%f", a[i]);
    }
    printf("\n");

}

/* Store the mean of the real number array a of the number of elements size in *ave and the variance in *var*/
voidave_var_double_array(double a[], int size, double*ave, double*var, double*hyoujunnhennsa, double*hennisati)
{
    inti;
    double sum = 0.0, dsum = 0.0;
    for(i=0;i<size;i=i+1){
        sum = sum + a[i]; 
       *ave=sum/size;
    }
    for(i=0;i<size;i=i+1){
        dsum=dsum+(a[i]-*ave)*(a[i]-*ave); 
       *var=dsum/size;
    }for(i=0;i<size;i=i+1){
        *var=(*hyoujunnhennsa)*(*hyoujunnhennsa);
        *hensati=((10*(a[i]-*ave))/(*hyoujunnhennsa))+50;
    }    
}


int main (void)
{
    double a[100], ave, var, hyoujunnhennsa, hennsati;
    int num;
     
    printf("Number of data? ");
    scanf("%d", & num);

    readDoubleArray(a,num);
    
    ave_var_double_array(a, num, & ave, & var, & hyoujunnhennsa, & hennsati);
 
    printf("Score:");
    printDoubleArray(a,num);
    printf("Deviation value: %.1f\n", hensati);

    return 0;
}

Run Results

$./a.out
Number of data?5
Number one? 40
Second? 56
Third?92
4th? 78
Number five? 67
Score: 40.00000056.000092.000078.000067.000000 
Deviation value: inf

"I have experienced core dumps and 0 display of everything, but this is my first time to see ""inf"" display, so I have heard of it."
What is wrong with my program?I look forward to hearing from you.

c

2022-09-29 20:27

1 Answers

inf represents infinity.(infinity)

I think 0 division is occurring because hyoujunnhennsa declared in the main function is passed to the function without initializing and is used internally for division.


2022-09-29 20:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.