Simple double sqrt, pow

This is a discussion on Simple double sqrt, pow within the C Programming forums, part of the General Programming Boards category; This is a simple programming, but I never really got how to do this. Code: printf("Enter 5 doubles: "); scanf("%lf ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Simple double sqrt, pow

    This is a simple programming, but I never really got how to do this.

    Code:
    printf("Enter 5 doubles: ");
    scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &e);
    printf("v[] = {%11.3lf %11.3lf %11.3lf %11.3lf %11.3lf}\n\n", a,b,c,d,e);
    
    length = double sqrt((double pow(double a,2) + double pow(double b,2) + double pow(double c,2) + double pow(double d,2) + double pow(double e,
    2)));
    I'm trying to get the sum of the squares of a,b,c,d, and e, then square root that answer, but I never knew where to put the doubles and where not to (I always deleted, test, deleted, test, etc.). Can someone help me please? Much thanks!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    pow and sqrt are overloaded to take doubles, floats and all sorts of integers even. So your program might be written:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void) {
        double a, b, c, d, e;
        fputs("Enter 5 decimals: ", stdout);
        scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &e);
        
        double sum_of_powers = pow(a, 2) 
                             + pow(b, 2)
                             + pow(c, 2) 
                             + pow(d, 2)
                             + pow(e, 2);
                             
        double length = sqrt(sum_of_powers);
        
        printf("The square root of the sum is %lf\n", length);
        
        return 0;
    }
    Notice the lack of typecasting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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