Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Secure l https://mail.google.corm/mail/u/0/#inbox/15eal 136ac387d167projectoral

ID: 3887962 • Letter: S

Question

Secure l https://mail.google.corm/mail/u/0/#inbox/15eal 136ac387d167projectoral Problem 2 (15 pts): Unit testing (functions)-sec 5.7 in Zybook a) (9 pts) The square root of a number x can be approximated by the repeated application of the following Newton-Raphson formula: y new- y_old+x/y old) where y_new is the new estimate of the square root of x and y_old is the previous one al) Write a C user-defined function that calculates the square root of any positive number with an accuracy of 10-That is, the new and the old estimates differ by less than lo- Function prototype: double root(double x); a2) In the main function, use assert() (see sec 5.7 (Figure 5.7.2) in your Zybook) to test your "root" function. ASSERT your function by running unit tests for test values: 0, 4, 10, 10000, 0.25, 7.85, and 9.8696. One example for testing square root of 10 is assert(fabs(root(10)-3.16228)

Explanation / Answer

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

int main(void) {
double a, b, sh, c, sa, root1, root2;
a = 1;
char con='y';
while(1){
if( con == 'y'){
printf(" Enter the area of the square pyramid: ");
scanf("%lf", & sa);
printf("Enter the slant height of the square pyramid : ");
scanf("%lf", & sh);
b = 2 * sh;
c = (-1) * sa;
root1 = (-b + sqrt(b * b - 4. * a * c)) / (2. * a);
root2 = (-b - sqrt(b * b - 4. * a * c)) / (2. * a);
if (root1 > 0) {
printf("The Base of the square pyramid is: %lf ", root1);
} else if (root2 > 0) {
printf("The Base of the square pyramid is: %lf ", root2);
}
printf("Do you want to continue ( y / n ) ? ");
scanf("%s",& con);
}else{

}
}
return 0;
}