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.

4
  • 3
    std::sort is your friend Commented Nov 1, 2012 at 12:08
  • This h[o].get_price()=h[o+1].get_price(); does not change anything. Even if it did, it would randomly re-assign product prices. Commented Nov 1, 2012 at 12:09
  • it just gave me errors like this one error C2106: '=' : left operand must be l-value\\ assinging me to change h[o].get_price()=h[o+1].get_price() this one Commented Nov 1, 2012 at 12:12
  • You don't need any destructors in this code. Your fish class doesn't need to do any 'clean up' so no need for a destructor. Commented Nov 1, 2012 at 12:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

std::swap works fine too
Yes indeed, so would std::sort, but looks like user1791233 wants to do things for himself.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.