Homework question:
- Cygwin GNU GDB
- Cygwin GNU GCC
Attempting to establish the length of the hypotenuse C from the square root of A power of 2 and B power of 2.
Example input:
Enter the length of two sides of a right-angled triangle: 2.25 8.33
Answer:
The length of the hypotenuse is: 8.628523
- Question: when I specify the same input as above, the result is not the same - output is 19.84.9596
Full code below:
float squareRoots(float *s)
{
float cx;
float nx;
float e;
cx = 1;
nx = (cx +*s/cx)/2;
e = nx - cx;
cx = nx;
if (e*e > 0.001)
{
nx = (cx +*s/cx)/2;
return nx;
} else {
return nx;
}
}
float hypotenuse(float *a, float *b)
{
float c;
//raise a to power of 2
*a = (*a * *a);
*b = (*b * *b);
//add a and b
float y = *a + *b;
c = squareRoots(&y);
return c;
}
int main()
{
float x,y;
printf("Enter the length of two sides of a right-angled triangle:");
scanf("%f %f", &x, &y);
float k=hypotenuse(&x,&y);
printf("The length of the hypotenuse is: %f", k);
exit(0);
}