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

You will now use the time and difftime functions to find out which function (pow

ID: 658652 • Letter: Y

Question

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.

COMPLETE THE CODE IN BOLD

Please help with this practice problem...

//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

    for (i = 0; i < ITERATIONS; i++)

        result = pow(x,0.5);

    //your code goes here: record the time after using pow

   

    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** << " seconds." << endl;

   

    //your code goes here: record the time before using sqrt

   

    for (i = 0; i < ITERATIONS; i++)

        result = sqrt(x);

    //your code goes here: record the time after using sqrt

   

    cout << "sqrt(" << x << ") = " << result << endl;

    //complete the following lines so that

    //the appropriate value is displayed

    cout << "Operation was calculated in ";

    cout << **your code goes here** << " seconds." << endl;

    return 0;

}

Explanation / Answer

//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
time(&before);

for (i = 0; i < ITERATIONS; i++)
result = pow(x,0.5);
//your code goes here: record the time after using pow
time(&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 << difftime(after,before) << " seconds." << endl;

//your code goes here: record the time before using sqrt
time(&before);

for (i = 0; i < ITERATIONS; i++)
result = sqrt(x);
//your code goes here: record the time after using sqrt
time(&after);

cout << "sqrt(" << x << ") = " << result << endl;
//complete the following lines so that
//the appropriate value is displayed
cout << "Operation was calculated in ";
cout << difftime(after,before) << " seconds." << endl;


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote