Thread: Finding the square root! Not Working!

  1. #1
    Registered User
    Join Date
    Sep 2003

    Exclamation 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.

    #include <stdio.h>
    #include <math.h>
    #define size 29
    void doBubbleSort(double [], int);
    double  calTotalNum(double []);
    double sqrt(double var);
    	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;
    	doBubbleSort(num, n);
    	totalNum = calTotalNum(num);
    	mean = totalNum/size;
    	stdDev= sqrt(var);
    	printf("             Statistical report of Data%s\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);
    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. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Cape Town
    Uncomment the line that says :

    double sqrt( double var );

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

  3. #3
    Registered User
    Join Date
    Jan 2003
    dont forget to compile with the -lm
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    Registered User
    Join Date
    Sep 2003
    i got rid of this line:
    double sqrt( double var );

    its still not working though
    Last edited by Lah; 09-14-2003 at 07:32 AM.

  5. #5
    Registered User
    Join Date
    Sep 2003
    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. #6
    Registered User
    Join Date
    Sep 2003
    thx for your help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Square Root Problem.
    By thekautz in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2008, 12:38 PM
  2. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. square root alternative
    By FH|RogueHunter in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2005, 10:17 PM
  5. square root
    By Noobie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2003, 11:01 AM