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