2a. Recall that pow and sqrt are predefined mathematical functions available in
ID: 3564556 • Letter: 2
Question
2a. Recall that pow and sqrt are predefined mathematical functions available in C++. Given that sqrt(x) = x^1/2, both of these functions can be used to calculate the square root of a number. The following program uses the pow and sqrt functions to calculate the square root of a number entered by the user. Save this program as sqrt.cpp.
//Calculate the square root of a number using sqrt and pow
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double x;
double result;
cout << "Enter a (double) value: ";
cin >> x;
result = pow(x, 0.5);
cout << "pow(" << x << ", 0.5) = " << result << endl;
result = sqrt(x);
cout << "sqrt(" << x << ") = " << result << endl;
return 0;
}
Compile, link, and execute sqrt.cpp. What is the square root calculated by the program if the user input is 81? Step through the code by hand.
Use the following memory table to show what occurs in memory when the C++ code is executed. (Include line numbers as documentation only. Do not use line numbers when entering your final program.) To fill out the memory table, use one or two lines for each variable. On one line, enter declaration information. Write the name of the declared variable, its data type, and the line number at declaration.
Variable Name
Data Type
Value in Memory
Line Number at Declaration
Line Number When Initialized
In the following space, enter what is displayed on the screen after executing the program.
2b. Test the program with other values. Fill the following table with the results from your program:
Input Values (x)
pow(x, 0.5)
sqrt(x)
1
2
4
13
121
1029
3a. You will now use the time and difftime functions to find out which function (pow or sqrt) is more efficient in calculating the square root of a number. Follow the indications in the following code to complete the program. Save it in a file named sqrt2.cpp.
//Calculate the square root of a number using sqrt and pow
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main ()
{
time_t before;
time_t after;
double x;
double result;
int i;
//number of times pow and sqrt will be called:
const int ITERATIONS = 1;
cout << "Enter a (double) value: ";
cin >> x;
//your code goes here: record the time before using pow
before = 0seconds____________
for (i = 0; i < ITERATIONS; i++)
result = pow(x,0.5);
//your code goes here: record the time after using pow
after = _______________
cout << "pow(" << x << ", 0.5) = " << result << endl;
//complete the following lines so that
//the appropriate value is displayed
cout << "Operation was calculated in ";
cout << **your code goes here
Variable Name
Data Type
Value in Memory
Line Number at Declaration
Line Number When Initialized
Explanation / Answer
Hello. I am an online tutor and freelance journalist. I am willing to assist you with this assignment and many more. My work is 100% original and delivered on time. Email me at ashioya.george@gmailc.com
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.