-3

Problem:

    • A student signed up for workshops and wants to attend the maximum
      number of workshops where no two workshops overlap. You must do the
      following: Implement structures:
      1. struct Workshop having the following members: The workshop's start time. The workshop's duration. The workshop's end time.
      2. struct Available_Workshops having the following members: An integer, (the number of workshops the student signed up for). An
        array of type Workshop array having size . Implement functions:
      3. Available_Workshops* initialize (int start_time[], int duration[], int n) Creates an Available_Workshops object and initializes its elements using the elements in the and parameters (both are of size ). Here, and are the respective start time and duration for the workshop. This function must return a pointer to an Available_Workshops object.
      4. int CalculateMaxWorkshops(Available_Workshops* ptr) Returns the maximum number of workshops the student can attend—without overlap.
        The next workshop cannot be attended until the previous workshop
        ends. Note: An array of unkown size ( ) should be declared as
        follows: DataType* arrayName = new DataType[n];

    Your initialize function must return a pointer to an Available_Workshops object. Your CalculateMaxWorkshops function
    must return maximum number of non-overlapping workshops the student
    can attend.
    Sample Input

6
1 3 0 5 5 8
1 1 6 2 4 1
Sample Output

4

Explanation The first line denotes , the number of workshops. The next line contains space-separated integers where the integer is the workshop's start time. The next line contains space-separated integers where the integer is the workshop's duration. The student can attend the workshops and without overlap, so CalculateMaxWorkshops returns to main (which then prints to stdout).

MY CODE:

#include <iostream>

using namespace std;


class Workshop{
    public:
    int start_time{},duration{},end_time{};};

class Available_Workshops
{
    public:
     int n{};
    struct Workshop*arr=new struct Workshop[n];
    ~Available_Workshops()
    {
        delete [] arr;

    }
    void arr_sort();
    void arr_delete(int i);


};
////////////////////////////////////////////////////////////////////////////////////////////
Available_Workshops * initialize(int start_time[],int duration[],int n)
{
    Available_Workshops * x=new Available_Workshops{};
    x->n=n;
    for(int i=0;i<n;i++)
    {
     x->arr[i].start_time=start_time[i];
     x->arr[i].duration=duration[i];
     x->arr[i].end_time=start_time[i]+duration[i];
    }

    return x;

}
///////////////////////////////////////////////////////////////////////////////////////////
void Available_Workshops:: arr_delete(int i)
{

    n-=1;


    for(int j=i;j<n;j++)
    {
     arr[j]=arr[j+1];
    }


}
///////////////////////////////////////////////////////////////////////////////////////////
void Available_Workshops:: arr_sort()
{
for(int i=0;i<n;i++)
{
    for(int j=i+1;j<n;j++)
    {
        if(arr[i].start_time>arr[j].start_time)
            {
                struct Workshop temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
    }
}


}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int CalculateMaxWorkshops(Available_Workshops * x)
{
    x->arr_sort();
    for(int i=0;i<x->n-1;i++)
    {
       for(int j=i+1;j<x->n;j++)
       {
           if(x->arr[i].end_time>x->arr[j].start_time)
           {
               if(x->arr[i].duration>=x->arr[j].duration)
                x->arr_delete(i);
               else x->arr_delete(j);
               j--;
           }
       }
    }
    int y=x->n;
    delete x;
   return y;

}

int main(int argc, char *argv[]) {
    int n; // number of workshops
    cin >> n;
    // create arrays of unknown size n
    int* start_time = new int[n];
    int* duration = new int[n];

    for(int i=0; i < n; i++){
        cin >> start_time[i];
    }
    for(int i = 0; i < n; i++){
        cin >> duration[i];
    }

    Available_Workshops * ptr;
    ptr = initialize(start_time,duration, n);

    cout << CalculateMaxWorkshops(ptr) << endl;
    return 0;
}

My code is not running. It has segmentation fault. Please help me find this error

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Gagan Walia
  • 131
  • 5
  • Line no. 15 seems suspicious and probably throw an exception. `~Available_Workshops() { delete [] arr; }` – Rohan Bari Jun 02 '20 at 17:22

1 Answers1

0

You bug can be seen from the class declaration:

class Available_Workshops
{
public:
    int n{};
    struct Workshop* arr = new struct Workshop[n];

    ~Available_Workshops()
    {
        delete[] arr;

    }
    void arr_sort();
    void arr_delete(int i);


};

Member n gets explicitly initialized to 0. Yet, your initialize function will happily fill in more elements into arr (an array of zero elements) and cause all kinds of undefined behavior.

You really, really want a proper constructor for your class instead of trying to inline initialize the members.

Available_Workshops(int size) :
n(size)
{
    arr = new Workshop[n];
}

Another issue, although not related to your crash is inside your arr_delete function.

for (int j = i; j < n; j++)
{
    arr[j] = arr[j + 1];
}

When j == n-1 on the last iteration of the loop, it will execute arr[n-1] = arr[n]. Accesing arr[n] is undefined behavior since the only valid indices in the array are from [0..n-1]

selbie
  • 100,020
  • 15
  • 103
  • 173