Write a function to calculate the square root of a float number with the followi
ID: 3755545 • Letter: W
Question
Write a function to calculate the square root of a float number with the following interface: float squareRoot( float x) Il assert that x is not negative The function should return the vx. In addition, if the input variable x is negative, your function should stop the execution via the assert() function, which is built-in function in C++. You should add a function call to assert() at the beginning of the above function to guarantee the preconditiorn of this function is correct In your main function, you need design two test cases: x-3 andx--3. Create a screenshot to demonstrate the execution of your program in these two test cases. In the case: x--3, there should be an error prompt window. Save this error prompt window as the evidence that you successfully implement the assert function call. Do you think this error prompt window provides you some useful information? Furthermore, set a break point in the main function before calling squareRoot( float x). Then, use step-wise debugging tool to step into this function, and step through the whole function until the program returns to the main function. Use screenshots to record the process, and embed the screenshots into the word document.Explanation / Answer
#include <stdio.h> /* printf */
#include <assert.h> /* assert */
#include<math.h>
//method to find square root of a given number x
float squareRoot(float x) {
assert (0<=x);//using assertion
//if number is not negative then
return sqrt(x);//
}
int main ()
{
//testing
float a=3,b=-3;
//case1
//checking with positive number
printf("Square root of %f is: %f ",a,squareRoot(a));
//case2
//checking with negative number
printf("Square root of %f is: %f ",b,squareRoot(b));
return 0;
}
output:
Square root of 3.000000 is: 1.732051
Assertion failed!
Program: D:chegg-supremecppsquareroot.exe
File: D:chegg-supremecppsquareroot.cpp, Line 6
Expression: 0<=x
Process exited with return value 3
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.