Hello everyone.
First of all i'm new here, and i registered particulary for this problem of mine i can't figure.
In the code shown below, in the function void printExpensiveThanT(..) i'm supposed to print out the destination, distance and the price for the offers which are more expensive than the offer T in the function, sorted in ascending order by the distance value.
I'm not sure what should i use to sort them, i experimented something with vectors but it didn't work out so i deleted it.
Any help would be appreciated.

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

class Transport {
    char destination[100];
    int basePrice;
    int distance;
    Transport() {}
    Transport(char *destination, int basePrice, int distance) {
        strcpy(this->destination, destination);
        this->basePrice = basePrice;
        this->distance = distance;
     virtual ~Transport() {}
    virtual int priceTransport() = 0;
    friend bool operator<(const Transport &t1, const Transport &t2) {
        return t1.distance<t2.distance;
    int getDistance(){ return distance; }
    char *getDestination() { return destination; }
    int getPrice() { return basePrice; }

class AutomobileTransport : public Transport {
    bool ifDriver;
    AvtomobilTransport() {}
    AvtomobilTransport(char *destination, int basePrice,int distance, bool ifDriver) : Transport(destination,basePrice,distance) {
        this->ifDriver = ifDriver;

    void setIfDriver(bool ifDriver) {
        this->ifDriver = ifDriver;
    bool getIfDriver() {
        return ifDriver;

    int priceTransport() {
        if(ifDriver) {
        return basePrice;
    friend bool operator<(const AutomobileTransport &a1, const AutomobileTransport &a2) {
        return a1.distance<a2.distance;

class VanTransport: public Transport {
    int passengers;
    KombeTransport() {}
    KombeTransport(char *destination, int basePrice, int distance, int passengers) : Transport(destination, basePrice, distance) {
        this->passengers = passengers;
    void setPassengers(int passengers) {
        this->passengers = passengers;
    int getPassengers() {
        return passengers;
    int priceTransport() {
        for(int i = 0; i < passengers; i++) {
        return basePrice;
    friend bool operator<(const VanTransport &k1, const VanTransport &k2) {
        return k1.distance<k2.distance;

void printExpensiveThanT(Transport **offers,int n,AutomobileTransport &T) {
    Transport *tmp;
    for(int i = 0; i <= n; i++){
        if(offers[i]->priceTransport() > T.priceTransport())

            cout<<offers[i]->getDestination()<<" "<<offers[i]->getDistance()<<" "<<offers[i]->getPrice()<<endl;    

int main() {

    char destination[20];
    int type,price,distance,passengers;
    bool driver;
    int n;
    Transport  **offers;
    offers=new Transport *[n];

    for (int i=0; i<n; i++) {

        if (type==1) {
            offers[i]=new AutomobileTransport(destination,price,distance,driver);

        } else {
            offers[i]=new VanTransport(destination,price,distance,passengers);


    AutomobileTransport at("Ohrid",2000,600,false);

    for (int i=0; i<n; i++) delete offers[i];
    delete [] offers;
    return 0;

Edited by Vlatko_1

2 Years
Discussion Span
Last Post by AssertNull

There are a lot of different sorting algorithms. Since you are reading in the values one at a time from user input, you start with a sorted list of one element. Given that, I would say that the Insertion Sort is your easiest approach. You have a sorted list and you never let it get unsorted. You'll need a swap variable for just about any sort, including the Insert Sort, so I would read the input into that swap variable rather than the end of the array since the end of the array will be overwritten anyway.

So let's say you have 6 elements in your ordered array (looks like you are sorting on distance). Your array indexes are 0 to 5. You read in your 7th element from cin into your swap/temporary variable. Let's say the distances (in order) are: 3, 6, 9, 12, 15, 18. The new element's distance is 10, so it's bigger than 9 and less than 12, so it belongs at array index 3. You'll move array elements 3 to 5 to be elements 4 to 6 either by iterating through a loop or by using memmove http://www.cplusplus.com/reference/cstring/memmove/
You then insert the new element to be array index 3. You now have a sorted array of seven elements. Do the same thing for the 8th element, then the ninth, etc. Your array never gets out of order.


That'd be my vote.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.