Define a function called hypotenuse that calculates the length of the hypotenuse
ID: 3790621 • Letter: D
Question
Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. The function should take two arguments of type double and return the hypotenuse as a double. Test your program with the side values specified as follows: Write a function integerPower(base, exponent) that returns the value of base^exponent For example. integerPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent isa positive, nonzero integer, and base is an integer. Function integerPower should use for to control the calculation. Do not use any math library functions. Write the int main(void) function as a driver program and call the above two functions result with sample Input/Output.Explanation / Answer
#include <stdio.h>
#include<math.h>
int integerPower(int base, int exp){
int res = 1,i = 0;
for(i=0;i<exp;i++)
res*=base;
return res;
}
double hypotenuse(double s1, double s2){
return (sqrt(integerPower((int)s1,2) + integerPower((int)s2,2)));
}
int main()
{
printf("%d %d %d ", integerPower(2,0), integerPower(2,1), integerPower(2,4) );
printf("%lf ",hypotenuse(3,4));
printf("%lf ",hypotenuse(5,12));
printf("%lf ",hypotenuse(8,15));
return 0;
}
//-------------------------------------------------------------------------Output-------------------------------------------------------
sh-4.2$ gcc -o main *.c -lm
sh-4.2$ main
1 2 16
5.000000
13.000000
17.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.