0

Possible Duplicate:
c++ sort with structs

#include <iostream>
using namespace std;

class fish{
private:
    int size;
    int price;
public:
    fish()
    {
        size=0;
        price=0;
    }
    void set_price(int x)
    {
        price=x;
    }
    void set_size(int g)
    {
        size=g;
    }
    int get_size()
    {
        return size;
    }
    int get_price()
    {
        return price;
    }
    void display()
    {
        cout<<" Fish price is "<<price<<" Fish size is "<<size<<endl;
    }
    void sort(fish h[5])
    {
        for (int o=0;o<=5;o++)
        {
            fish temp;
            temp.set_price(0);

            if (h[o].get_price()>h[o+1].get_price())
            {
                temp.get_price()=h[o].get_price();
                h[o].get_price()=h[o+1].get_price();
                h[o+1].get_price()=temp.get_price();

            }
        }
    }
};
void main()
{
    fish a;
    fish b[5];
    a.set_size(500);
    a.set_price(2);
    a.display();

    for (int i=0;i<=5;i++)
    {
        b[i].set_size(i*2);
        b[i].set_price(i*100);
    }
    for (i=0;i<=5;i++)
        b[i].display();
}

I want to to find out how I send array b, and sorting it. Also I was going to ask about the destructors and where I can put them into my code.

Community
  • 1
  • 1
Bara Hassanya
  • 21
  • 1
  • 1

2 Answers2

0

To swap fish around when you are sorting you should write this

fish tmp = h[o];
h[o] = h[o+1];
h[o+1] = tmp;

You are sorting based on the fish price, but it's the whole fish that should be sorted.

On your other question, there is no need for destructor in this code. Your fish class doesn't need to do any 'clean up' so it doesn't need a destructor.

john
  • 7,897
  • 29
  • 27
0

if you're looking to sort your array by a given element the STL container should be just fine, if not i would use this method

template<class T>
void quickSort(T * elements, unsigned int first, unsigned int last)
{
    if(first < last)                        //make sure params are in bounds
    {
        T t = elements[first];              //t is PIVOT
        unsigned lastLow = first;           //create last low item
        unsigned i;                         //used for loop/swapping
        for(i = first + 1; i <= last; i++)  //run through entire bounds
            if(elements[i] < t)             //if elements is less than Low
            {
                          << " adding one onto lastLow...\n";
                lastLow++;                  //move lastLow up one
                swap(elements,lastLow, i);  //swap lastlow and i
            }
        swap(elements,first, lastLow);      //swap first and lastlow
        if(lastLow != first)                //if lastlow is not first element
            quickSort(elements, first, lastLow - 1);
        if(lastLow != last)                 //if lastlow is not last element
            quickSort(elements, lastLow + 1, last);
    }
}

this is a common quicksort function used to sort an array. Just replace the right variables to represent your data E.g. T * elements becomes Fish * stuff, T t = Elements[first] becomes double price = stuff[first] and so on.

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177