Meks
Meks

Reputation: 3

Dynamically allocated

I need help, to change the size and arr, in the main function to allocate memory dynamically (dynamically allocated)

this program uses looping for integer numbers for the 1st program, while classes and structs are the second program

program_1

#include <iostream>
#include <iterator>
using namespace std;

void display_desc(const int arr[], int size)
{
    int copy_arr[size];
    copy(arr, arr + size, copy_arr);
    int temp;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if (copy_arr[i] > copy_arr[j]) {
                temp = copy_arr[i];
                copy_arr[i] = copy_arr[j];
                copy_arr[j] = temp;
            }
        }
    }
    for (int i = 0; i < size; i++) {
        cout << copy_arr[i] << " ";
    }
    cout << endl;
}

int main()
{
    int size = 5;
    int arr[size];
    for (int i = 0; i < size; i++) {
        arr[i] = i;
    }
    display_desc(arr, size);
}

and program_2 Change the variable that is in int main () to use dynamic memory allocation

#include <iostream>
#include <iterator>
#include <windows.h>
using namespace std;
const int SLOT_ROOM = 5;
class Person {
public:
    Person()
    {
        name = "";
    }
    Person(string names)
    {
        name = names;
    }
    void set_name(string names) { name = names; }
    string get_name() { return name; }
private:
    string name;
};

struct Slot {
    bool blank;
    Person person;
};
class rental {
public:
    Slot used[SLOT_ROOM];
    rental()
    {
        for (int i = 0; i < SLOT_ROOM; i++) {
            Person person;
            used[i].person = person;
            used[i].blank = true;
        }
    }
    int in(const Person person)
    {
        for (int i = 0; i < SLOT_ROOM; i++) {
            if (used[i].blank) {
                used[i].blank = false;
                used[i].person = person;
                cout << "used in position " << i << endl;
                return i;
            }
        }
        cout << "the rental is full" << endl;
        return -1;
    }
    bool out(int i)
    {
        used[i].blank = true;
    }

    Slot* get_rental_list()
    {
        return used;
    }

    void print_person_list()
    {
        cout << endl
             << "List rental" << endl;
        for (int i = 0; i < SLOT_ROOM; i++) {
            cout << "Slot rental to " << i << endl;
            cout << "Name: " << used[i].person.get_name() << endl;
            cout << "Avail: " << used[i].blank << endl
                 << endl;
        }
    }

private:
    int SLOT_ROOM = 2;
    string time_rental;
};
int main()
{

    rental rental;

    Person person_1("make");
    Person person_2("angel");
    rental.in(person_1);
    rental.in(person_2);
    rental.print_person_list();
    rental.out(2);
    rental.print_person_list();
    rental.in(person_2);
    rental.print_person_list();
}

please help, I don't understand about using dynamic memory allocation

I still learn c ++

Upvotes: 0

Views: 98

Answers (1)

john
john

Reputation: 88017

Change

int arr[size];

to

int* arr = new int[size];

You need to make the same change to int copy_arr[size]; in display_desc.

You also need to delete[] memory once you've finished with it.

delete[] arr;

at the end of main, and delete[] copy_arr; at the end of display_desc.

In the second question it's harder to understand what you want. Why do you want to use dynamic allocation? Your code looks perfectly good as it is. You also don't say which variable it is that you want to use dynamic allocation.

Upvotes: 1

Related Questions