Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 // cin, cout

#include // For calculation.

#include // string

#include

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));

}

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions