Time keeper...Help

Jan 25, 2016 at 12:19am
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.

#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>

using namespace std;

void explanation();
bool IsPrime();

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

std::cout<<"finished computation at:"<<std::ctime(&end_time)
<<"elapsed time: " << elapsed_seconds.count()<<"s/n";

--------------------------------------------------------------------------------
Thank you in advance for your help.
Jan 25, 2016 at 2:14am
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;
Last edited on Jan 25, 2016 at 2:24am
Jan 25, 2016 at 2:50am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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_clock
    const auto start = std::chrono::steady_clock::now();

    // ...
    // rest of the code in main goes here
    // ...

    const auto end = std::chrono::steady_clock::now();
    const auto c_time = std::time(nullptr) ;

    const auto elapsed_time = end - start ;

    // http://en.cppreference.com/w/cpp/chrono/duration/duration_cast
    const auto 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" ;
}
Jan 25, 2016 at 3:34am
Thank you both very much. Just wondering how do you put the code in code tags?
Jan 25, 2016 at 3:40am
Topic archived. No new replies allowed.