-1

I have class City with following private data: name of city, width,length and height of the city. I have to make dynamic array, which is inserted by constructor by default- City(),when the programs starts.Then the program uses method output() and prints inserted array of cities.

I should use bubble sort to sort the cities by their length. And when this is done, the program should show the sorted cities in increasing lengths.

The problem is that my data are in private(in public everything works excellent but principle of capsulation is violated!) so I can't do bubble sort.

I tried to do another dynamic array of type double double Lengths[n], which content is lengths of first array. Then I do sorting, but program prints only sorted lengths and this is not my goal.

I should print the names of cities sorted by their lengths.

Code:

class City{
private: char *name;
         double width;
         double length;
         double height;
public:void Output();
    City();
    ~City();
    double GetLength()
    {
        return length;
    }
    double GetWidth(){ return width; }
    double GetHeight(){ return height; }
    char GetName(){ return *name; }

};
City::City()
{
    char ime[20];
    cout << "Name= ";
    cin >> ime;
    name = new char[strlen(ime) + 1];
    for (int i = 0; i <=strlen(ime); i++)
        name[i] = ime[i];

    cout << "Width= ";
    cin >> width;
    cout << "Length= ";
    cin >> length;
    cout << "Height= ";
    cin >> height;
}
void City::Output()
{
    cout << "Name is: " << name << endl;
    cout << " Width is: " << width <<" deg"<< endl;;
    cout << " Length is: " << length << " deg"<<endl;
    cout << " Height is: " << height <<" m"<<endl;
    return;
}
City::~City()
{
    cout << " " << endl;
cout << "Destructor of City!" << endl;
delete[] name;
}

int main()
{
    //City town;

    //town.Input();
    //town.Output();
    int n;
    City *mA;
    cout << "Input number of cities: " << endl;
    cin >> n;
    mA = new City[n];
    for (int j = 0; j < n; j++)
    {
        mA[j].Output();
    }
    cout << "Cities from west to east, sorted by their length" << endl;
    double *Lengths = new double[n];
    for (int j = 0; j < n; j++)
    {
        Lengths[j] = mA[j].GetLength();

    }
int k = 0;//counter
    double max = Lengths[0];
    for (int j = 1; j < n; j++)
    {
        if (Lengths[j - 1] >Lengths[j])
        {
            max = Lengths[j - 1];
            Lengths[j - 1] = Lengths[j];
            Lengths[j] = max;
        }
    }


    for (int j = 0; j < n; j++)//cycle for output
    {
        mA[j].Output();
    }


    delete[]mA;
    return 0;

}
  • You have broken the rule or 0, 3, 5. Add a minimum a copy constructor and `operator=`. Possible duplicate of [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Richard Critten Dec 07 '17 at 18:22
  • 1
    I assume this is just to try implementing bubble sort as a learning exercise. Because, in any real program, you should just use [std::sort](http://en.cppreference.com/w/cpp/algorithm/sort). – Jesper Juhl Dec 07 '17 at 18:25

3 Answers3

0

I don't see a direct question but I can assume majority of the question by your story. You have multiple options on how to solve this case. One of the simple ways to solve this is create a function within the object that allows you to get the length of a member of an object for example in your case it would be name of city.

Create a method inside the object that you can call to return a private method's length. Create a loop that calls this method and checks each element side by side until you can't refine it any longer. Is there a specific reason your using char instead of string for name?

Nick Macroy
  • 16
  • 1
  • 8
0

I'm not entirely sure of what you're asking.

However, from what I can tell your main issue is that you can't sort because you're trying to compare two private variables from two objects.

If the objects are placed into an Array of type city, you can bubblesort by length however you would be required to use the getters in order to reference the variables that are private during sorting.

For example (not exact syntax)

if(cityArray[0].getLength() < cityArray[1].getLength())
{
 //Do Something
}
0

As I can't quite comment on your response, I will give you a few bits of advice. First in the line of:

Array[j]=mA[j].GetName() ;

You have a random space which may be a copy change and relatively minor but for reading purposes that is wrong.

Second your naming conventions are really something you should work on. I should be able to read a variable and understand what it means but instead I struggle to understand what a variable named mA means.

Third your else clause does nothing literally. Your not moving any parts of the array if that is your intention my saying this:

mA[j+1];

you are simply targeting the element in the array that is above the iteration. I see you stuck with your idea of making something similar to a parallel array for the bubble sort, and that is fine but you lack any action in the first loop.

This is not bubble sort what so ever as you are simply going through each iteration and checking if the element in Lengths is equal in length to the element in mA and then storing that element in array but your else statement does nothing.

Your loop should look similar to something like this but I'm gonna get rid of the character array and the extra array for some reason as it is unnecessary and lets say you start out with an array of your objects:

        if(myObjects[i].GetLength() > myObjects[i+1].GetLength()) //Shortest to longest name or vice versa?
        {
             //Store myObject[i] in temp spot
             //myObject[i] = myObject[i+1]
             //myObject[i+1] = temp Storage
        }

This will give you a bubble sort of the objects on the first round. Of course your going to have to find out how to iterate through the array in loop to verify all of the elements have been sorted correctly as this will of course take many iterations for bubble sort.

Nick Macroy
  • 16
  • 1
  • 8