Question

In: Computer Science

C++ How do I initialize a clock to 0 and run it constantly throughout the program?...

C++ How do I initialize a clock to 0 and run it constantly throughout the program? THEN, how do I get the time at that moment multiple times? As in id like to set the

clock = 0,

do stuff

do stuff

get time now, store it

do stuff

get time now, store it

then also be able to do arithmetic on it, for instance, last time - current time = 2 seconds. I made it sound complicated but I believe this is very easy.

Solutions

Expert Solution

Code -

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
std::clock_t startTime;
double endTime;
//initialize start time
startTime = std::clock();
//print the start time
cout<<"Start Time "<<startTime<<endl;
/* Your algorithm here */
/*do stuff

do stuff*/
//calculate time after some interval
std::clock_t time1 = std::clock();
//print the time
cout<<"Time in between "<<time1<<endl;
//calculate the time difference from now to the start time and print the result
double secondDifference = ( time1 - startTime ) / (double) CLOCKS_PER_SEC;
cout<<"Time difference "<<secondDifference<<" second"<<endl;
/*do stuff*/
//calculate time after some interval
std::clock_t time2 = std::clock();
//print the time
cout<<"Time at end "<<time2<<endl;
//calculate the time difference from now to the start time and print the result
secondDifference = ( time2 - startTime ) / (double) CLOCKS_PER_SEC;
cout<<"Time difference "<<secondDifference<<" second"<<endl;

}

Screenshots -


Related Solutions

How do I run a lex program written with C on xcode?
How do I run a lex program written with C on xcode?
how do i run 3 files on c++?
how do i run 3 files on c++?
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
C Program: How do I write a Greedy function for 0-1 knapsack, to find total value...
C Program: How do I write a Greedy function for 0-1 knapsack, to find total value only( replace struct Knapsack) # include #include #include struct Knapsack {    int value;    int weight; };    // returns maximum of two integers    int max(int a, int b) { return (a > b)? a : b; }    // Returns the maximum value that can be put in a knapsack of capacity W    struct Knapsack knapSackExhaustive(int W, int wt[], int...
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Can someone explain how to manipulate the clock in verilog code? For example, I do not...
Can someone explain how to manipulate the clock in verilog code? For example, I do not understand how to go from 100MHz to 68027Hz.
Operating system How do I run a program about processces and IPC on linux can someone...
Operating system How do I run a program about processces and IPC on linux can someone show me an example and the step by step way of doing it. I have files tha were given and I need to run them. Thank you
When I run this C++ program that asks the user to enter the population of 4...
When I run this C++ program that asks the user to enter the population of 4 cities and produce the bar graph - it runs but ends with a Debug Error - run time check failure 2 stack around variable population was corrupted - any help would be appreciated #include <iostream> using namespace std; int main() { int population[4],k;    int i=1,n=5;       do    {        cout<<"Enter the population of city "<<i<<":"<<endl;             cin>>population[i];        if(population[i]<0)       ...
Question: How do I write a program in C# that calculates a student's final grade for...
Question: How do I write a program in C# that calculates a student's final grade for a test with 20 multiple questions using arrays and helper methods? Instructions: (0-incorrect answer, 1-correct answer) If the student answered the question right, add .5 points to the running total. If the student didn’t answer correctly subtract .5 points from the running total. The running total is initialized with 5 points (so the student receives 5 points extra credit). To define the final grade...
How do I write a C++ program to call a frequency table from a csv file,...
How do I write a C++ program to call a frequency table from a csv file, using vector? Data given is in a csv file. Below is part of the sample data. Student ID English Math Science 100000100 80 90 90 100000110 70 60 70 100000120 80 100 90 100000130 60 60 60 100000140 90 80 80
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT