I made a program that generates a user predefined number of random numbers and then sorts them and it repeats that as many times as the user wants it to repeat. While doing so, the program tracks how long the key functions take.
For that I defined a struct (I guess I could delete the pPrev but let that aside ..) that I'm allocating dynamically and that works like a charm, at least I think so.
struct sTimes {
struct sTimes *pNext;
struct sTimes *pPrev;
time_t tNumGen;
time_t tSort;
};
and declared / initialized three things corresponding to the average time calculation:
struct sTimes * pTimes = NULL; // pointer to times
struct sTimes * pTimeStart; // pointer to start of times
struct sTimes tAverage; // average times
Before each generation and sorting I'm doing:
pTimes->tNumGen = clock();
pTimes->tSort = clock();
and after each generation and sort:
pTimes->tNumGen = clock() - pTimes->tNumGen;
pTimes->tSort = clock() - pTimes->tSort;
okay, so that's the basic structure. The cycles and sorting etc works. My problem is that the average times don't get calculated super exactly, and I can't figure out why or how to fix that (I'm getting this instead of this).
I calculated the average times like that:
tAverage.tNumGen = pTimeStart->tNumGen;
tAverage.tSort = pTimeStart->tSort;
pTimes = pTimeStart;
if(pTimes->pNext != NULL)
{
pTimes = pTimes->pNext;
}
while(pTimes != NULL)
{
tAverage.tNumGen += pTimes->tNumGen;
tAverage.tNumGen /= 2;
tAverage.tSort += pTimes->tSort;
tAverage.tSort /= 2;
pTimes = pTimes->pNext;
}
I would be so glad if someone has a fancy answer or at least can give me a solution approach to my problem. And I would probably be even happier if someone can explain why this is happening. Have a gread day :)