0

I need to add elements to a dynamically allocated Flight array to my program. As initialization of the Flight* allFlights, I set the size of the array as 1 and want to enlarge its size as I need to add more.

Such as:

//Flight.cpp
Flight::Flight(int flightNumber, int rowNumber, int seatNumber)
{
    flightNo = flightNumber;
    rowNo = rowNumber;
    seatNo = seatNumber;
}
//main.cpp
Flight* allFlights = new Flight[1];

I could not find a proper way to extend it as needed, so I tried to approach the problem by having 2 separate integer values as numberOfFlights and capacityOfFlights. I initialized numberOfFlights as 0 and capacityOfFlights to 1, and incremented numberOfFlights by 1 each time I added a new Flight and compared it to capacityOfFlights at each addition. If they were equal, I followed the steps below to extend my array.

if (numberOfFlights != flightCapacity)
{
    Flight newFlight(flightNo, rowNo, seatNo); //Creating new flight object
    allFlights[numberOfFlights] = newFlight;
    numberOfFlights = numberOfFlights + 1;
}
else
{
    Flight newFlight(flightNo, rowNo, seatNo); //Creating new flight object
    //Reallocating dynamic array of allFlight to fit new flights
    flightCapacity = flightCapacity * 2;
    auto* temp = new Flight[flightCapacity];
    copy(allFlights, allFlights + numberOfFlights, temp);
    delete[] allFlights;
    allFlights = temp;

    //Adding the new flight to array
    allFlights[numberOfFlights] = newFlight;

    numberOfFlights = numberOfFlights + 1;
}

I know using std::vector could easily solve this problem, but in this case I'm not allowed to use it.

Is this a good way to solve the problem? I personally find it wasteful of resources by using 2 int values and constantly checking the size by if statements.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
brkyldrn
  • 35
  • 1
  • 7
  • 1
    *" I personally find it wasteful of resources"* - why? You need to know the size of the array one way or another. – UnholySheep Oct 22 '20 at 17:35
  • 1
    Explicit use of `new` and `delete` [is not recommended](https://www.modernescpp.com/index.php/c-core-guidelines-allocating-and-deallocating). Just use `std::vector` and inserting items will be trivial. – Marek R Oct 22 '20 at 17:37
  • @MarekR I guess one of the requirements is no vectors, like the question says. – Anonymous1847 Oct 22 '20 at 17:44
  • 1
    You can use [`std::deque`](https://en.cppreference.com/w/cpp/container/deque) instead of `std::vector`. – MikeCAT Oct 22 '20 at 17:47
  • 2
    *Is this a good way to solve the problem?* -- Create your own generic vector class, where you actually could learn how to properly put together such classes, instead of creating one-off classes such as `Flight`. Then anytime you get the requirements that you "can't use vector", you have a ready-made vector class. – PaulMcKenzie Oct 22 '20 at 17:52

1 Answers1

1

Your code have duplicate parts. Increasing of array and putting data there can be separated.

       if (numberOfFlights == flightCapacity)
       {
          //Reallocating dynamic array of allFlight to fit new flights
          flightCapacity = flightCapacity * 2;
          auto* temp = new Flight[flightCapacity];
          copy(allFlights, allFlights + numberOfFlights, temp);
          delete[] allFlights;
          allFlights = temp;
       }
       Flight newFlight(flightNo, rowNo, seatNo); //Creating new flight object
       //Adding the new flight to array
       allFlights[numberOfFlights] = newFlight;
       numberOfFlights = numberOfFlights + 1;
MikeCAT
  • 73,922
  • 11
  • 45
  • 70