# Thread: Finding the square root! Not Working!

1. ## Finding the square root! Not Working!

What i want to do is calculate the square root of the variance in a statistical program i am creating. Anyways here is some of the code of my program, i have already sorted the data using a sort and have done all the other calculations with other functions that i havent posted in this extract of code but i am having trouble finding the square root of (var). In this case since i havent worked it out properly i have given var a permanently value.

Code:
```#include <stdio.h>
#include <math.h>
#define size 29
void doBubbleSort(double [], int);
double  calTotalNum(double []);
double sqrt(double var);

main()
{

double num[] = {15.5, 17.2, 17.4, 15.2, 15.3,
17.8, 17.5, 18,0, 18.1, 19.5, 17.5, 18.0, 19.5,
17.5, 15.9, 20.0, 16.3, 14.5, 20.5, 20.5, 17.3,
15.5, 15.8, 18.3, 16.5, 16.6, 15.5, 16.2, 16.0};
double totalNum, mean, median, lowerQuart, upperQuart, iqr, var, stdDev;
int i, n;

n=sizeof(num)/sizeof(double);

doBubbleSort(num, n);

totalNum = calTotalNum(num);
mean = totalNum/size;
median=(num[(size+1)/2]);
lowerQuart=(num[(size+3)/4]);
upperQuart=(num[3*size/4]);
iqr=((num[3*size/4]-num[(size+3)/4]));
var=4.5;
stdDev= sqrt(var);

printf("\n\n");
printf("             Statistical report of Data%s\n");
printf("------------------------------------------------------\n");
for(i = 1; i<n; ++i) printf("%6.1lf\n", num[i]);
printf("mean temperature      : %5.2lf.\n", mean);
printf("median temperature    : %5.2lf.\n", median);
printf("lower quartile value  : %5.2lf.\n", lowerQuart);
printf("upper quartile value  : %5.2lf.\n", upperQuart);
printf("inter quartile range  : %5.2lf.\n", iqr);
printf("variance              : %5.2lf.\n", var);
printf("standard deviation    : %5.2lf.\n", stdDev);

printf("\n");
}```
Can someone please show me where i am going wrong, i have tried and tried to get it working but i keep getting errors.

2. Uncomment the line that says :

double sqrt( double var );

You are already including "math.h" so there's no need for a prototype.

3. dont forget to compile with the -lm

4. i got rid of this line:
double sqrt( double var );

its still not working though

5. Compiling with math library is important, so use the -lm.
Otherwise it will give some unpredictable result, I have tried it once (by mistake) and I was totally surprised what is the program doing. Maybe there was more than one mistake

And it's good to return some value from int main().
I personally use gcc -W -Wall to turn all warnings on, it will report such things.

Or use the pow() function with exponent 0.5

6. thx for your help guys