netminder
asked on
Adding a counter
I want to add a counter to the functions InsertionSort and Mergesort. I want to try and count the number of comparsions that are made. My problem is how to go implementing this task. Any sugestions
Thanks
Thanks
What function do they use to compare. You could let these functions count the number of time they are called.
I would imagine you would
just do a
count = count + 1;
in your loop and it would
tell you how many comps there
were.
post the loop in your code
just do a
count = count + 1;
in your loop and it would
tell you how many comps there
were.
post the loop in your code
Adding the counter to the compare function is more robust. You can not forget to add (remove) it to (from) any loop.
some questions to get the problem:
1) do you want one counter for both functions (declared in the calling function) or one for each function?
2) will the count be used outside of the two functions?
3) do the functions return a value yet?
1) do you want one counter for both functions (declared in the calling function) or one for each function?
2) will the count be used outside of the two functions?
3) do the functions return a value yet?
Make sure the counter is declared static.
ASKER
Only one counter for both functions. The # of compares and the number of items in the array(hopefully I can get to output file(appended mode).
static int count_compares = 0;
void reset_counter() { count_compares = 0; }
int compare1(const Something& s1, const Something& s2)
{
++count_compares;
// .... other stuff
}
int compare2(const SomethingElse& s1, const SomethingElse& s2)
{
++count_compares;
// .... other stuff
}
void f()
{
reset_counter();
....
InsertionSort();
out << "Number of compare: " << count_compares << endl;
....
}
void reset_counter() { count_compares = 0; }
int compare1(const Something& s1, const Something& s2)
{
++count_compares;
// .... other stuff
}
int compare2(const SomethingElse& s1, const SomethingElse& s2)
{
++count_compares;
// .... other stuff
}
void f()
{
reset_counter();
....
InsertionSort();
out << "Number of compare: " << count_compares << endl;
....
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
.....
Try reading the thread first.
Try reading the thread first.