Having an issue with the sqrt() function in c program

0 Adam Martin · September 22, 2015
Hey guys, listed below is my script. As the title says, when I compile the script it shows no errors, but when I execute it (as you can see if you execute it) the standard deviation is shown as sqrt(353.84) instead of actually putting the answer (which is 18.81).
Note that when I do compile it I do use -lm.

I am new to thenewboston, and to programming in general, so hello all! 

Cheers


#include <stdio.h>
#include <math.h>

int main(void)

{

//Input Data
float X1,X2,X3,X4,X5;
X1 = 60.00, X2 = 95.00, X3 = 73.00, X4 = 41.00, X5 = 84.00;
//Output Data
float Variance,Sum,SD, D1sqr, D2sqr, D3sqr, D4sqr, D5sqr, M,D1,D2,D3,D4,D5;
M = (X1 + X2 + X3 + X4 + X5)/5.0;
D1 = X1 - M;
D2 = X2 - M;
D3 = X3 - M;
D4 = X4 - M;
D5 = X5 - M;
D1sqr = D1*D1;
D2sqr = D2*D2;
D3sqr = D3*D3;
D4sqr = D4*D4;
D5sqr = D5*D5;
Sum = D1sqr + D2sqr + D3sqr + D4sqr + D5sqr;
Variance = (D1sqr + D2sqr + D3sqr + D4sqr + D5sqr)/5.0;
SD = sqrt(D1sqr + D2sqr + D3sqr + D4sqr + D5sqr)/5.0;

//Scores//
scanf("%f %f %f %f %f", &X1,&X2,&X3,&X4,&X5);
//Mean Equation//
scanf("(%f+%f+%f+%f+%f)/5.0 = %f",&X1,&X2,&X3,&X4,X5,M);
//Deviation Scores//
scanf("%f-M",X1);
scanf("%f-M",X2);
scanf("%f-M",X3);
scanf("%f-M",X4);
scanf("%f-M",X5);
//Squared Deviation//
scanf("%f*%f",D1,D1);
scanf("%f*%f",D2,D2);
scanf("%f*%f",D3,D3);
scanf("%f*%f",D4,D4);
scanf("%f*%f",D5,D5);
//Sum//
scanf("%f+%f+%f+%f+%f",D1sqr,D2sqr,D3sqr,D4sqr,D5sqr);
//Variance//
scanf("(%f+%f+%f+%f+%f)/5.0 = %f",D1sqr,D2sqr,D3sqr,D4sqr,D5sqr,Variance);
//Standard Deviation//
scanf("sqrt(Variance)");

printf("Score \t-\t Mean \t=\t Deviation Score \t\tSquared Deviation\n");
printf("(X)\t\t(M)\t\t(X-M)\t\t\t\t(X-M)squared\n");
printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X1,M,D1,D1sqr);
printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X2,M,D2,D2sqr);
printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X3,M,D3,D3sqr);
printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X4,M,D4,D4sqr);
printf("%.2f\t\t%.2f\t\t%.2f\t\t\t\t%.2f\n",X5,M,D5,D5sqr);
printf("\t\t\t\t\t\tSum: %.2f\n",Sum);
printf("Variance: = %.2f/5 = %.2f\n",Sum,Variance);
printf("Standard Deviation = SD = sqrt(%.2f)\n",Variance);

return 0;

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Adam Martin · September 25, 2015
Hey Krootushas, 

I ended up figuring it out. I just changed printf("SD = %.2f\n",SD); and it resolved it. 

thank you for the reply! 
  • 1

C

107,193 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator