Jump to content
  • Advertisement
Sign in to follow this  
loachman

Finding median and mode array of pointers

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
first you have to sort the pointers based on some value associated with the object that they point to. I wouldnt sort the pointers, but a small object containing the pointers. Something like

class Entry
{
public:

type* pointer;
type2 someValueFromPointer;

Entry(type* pointer)
{
pointer = pointer;
someValueFromPointer = pointer->value;
}

bool operator < (const Entry& rhs)
{
return someValueFromPointer < rhs.someValueFromPointer;
}
};




put the pointers in Entry, and sort Entry's, which, ends up sorting the pointers, based on some value they contain. Entry would then behave like a simple int, float, etc. Not sure if this helps, might need to know more about what youre doing, quick sort or similar?

Share this post


Link to post
Share on other sites
Well the median is the number that is in the center of the array when sorted, and the mode is the most frequent number in the list. So to start, you gotta sort, and there are many ways to sort, bubblesort, quicksort, radix sort, so on. Im not sure if this is just learning practice or a project. The STL has a sort algorithm if this is something you're just trying to get done, but I assume you're planning on writing these yourself, so try googling for sorting algorithms.

Share this post


Link to post
Share on other sites
I know what the mode and median are.

The problem is the book shows how to do quick sort and bubble sort.
But it does not show how to return the whole array to be used for later.
You just pass the array as a parameter then call the function.
e.g. here are the values sorted showSorted(myarray,numElems);

I was loving this book till now.

Share this post


Link to post
Share on other sites
The parameter "int* mov" will go in unsorted and come out sorted, I assume the sort function will do some array element swapping and the ints being pointed to will be moved around, so you dont have to return the array, as the array is being modified because it wasnt declared as "const int* move".

Share this post


Link to post
Share on other sites
O.K. so if I just run the sorting on it. I will be sorted from then on? In other words I can just call the function as void? I do not need to return anything?

Share this post


Link to post
Share on other sites
Cool thanks! That just helped tremendously. I thought I had to return the array.
Although I have never seen a function return a whole array. Finding the median and mode should be simple now.

Share this post


Link to post
Share on other sites
Quote:
Original post by loachman
I thought I had to return the array.
Although I have never seen a function return a whole array.


That's because it's impossible. When you pass an array, it decays to a pointer (so you have no information about number of elements in the array, etc.); this means that you can modify the elements of the array and the original will also have its elements modified (I say 'will also' but it's obviously more of a case of the two being one and the same).

So whilst you could return the array in terms of a pointer to the first element, this only works if you're returning the array that was passed to you (for ease's sake, so you can do something like)

int myArray[25];
foo(bar(myArray));


Where bar returns a pointer to the first element of myArray.

This is why, as you move on, you'll find that it is a definite help to use the SC++L, because it provides things such as std::vector (the de facto replacement for an 'array' unless you really actually want/only need an array - which is fairly rare - in which case you use boost::array or similar) which behaves 'normally' (passed by copy, and you can choose to pass by pointer or reference; you can return it, etc.).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!