Hello.
I already wrote a working program checking to see if a number is prime through primal testing. Now I am tasked with adding a clock to tell how long different variations of numbers take to run. My teacher has an accent which makes it difficult for me to understand him during lecture so I didn't catch everything that he told us about this.
int main()
{
char another = 'y';
long long N;
bool IsPrime(long long N);
double
explanation();
while (another == 'y' || another == 'Y')
{
cout << "Enter a positive (+) integer: " << endl;
cin >> N;
if (N <= 0)
{
cout << "Must be a positive integer." << endl;
cout << "Enter a positive (+) integer: " << endl;
cin >> N;
if (N <=0)
{
cout << "This program will now close." << endl;
system("pause");
return 0;
}
}
if (IsPrime(N))
cout << N << " is a prime number. " << endl;
else
cout << N << " is not a prime number. " << endl;
cout << "Would you like to try again? , enter (Y/y or N/n)" << endl;
cin >> another;
}
return 0;
}
bool IsPrime(long long n)
{
for ( int i = 2; i <= n/2; i++)
{
if (n % i == 0)
return 0;
}
return true;
}
void explanation()
{
cout<<" - - - - - - - - - - - - - - - - - - - - - - - - - - - -"<<endl;
cout<<" This program will take a positive number from the user "<<endl;
cout<<" and tell them whether it is a prime number or not. "<<endl;
cout<<" - - - - - - - - - - - - - - - - - - - - - - - - - - - -"<<endl;
}
--------------------------------------------------------------------------------
This is what we are supposed to transition into our already working program.
std::chrono::time_point<std::chrono::system_clock> start. end;
start = std::chrono::system_clock::now();
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
In your posts, if you put your code in code tags it will be much easier to read, future reference.
You have everything you need - kinda just skimmed your program but if I understand what you need to do: just throw in some if statements (if the program hits a prime, execute start = std::chrono::system_clock::now(); and end = std::chrono::system_clock::now();) when you hit another prime and you have your duration in-between with std::chrono::duration<double> elapsed_seconds = end - start;
#include <iostream>
#include <chrono>
#include <ctime>
int main()
{
// Class std::chrono::steady_clock represents a monotonic clock.
// The time points of this clock cannot decrease as physical time moves forward.
// This clock is not related to wall clock time, and is best suitable for measuring intervals.
// http://en.cppreference.com/w/cpp/chrono/steady_clockconstauto start = std::chrono::steady_clock::now();
// ...
// rest of the code in main goes here
// ...
constauto end = std::chrono::steady_clock::now();
constauto c_time = std::time(nullptr) ;
constauto elapsed_time = end - start ;
// http://en.cppreference.com/w/cpp/chrono/duration/duration_castconstauto elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>(elapsed_time).count() ;
std::cout << "finished computation at " << std::ctime( &c_time ) << '\n'
<< "elapsed real time: " << elapsed_seconds << "s\n" ;
}