Friday, July 31, 2009

C++ finding the mode of an array?

How would you go about finding the mode of an array? Can you show me some code as to how you can do that? My teacher says you can use an array to store the counts, but I dunno how. Where would the counters be?

C++ finding the mode of an array?
Well, you could designate the first n entry in the array to store information about the array if you wish. Of course that imply that you remember this each time you do any work on the array and that the type of the element is fit to hold the kind of information you wanted to store.





Let's suppose you have an array of int.


int myIntArray[60];


// now designate the first position to hold the array size


myIntArray[0] = 60; // but 59 values since this one is wasted





Now to answer your question about the mode, I assume here we are talking about stats (the mode being the most often occurring value). You basically need to count each time a specific value occur. There is many way to do it, but probably the most compact would be to use a std::map. For each value present in the array, create an entry in the map, with the value as the key and it's occurrence as the value, which you increment each time you find it in the array.





If you are not familiar with std::map, you can use an other array to hold the occurrence of each of the values. The problem with this is that you don't know in advance the range of values in your first array so it can go from minus infinity to infinity. It could require quite a large one. email me if you need a more precise description of that.





And then to keep in line with the rest of the question, you could store the mode once computed into the array the same way we did for it's size. That way you don't need to recompute it the next time you need it. Unless of course you change the content of the array.


myIntArray[1] = ComputeTheMode(myIntArray);


// now only 58 remaining spot for values





Obviously though, the preferred way to go when you need to store additional information like that, would be to create a struct or a class, have the array, it's size, and any additional information stored as member of it. Not as part of the array itself. Better yet, never use arrays. std::vector and other stl containers are so much better. This is c++ after all. Use it's features anywhere you can.





Hope that help
Reply:Look at http://www.daniweb.com/techtalkforums/th...





Anyway, one way in C++ is:


1. Your numbers are in a vector (http://www.cppreference.com/cppvector/in... ). I'm guessing vector is a better choice because you probably have no idea how many numbers you are doing to be dealing with, right?


2. Use a map to store the frequency of each number. Walk through the vector of numbers, incrementing the frequency of the corresponding number in the map.


3. Then go through the map itself. Look for what frequency is the largest. That will tell you your mode.
Reply:you have to keep track of the size of the array seperately. create a new variable for that.


No comments:

Post a Comment