So I have completed my program thus far to take an array the user desingnates and breaks with any letter.
After outputting what the array holds it sorts and re prints. I want to also include the time it takes this to happen.
I am trying to use the gettimeofday function. I am getting "not declared" in the header
It's 3 am too, that could be it. Here is my code: Main.cpp here
int main(int argc, char * argv[])
{
cout << "Welcome To The Sorter! V 1.0..." << endl << endl;
cout << "----------------------------------------------------" << endl;
cout << "| Please Enter Numbers That You Would Like Sorted |" << endl;
cout << "| Enter Any Letter To Exit Sequence |" << endl;
cout << "----------------------------------------------------" << endl;
Sorting derp;
int x = 0;
int Myarray[200];
int copied[200];
char holder[128] = {0};
while(derp.isnumber(holder) == true)
{
cin.getline(holder,256);
if(derp.isnumber(holder))
{
int c = atoi(holder);
Myarray[x] = c;
x = x + 1;
derp.print_array(Myarray,x);
}
}
cout << "----------------------------------" << endl;
cout << "| Breaking from numerical input |" << endl;
cout << "----------------------------------" << endl;
derp.copy_array(Myarray,copied,x);
derp.selection_sort(copied,x);
derp.print_array(copied,x);
derp.copy_array(Myarray,copied,x);
struct timeval *start, *finish;
gettimeofday(&start, NULL);
derp.selection_sort(Myarray,x);
derp.diff_in_micros(finish, start);
gettimeofday(&finish, NULL);
cout << "Time (us) : " << diff_in_micros(finish,start) << endl;
//derp.linear_insertion_sort;
//derp.print_array(copied,x);
return 0;
}
Header.cpp // Leaving out the sorting and unimportant functions
#include "Sorting.h"
#include <iostream>
using namespace std;
long Sorting::diff_in_micros(timeval finish, timeval start)
{
long sec = (finish.tv_sec - start.tv_sec) * 1000;
long us = (finish.tv_usec - start.tv_usec);
return sec + us;
}
Header File:
#ifndef SORTING_H
#define SORTING_H
class Sorting
{
public:
Sorting();
void copy_array(const int src[], int dst[], int size);
void print_array(const int array[], int size);
void selection_sort(int array[], int size);
void linear_insertion_sort(int array[], int size);
void binary_insertion_sort(int array[], int size);
bool isnumber(char *str);
long diff_in_micros(timeval finish,timeval start);
virtual ~Sorting();
protected:
private:
};
#endif // SORTING_H