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

I want to make my code run. This is my code. How can I measure time elased for c

ID: 3729830 • Letter: I

Question

I want to make my code run.

This is my code.

How can I measure time elased for calculating Ackermann function?

If I run my code below, it always shows me 0 seconds. I think that is wrong.

I want to make code run by reducing // in //code_to_time();

What should I add to make this code run including code_to_time(); ???

Please help me..

----------------------------------------------------------------------------------------------------------------------------------------

#include <iostream> // cin, cout

#include <cmath> // For calculation.

#include <string> // string

#include <ctime>

using namespace std; // std::cout - > cout

// function prototype

double A(int, int); // The Ackermann function A(m,n)

void main()

{

int num1, num2; // number

cout << "Enter an two integer values ";

cout << " I will display the value of the Ackermann function" << endl;

cin >> num1 >> num2; // User will type numbers

clock_t begin, end;

begin = clock();

//code_to_time();     <<-- **** How can I make this code run to calculate how many times elapsed??

end = clock();

double elapsed_secs = (double(end - begin) / CLOCKS_PER_SEC);

// Try ... catch phrase

try

{

//Display the result

cout << "A(" << num1 << ", " << num2 << ")" << " answer is ";

cout << A(num1, num2) << " and it took " << elapsed_secs << " seconds." << endl;

}

catch (string exceptionString) // For the exception string

{

cout << exceptionString;

}

system("pause");

}

// Define A function

double A(int m, int n)

{

if (m == 0)

{ // If m = 0 return n + 1;

return n + 1;

}

if (m < 0 || n < 0)

{ // If m < 0 OR n < 0 throw an error.

string exceptionString = "Error ";

throw exceptionString;

}

if (n == 0)

{ // If n = 0 then return A(m-1, 1)

return A(m - 1, 1);

}

if (m > 0 && n > 0) // If m > 0 and n > 0 return A(m-1, A(m, n-1))

return A(m - 1, A(m, n - 1));

}

------------------------------------------------------------------------------------------------------------

Explanation / Answer

Hi,

i can see that there is no function call in the main() as a result the function is not getting executed and hence the time elapsed is 0

double A(int, int); // The Ackermann function A(m,n)

void main()

{

int num1, num2; // number

cout << "Enter an two integer values ";

cout << " I will display the value of the Ackermann function" << endl;

cin >> num1 >> num2; // User will type numbers

clock_t begin, end;

begin = clock();

//code_to_time();     <<-- **** How can I make this code run to calculate how many times elapsed??

// MAKE A FUNCTION CALL HERE like double A(int m, int n);

end = clock();

double elapsed_secs = (double(end - begin) / CLOCKS_PER_SEC);

// Try ... catch phrase

try

{

//Display the result

cout << "A(" << num1 << ", " << num2 << ")" << " answer is ";

cout << A(num1, num2) << " and it took " << elapsed_secs << " seconds." << endl;

}

catch (string exceptionString) // For the exception string

{

cout << exceptionString;

}

system("pause");

}

// Define A function

double A(int m, int n)

{

if (m == 0)

{ // If m = 0 return n + 1;

return n + 1;

}

if (m < 0 || n < 0)

{ // If m < 0 OR n < 0 throw an error.

string exceptionString = "Error ";

throw exceptionString;

}

if (n == 0)

{ // If n = 0 then return A(m-1, 1)

return A(m - 1, 1);

}

if (m > 0 && n > 0) // If m > 0 and n > 0 return A(m-1, A(m, n-1))

return A(m - 1, A(m, n - 1));

}

As I can see from your question, it looks like you want to know the elapsed time after execution of some piece of code. I guess it would be comfortable to see the results in second(s). If so, try using difftime() function as shown below. Hope this solves your problem


#include <time.h>
#include <stdio.h>

time_t start,end;
time (&start);
.
.
.
<your code>
.
.
.
time (&end);
double dif = difftime (end,start);
printf ("Elasped time is %.2lf seconds.", dif );

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