can anyone help me to solve this problem?

the input is a number n, and a set of n array.
the output are maximum array, minimum array, rate, and the sds.

this is my code:


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

int swapp(int m,int n);
int sortt(int m,int n);
double sb(int m, int n);
double d;
typedef double array;
array arr[100000];

int main()
{
int a,b;
double sum,sbs;

scanf("%d", &a);

for(b=1; b<=a; b++)
{
scanf("%lf",&arr[b]);
sum=sum+arr[b];
}

sortt(1,a);
d= sum/a;
sbs= sqrt((sb(1,a))/(a-1));
printf("%.2lf %.2lf %.2lf %.2lf\n", arr[1],arr[a],d,sbs);
return 0;
}


double sb(int m, int n)
{
int c;
double sigma;
double total;
for (c=m; c<=n; c++)
{
sigma=pow(arr[c]-d,2);
total= total + sigma;
}
return total;
}


int swapp(int m, int n)
{
double tmp;
tmp=arr[m];
arr[m]=arr[n];
arr[n]=tmp;
return 0;
}


int sortt(int m, int n)
{
double piv;
int l,r;

if (n>m)
{
piv=arr[(m+n)/2];
l=m;
r=n;

while (l<=r)
{
while (arr[l]<piv)
l++;
while (arr[r]>piv)
r--;

if (l<=r)
{
swapp(l,r);
l++;
r--;
}
}

sortt(m,r);
sortt(l,n);
}
return 0;
}



input:
10
297536.26
526260.62
828177.56
-45559.92
978715.10
383672.24
467737.00
67692.93
-765057.71
790913.04


output:
-765057.71 978715.10 353008.71 nan



what the hell "nan"?
what should i get is:
-765057.71 978715.10 353008.71 510618.90


can you tell me why i got "nan"?
are my variables wrong?



When I compile your code it tells me:

1>main.cpp(40): warning C4700: uninitialized local variable 'total' used
1>main.cpp(21): warning C4700: uninitialized local variable 'sum' used


You probably shouldn't ignore such warnings.

1
2
3
4
5
6
7
8
9
10
    int a,b;
    double sum,sbs;

    scanf("%d", &a);

    for(b=1; b<=a; b++)
    {
        scanf("%lf",&arr[b]);
        sum=sum+arr[b];
    }


In the above code fragment, sum has an indeterminate value. Perhaps you should set it to some reasonable value (such as 0.0) before using it.
Topic archived. No new replies allowed.