-1

// lab5.cpp : Defines the entry point for the console application. // How can I sort the array object with its distance? I am not so clear on how to use the assignment operator function either... any help would be rally appreciated..

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
class Leg {
public:
    Leg(const char *nameS, const char *nameE,const double totalDis): nameOfstartingCity(nameS), nameOfendingCity(nameE), totalDistance(totalDis) {};
    double getDistance(double);
    void  outPut(ostream &);
    Leg &operator=(const Leg&);

private:
    const char *nameOfstartingCity;
    const char *nameOfendingCity;
    const double totalDistance;
};

int main()
{
    int arraySize = 0;
    const int SIZEOFARRAY = 11;
    Leg myArray[SIZEOFARRAY] = {    Leg("Pinole", "Richmond", 3),
                                    Leg("Redwood City", "Palo Alto", 21.6),
                                    Leg("Berkeley", "Oakland", 4.3),
                                    Leg("Concord", "Walnet Creek", 8.3),
                                    Leg("San Francisco", "Oakland", 23),
                            Leg("South San Francisco", "San Mateo",15.3),
                                    Leg("Palo Alto", "Sunnyvale", 11.2),
                                    Leg("San Jose", "Milpitas", 15.2) ,
                                    Leg("Fremont", "Union City", 16.3) ,
                                    Leg("San Leandro", "Piedmont", 19.4),
                                    Leg("Pinole", "San Jose", 40.1) };


    arraySize = sizeof(myArray) / sizeof(myArray[0]);

    for (int x = 0; x < arraySize; x++) {
        for (int j = x + 1; j < arraySize; j++)
            if (myArray[j].getDistance < myArray[x].getDistance)
                swap(myArray[x], myArray[j]);

    }

    for (int i = 0; i < arraySize; i++)
    {
        myArray[i].outPut(cout);
    }

    cout << "\n\n\n\n" << endl;

    system("PAUSE");
    return 0;
}
double Leg::getDistance(double x)
{
    x = totalDistance;
    cout << x << "   From getDistance " << endl;

     return x;
}

void Leg::outPut(ostream &x)
{
        x << "Leg : " << nameOfstartingCity << " to " << nameOfendingCity << 
         ", " << totalDistance << " miles." << endl;
}
Leg& Leg::operator=(const Leg &copyThis)
{
    nameOfstartingCity = copyThis.nameOfstartingCity;
    nameOfendingCity = copyThis.nameOfendingCity;
    return *this;
}
cleblanc
  • 3,678
  • 1
  • 13
  • 16
user7723264
  • 21
  • 10
  • http://en.cppreference.com/w/cpp/algorithm/sort scroll down to the example at the bottom and note how it is using a comparison function object or a lambda expression. Basically you provide a function that knows what members of `Leg` to compare. You could also implement `operator <` for `Leg` – user4581301 Mar 23 '17 at 21:17
  • Off topic: `SIZEOFARRAY` and `arraySize = sizeof(myArray) / sizeof(myArray[0]);` should always be the same. – user4581301 Mar 23 '17 at 21:22

1 Answers1

0

Modify Leg to

  1. Fix getDistance There is no point to passing in a variable to a getter or doing any of the swapperoo nonsense.
  2. Fix the totalDistance There is no point to making this const. It makes the assignment operator impossible which makes swap and sort impossible.
  3. Remove the broken assignment operator

New Leg

class Leg {
public:
    Leg(const char *nameS, const char *nameE,const double totalDis): nameOfstartingCity(nameS), nameOfendingCity(nameE), totalDistance(totalDis) {};
    double getDistance()
    {       
        return totalDistance;
    }
    void  outPut(ostream &);

private:
    const char *nameOfstartingCity;
    const char *nameOfendingCity;
    double totalDistance;
};

Then call the revised getDistance function properly. You always need brackets on a function call whether the function has arguments or not.

for (int x = 0; x < arraySize; x++) {
     for (int j = x + 1; j < arraySize; j++)
         if (myArray[j].getDistance() < myArray[x].getDistance())
             swap(myArray[x], myArray[j]);

That gets the code working. Now to get it working right.

  1. Add a < operator to Leg
  2. Replace the output routine with a standard << operator
  3. Format it so it's easier to read

New Leg

class Leg
{
public:
    Leg(const char *nameS,
        const char *nameE,
        const double totalDis) :
            nameOfstartingCity(nameS),
            nameOfendingCity(nameE),
            totalDistance(totalDis)
    {
    }
    double getDistance()
    {
        return totalDistance;
    }
    friend ostream & operator<<(ostream &x, const Leg &leg)
    {
        x << "Leg : " << leg.nameOfstartingCity << " to " << leg.nameOfendingCity
                << ", " << leg.totalDistance << " miles.";
        return x;
    }

    bool operator<(const Leg & rhs) const
    {
        return totalDistance < rhs.totalDistance;
    }

private:
    const char *nameOfstartingCity;
    const char *nameOfendingCity;
    double totalDistance;
};

Then

  1. Get rid of all of the array sizing nonsense because magic numbers build bad programs more ways than I can count.
  2. Replace the naive sort with std::sort. Why reinvent the wheel?
  3. Use a range-based for loop so we don't need any magic numbers.
  4. Use the new << operator

New main

int main()
{
    Leg myArray[] =
    { 
      Leg("Pinole", "Richmond", 3),
      Leg("Redwood City", "Palo Alto", 21.6),
      Leg("Berkeley", "Oakland", 4.3),
      Leg("Concord", "Walnet Creek", 8.3),
      Leg("San Francisco", "Oakland", 23),
      Leg("South San Francisco", "San Mateo", 15.3),
      Leg("Palo Alto", "Sunnyvale", 11.2),
      Leg("San Jose", "Milpitas", 15.2),
      Leg("Fremont", "Union City", 16.3),
      Leg("San Leandro", "Piedmont", 19.4),
      Leg("Pinole", "San Jose", 40.1) 
    };

    std::sort(std::begin(myArray), std::end(myArray));

    for (Leg &leg : myArray)
    {
        cout << leg << '\n';
    }

    cout << "\n\n\n\n" << endl;

    return 0;
}
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • WOW you are awesome.. thank you so so so........ much I learned a lot for this... each problem is explained in detail.. wow thanks again.. :) – user7723264 Mar 23 '17 at 23:38