Help with time function in c++!!!!!!!!

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#define SIZE 100

/*int compare (const void *elem1, const void *elem2)
{
return (*(int*)elem1 - *(int*)elem2);
}*/

int main()
{

LARGE_INTEGER start, finish, freq;
QueryPerformanceFrequency(&freq);

int count,n1;
int a[SIZE];
int num;

for(count=0;count<SIZE;count++)
{
n1=rand()%111; //generate numbers between 0 and 50
a[count]=n1;
}

//qsort(a, SIZE, sizeof(int), compare);

cout<<"The input numbers are :"<<endl;
for(count=0;count<SIZE;count++)
{

cout<<a[count]<<",";
}

getch();
cout<<endl<<"Input your searching number :";
cin>>num;


/* cout<<endl<<"Input your searching number :";
cin>>num;*/

QueryPerformanceCounter(&start);

cout << "Unsorted Numbers: " << endl;
cout << ">> ";

for (int k = 0; k < n; k++)
{
cout << numbers[k] << ", ";
}

for (int firstNumber = 0; firstNumber < n; firstNumber++)
{
int leastNumber = firstNumber;

for(int currentNumber = firstNumber + 1; currentNumber < n; currentNumber++)
{
if(numbers[currentNumber] < numbers[leastNumber])
{
leastNumber = currentNumber;
}
}
swap(numbers[firstNumber], numbers[leastNumber]);
}

cout << "\n\nSorted Numbers: " << endl;
cout << ">> ";

for (int j = 0; j < n; j++)
{
cout << numbers[j] << ", ";
}

QueryPerformanceCounter(&finish);

cout/*<<"This program's loop is executed "<<(count+1)*/<<" times to find out the number."<<endl;

cout<<"Execution took (time in seconds): "<<((finish.QuadPart-start.QuadPart)/(double)freq.QuadPart)<<endl;

getch();
}
This code : n1=rand()%111; //generate numbers between 0 and 50 generates numbers between 0 and 111


1
2
3
4
5
6
7
8
9
10
11
12
13
for (int firstNumber = 0; firstNumber < n; firstNumber++)
{
int leastNumber = firstNumber;

for(int currentNumber = firstNumber + 1; currentNumber < n; currentNumber++)
{
if(numbers[currentNumber] < numbers[leastNumber])
{
leastNumber = currentNumber;//<--Why are you doing this?
}
}
swap(numbers[firstNumber], numbers[leastNumber]);
}



Also you need to include ctime/time.h library and do this in main:

srand((unsigned)time(0));//Seed pseudo random number generator


I am not very familiar with the way you are timing your code, but I have done something similar before:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
clock_t NewTime = clock();
clock_t PrevTime = NewTime;


while (1)
    {
        NewTime = clock();
        checkTime += (double)(NewTime - PrevTime);
        PrevTime = NewTime;
        
        if (checkTime > (double)(CLOCKS_PER_SEC))
//Multiply CLOCK_PER_SEC by how often you want to update you code (in secs)
        {
            //Some Code in here
            checkTime -= (double)(CLOCKS_PER_SEC);
        }
    }
Topic archived. No new replies allowed.