Finding the square root! Not Working!

This is a discussion on Finding the square root! Not Working! within the C Programming forums, part of the General Programming Boards category; What i want to do is calculate the square root of the variance in a statistical program i am creating. ...

  1. #1
    Lah
    Lah is offline
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    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.

    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. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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
    Posts
    115
    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
    Lah
    Lah is offline
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    i got rid of this line:
    double sqrt( double var );

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

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    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
    Lah
    Lah is offline
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21