Trouble with sqrt ()

This is a discussion on Trouble with sqrt () within the C Programming forums, part of the General Programming Boards category; Here's a simple program that is to be used for calculating Sum, Average and Standard Deviation. But it doesn't work. ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    47

    Trouble with sqrt ()

    Here's a simple program that is to be used for calculating Sum, Average and Standard Deviation.

    But it doesn't work. There's a problem with the sqrt function. I hear it returns a double value. But then there will be a pointer problem I guess. Pointer variables have to be int. (just guessing)

    Here's the program:

    Code:
    /*
    Write a function that receives 5 integers and returns the sum, average and standard
    deviation of these numbers. Call this function from main() and print the results in main().
    */
    
    
    #include<stdio.h>
    #include<math.h>
    main()
    {
    	int a, b, c, d, e, sum, avg, sd;
    	int calc (int a, int b, int c, int d, int e, int *sum, int *avg,int *sd);
    
    	printf("Enter Five Numbers:");
    	scanf ("%d %d %d %d %d", &a, &b, &c, &d, &e);
    
    	calc (a, b, c, d, e, &sum, &avg, &sd);
    	
    	printf("Sum=%d", sum);
    	printf("Average=%d", avg);
    	printf("Standard Deviation=%lf", sd);
    
    }
    
    calc (int a, int b, int c, int d, int e, int *sum, int *avg, int *sd)
    {
    	int calc_sd;
    
    	*sum=a+b+c+d+e;
    	*avg=(a+b+c+d+e)/5;
    	
    	/*To calculate Standard Deviation, first we have to calculate
    	the mean average. Here n=5 .. Five Numbers and then calculate
    	Standard Deviation by this formula:
    	sqrt(((x1-mean)(x2-mean)....(xn-mean))/total numbers)*/
    	
    	calc_sd = ((a- *avg)*(b- *avg)*(c-*avg)*(d-*avg)*(e-*avg))/5;
    	*sd= sqrt(calc_sd); //sqrt function must have only one variable in those braces. :(
    }

    Also, one more thing...

    I used GCC, and the program won't compile on it.
    It gave me warning / errors I never knew.

    But then I tried MS Visual C++ IDE..it gives me warnings and the program compiles fine.
    Except that the Standard Deviation thing doesn't work out properly..

    So is it like GCC won't compile the programs if there are warnings?

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    No, an object pointed to by a pointer does not have to be an int. The pointer itself is an int though.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Try using
    Code:
    cc blahblah.c -lm

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    Quote Originally Posted by OnionKnight
    No, an object pointed to by a pointer does not have to be an int. The pointer itself is an int though.

    Okay..so do you mean that changing it to double will help?

    Also give some more inputs on using the sqrt function.

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    The reason you're getting errors is because GCC doesn't link the math library to your program automatically, you have to specify the -lm option when compiling as demonstrated above.
    You don't have to change your ints if you're happy with them.

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    You can have a pointer to any data type - even structs - just change your
    function parameters to double *sum, double *avg (etc.) Also, in main, the
    corresponding variables (sum, avg etc.) should be doubles as well.

    Other things:

    1. main returns int, see my sig.
    2. your calc function should be prototyped before main , not in it.
    3. Perhaps it would be a better idea to read your numbers into an array, as
    opposed to 5 separate variables
    4. sqrt () accepts a double parameter as well

    5. >>You don't have to change your ints if you're happy with them.

    why? If I was writing this, I would use doubles exclusively, using ints is just
    going to involve type casts and possible data loss - i'd read in 5 doubles and
    determine their average and use sqrt - keeping all the types the same.
    Last edited by Richie T; 09-02-2006 at 11:40 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by Richie T
    1. main returns int, see my sig.
    Unadorned main, while bad form, defaults to int, so it is technically okay. Actually since any unprototyped function defaults to int foo(...), his calc() is supposed to return an int, but doesn't. That's bad. Make it a void function.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    C99 is for the most part upward-compatible with C90, but is stricter in some ways; in particular, a declaration that lacks a type specifier no longer has int implicitly assumed. The C standards committee decided that it was of more value for compilers to diagnose inadvertent omission of the type specifier than to silently process legacy code that relied on implicit int. In practice, compilers are likely to diagnose the omission but also assume int and continue translating the program.
    From Wikipedia

    And I agree with them!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Yeah, but both his compilers default to C89 mode, so.

    And I'm certainly not advocating using unprototyped and untyped functions, if that's what you're implying.
    Last edited by zx-1; 09-02-2006 at 11:53 AM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > printf("Standard Deviation=%lf", sd);
    You're trying to print an int as a float (not that %lf is a valid format, just %f will do)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>Yeah, but both his compilers default to C89 mode, so.

    Be that as it may, it's still a good idea for the OP to be aware of potential issues
    with his code - he should write standard, portable code, not whatever his
    compiler lets him get away with. Granted, C99 isn't very widely supported,
    but it's the most up to date standard we have, so it's foolish to C89 forever,
    especially when it allows poor code like this. I've nothing further to add, unless
    the OP has more questions.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    The Standard deviation formula is not right. Follow the link to find the Formula http://en.wikipedia.org/wiki/Standard_deviation
    Following the right formula the code looks something as follow
    Code:
    /*
    Write a function that receives 5 integers and returns the sum, average and standard
    deviation of these numbers. Call this function from main() and print the results in main().
    */
    
    
    #include<stdio.h>
    #include<math.h>
    
    int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);
    
    int main()
    {
    	float a, b, c, d, e, sum=0.0, avg=0.0;
        float sd=0.0;
    
    	printf("Enter Five Numbers:");
    	scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
    
    	calc (a, b, c, d, e, &sum, &avg, &sd);
    	
    	printf("Sum=%f", sum);
    	printf("Average=%f", avg);
    	printf("Standard Deviation=%f", sd);
    
    
    	getchar();
    	return 0;
    }
    
    calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)
    {
        float Calc=0.0;
    
        *sum = a+b+c+d+e;
        *avg = *sum / 5.0;
        
        Calc += ( a - *avg) * ( a - *avg);
        Calc += ( b - *avg) * ( b - *avg);
        Calc += ( c - *avg) * ( c - *avg); 
        Calc += ( d - *avg) * ( d - *avg);
        Calc += ( e - *avg) * ( e - *avg);
        
        *sd = sqrt((double)Calc/5.0);
    }
    
    /*my output
    Enter Five Numbers:1.0
    2.0
    3.0
    4.0
    5.0
    Sum=15.000000Average=3.000000Standard Deviation=1.414214
    */

    NOTE*: use array for these sort of problems

    ssharish2005

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    I am learning and haven't yet dealt with arrays..so I have to make the program without arrays for the time being.

    @ssharish2005

    Thanks for pointing it out. (Logic is incorrect)
    I think I forgot to take the square of the terms.

    I will change my Logic accordingly now.

    Also, I was taught that its okay to declare the function prototype inside main.
    You fellas seem to do it the other way..declaring it outside main.
    I wonder if that causes much of a difference.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    @Salem
    Thanks for pointing it out.
    I had read somewhere that %lf is the format specifier for double!
    Now I verified whether that was correct.
    I understand now that the format specifier for double is %e and %Lf is for Long Double!

    @Richie T and zx-1
    Talking about C89 and C90...
    Hehe.your talks are abysmal for me.
    Not sure I understand that!
    I am also using Microsoft Visual Studio C++ by the way.

  15. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I am also using Microsoft Visual Studio C++ by the way.
    Which version? There are about 3 important ones out now. MSVC 6 (quickly fading), MSVC .NET 2003 (widely used), and MSVC .NET 2005 (very new).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to sqrt
    By shamma in forum C Programming
    Replies: 1
    Last Post: 05-09-2009, 01:42 PM
  2. Having trouble with the sqrt() function.
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2005, 12:08 PM
  3. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM
  4. sqrt
    By modec in forum C Programming
    Replies: 3
    Last Post: 04-16-2003, 07:19 AM
  5. how sqrt ?
    By sambs1978 in forum C Programming
    Replies: 3
    Last Post: 09-20-2001, 08:14 AM

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