Question

Instructions: Consider the following C++ program. It prompts the user for a number and calculates and...

Instructions:

Consider the following C++ program. It prompts the user for a number and calculates and prints the square root of the number using the "sqrt" function and the "pow" function.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   double num, root, error;
   cout << "Enter number:\n";
   cin >> num;

   root = sqrt(num);
   cout << "sqrt(" << num << ")= " << root << endl;

   root = pow(num, 0.5);
   cout << "pow(" << num << ",1/2)= " << root << endl;
   
   return 0;
}

Step 1: Copy this program into your C++ program editor, and compile it. Hopefully you will not get any error messages.

Step 2: Run the program and type in the number "42" to see what it prints. You should see the square root of 42 calculated using the "sqrt" function. The second output line shows 42 raised to the power 1/2 which is another way to calculate the square root. Try several other input values. Do the two square root calculations always match each other?

Step 3: Type in "man sqrt" in your Linux window to see the manual page for this function. Near the top of the manual page you will see the function prototype "double sqrt(double x);". If you read the description you will see that the sqrt function expects one double parameter and returns a double value that is the "nonnegative square root of x".

Step 4: Type in "man pow" in your Linux window to see the manual page for this function. Near the top of the manual page you will see the function prototype "double pow(double x, double y);". If you read the description you will see that the pow function expects two double parameters and returns a double value that is equal to "x raised to the power of y".

Step 5: If you entered the number "4" above you saw that the square root was equal to 2, which is exactly correct. On the other hand, we can only approximate the square root of "3" using a double in C++ because the true answer has infinite length. To see what the error in this square root calculation is, copy/paste the following two lines after the "cout << sqrt" and "cout << pow" lines above.

   error = num - root * root;
   cout << "error= " << error << endl << endl;

Step 6: Compile and run your program and try a few different input values. What is your error when you input a perfect square like "49" as input? What is the error when you input a prime number like "3"? Is there any difference in error between "sqrt" and "pow"?

Step 7: Type in "man cbrt" in your Linux window to see the manual page for this function. Near the top of the manual page you will see the function prototype "double cbrt(double x);". If you read the description you will see that the cbrt function expects one double parameter and returns a double value that is the "real cube root of x".

Step 8: Your next task is to extend your program to calculate cube roots two ways and compare their errors. Edit your program and use copy/paste to duplicate your sqrt, pow and error calculations. Next, change sqrt to cbrt, and change the pow exponent to 1.0/3.0. Finally, modify the error calculations accordingly. When you compile and run your program with an input of "42", it should output the following:

sqrt(42)= 6.48074
error= 0

pow(42,1/2)= 6.48074
error= 0

cbrt(42)= 3.47603
error= 0

pow(42,1/3)= 3.47603
error= 0

Step 9: Run your new program several times with different input values. What happens if you input a negative value? What happens if you enter a 5 digit input value? Is sqrt(x) as accurate as pow(x,1/2)? Is cbrt(x) as accurate as pow(x,1/3)? For most applications these small errors are not an issue. On the other hand, if you are calculating the gross domestic product of the country or the trajectory of a rocket to mars, these differences DO matter.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   // STEP - 1 && STEP - 2.
   double num, root, error;  
   num = 42;
   root = sqrt(num);
   cout << "sqrt(" << num << ")= " << root << endl;
   root = pow(num, 0.5);
   cout << "pow(" << num << ",1/2)= " << root << endl;
   error = num - root * root;
   cout << "error= " << error << endl << endl; // no error

   // STEP - 5.1 && STEP - 5.2
   num = 4; // 5.1
   root = sqrt(num);
   cout << "sqrt(" << num << ")= " << root << endl;
   root = pow(num, 0.5);
   cout << "pow(" << num << ",1/2)= " << root << endl;
   error = num - root * root;
   cout << "error= " << error << endl << endl; // no error

   num = 3; // 5.2
   root = sqrt(num);
   cout << "sqrt(" << num << ")= " << root << endl;
   root = pow(num, 0.5);
   cout << "pow(" << num << ",1/2)= " << root << endl;
   error = num - root * root;
   cout << "error= " << error << endl << endl; // Yes there is a difference!.

   // STEP - 6.1 && STEP - 6.2
   num = 49; // 6.1
   root = sqrt(num);
   cout << "sqrt(" << num << ")= " << root << endl;
   root = pow(num, 0.5);
   cout << "pow(" << num << ",1/2)= " << root << endl;
   error = num - root * root;
   cout << "error= " << error << endl << endl; // no error

   num = 3; // 6.2
   root = sqrt(num);
   cout << "sqrt(" << num << ")= " << root << endl;
   root = pow(num, 0.5);
   cout << "pow(" << num << ",1/2)= " << root << endl;
   error = num - root * root;
   cout << "error= " << error << endl << endl; // Yes there is a difference!.

   // STEP - 7
   num = 3; // 6.2
   root = cbrt(num);
   cout << "sqrt(" << num << ")= " << root << endl;
   root = pow(num, 1.0/3.0);
   cout << "pow(" << num << ",1/3)= " << root << endl;
   error = num - root * root;
   cout << "error= " << error << endl << endl; // Yes there is a difference!.


   // STEP - 9
   // There will always be a difference in roots, if we do no provide the exact complete value in decimal
   // which is infintely long. so we provide fractions for getting the best result possible.

   return 0;
}

IF THERE IS ANY PROBLEM OR YOU HAVE ANY QUERY REGARDING THE SOLUTION KINDLY FEEL FREE TO ASK, AND LEAVE A THUMBS UP WHEN YOU ARE SATISFIED. THANKS!.

Add a comment
Know the answer?
Add Answer to:
Instructions: Consider the following C++ program. It prompts the user for a number and calculates and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT