Im trying to write a program for calculating time of execution for different algorithms and for that I need to give different sizes to an array for each algorithm.
I tried to use a loop for doing that but it's not working as I wished. Here is my code :
int main()
{
clock_t TiCal;
int size;
for(int p = 5; p < 15; p++) // This loop is to provide different sizes for our test
{
int arr[size];
cout<<"Ordered array is : { ";
for(int j = 0; j<size; j++) // We just wanted to test binary search so I skipped the ordering part
{
arr[j] = j;
cout<<arr[j]<<" ";
}
cout<<"}"<<endl;
int key = 9; // Program will search for key in array
int n = sizeof(arr) / sizeof(arr[0]); // n is size of array
TiCal = clock(); // TiCal is our variable to save time of execution
int result = BinSh(arr, 0, n - 1, key);
Sleep(1);
TiCal = clock() - TiCal;
cout<<"Consumed time for size "<<p<<" of binary search is : "<< (float)TiCal/CLOCKS_PER_SEC << " seconds"<<endl;
(result == -1) ? cout << "Element is not present in array"<<endl
: cout << "Element is present at index " << result<<endl;
}
return 0;
}