Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding a counter

Posted on 1999-07-13
9
Medium Priority
?
374 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:netminder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1199959
What function do they use to compare. You could let these functions count the number of time they are called.
0
 
LVL 2

Expert Comment

by:pesst
ID: 1199960
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

0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1199961
Adding the counter to the compare function is more robust. You can not forget to add (remove) it to (from) any loop.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:leflon
ID: 1199962
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?


0
 
LVL 11

Expert Comment

by:alexo
ID: 1199963
Make sure the counter is declared static.
0
 

Author Comment

by:netminder
ID: 1199964
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).
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1199965
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;
   ....
}

0
 

Accepted Solution

by:
smaugwar earned 200 total points
ID: 1199966
use a pointer
like
int *counter = 0;
counter = new int;

void counter1(int *count)
{
    *count = *count++
    //other things
}

void counter2(int *count)
{
    *count = *count++
    //other things
}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1199967
.....
Try reading the thread first.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question