1234567891011121314151617181920212223
#undef WATCH_H #ifndef WATCH_H #define WATCH_H #include <time.h> class watch { public: watch(); void start(); void stop(); void delay(int duration); float seconds(); float minutes(); float hours(); private: clock_t ticks_passed; }; #endif /* WATCH_H_hpp */
12345678910111213141516171819202122232425262728293031323334353637383940414243
#include <iostream> #include <stdlib.h> #include "WATCH_H.hpp" using namespace std; watch :: watch() { ticks_passed = clock(); } void watch:: start() { ticks_passed = clock(); } void watch:: stop() { ticks_passed = clock() - ticks_passed; if(ticks_passed == clock_t(-1)) { cout << "Clock overflow" << endl; exit(1); } } void watch:: delay(int duration) { for(int i =0; i<duration; ++i); } float watch:: seconds() { return float(ticks_passed)/CLOCKS_PER_SEC; } float watch :: minutes() { return float(ticks_passed)/CLOCKS_PER_SEC/60; } float watch :: hours() { return float(ticks_passed)/CLOCKS_PER_SEC/60/60; }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
#include <iostream> #include <ctime> #include <vector> #include <iomanip> #include <algorithm> #include "WATCH_H.hpp" const int SIZE =10000; unsigned int seed = (unsigned)time(0); using namespace std; void bubble_sort(vector <int>v); double random (unsigned int &seed); void initialize_vector(vector<int> &v); void print_vector (vector<int> :: iterator b, vector<int> :: iterator e ); int main() { vector <int> v; watch my_watch; initialize_vector(v); my_watch.start(); bubble_sort(v); my_watch.stop(); print_vector(v.begin(), v.end()); cout << "Seconds passed " << my_watch.seconds()<< endl; my_watch.start(); sort(v.begin(),v.end()); my_watch.stop(); print_vector(v.begin(), v.end()); cout << "Seconds passed " << my_watch.seconds()<< endl; return 0; } void initialize_vector(vector<int> &v) { for (int i=0; i<SIZE; i++) v.push_back(random(seed)*11); } void bubble_sort(vector<int> &v) { int temp; for(int k=1; k<v.size()-1; ++k) for(int j=0;j<v.size()-1-k;++j) if(v[j]>v[j+1]) { temp= v[j]; v[j]=v[j+1]; v[j+1] = temp; } } double random(unsigned int &seed) { const int MODULUS = 15749; const int MULTIPLIER = 69069; const int INCREMENT = 1; seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS; return double (seed)/MODULUS; } void print_vector (vector<int> :: iterator b, vector<int> :: iterator e ) { vector<int> :: iterator p =b; while(p<e) cout << setw(3) << (*p++); cout << endl; }
void bubble_sort(vector<int>& v)
void bubble_sort(vector<int> v)