The project I am working on for class is a delivery scheduling program. The first part of the project had use create static arrays to store all of the information for the deliveries. This part has us replace those static arrays with dynamic ones. The arrays are meant to stay within 50-75% full at all times, and so, I am trying to maintain them around 62-64% full to prevent having to resize at boundaries. The problem I am having comes with calculating and resizing the arrays.
void grow_array(int &array_size, int &number_of_deliveries,
string name_of_sender[], string shipping_type[],
double weight[], string id[])
{
double test_percentage;
test_percentage = ((100 * number_of_deliveries) / array_size);
if(number_of_deliveries = 2)
{
array_size = 5;
string *n_t_array = new string[array_size];
string *s_t_array = new string[array_size];
string *i_t_array = new string[array_size];
double *w_t_array = new double[array_size];
for(int i = 0; i < number_of_deliveries; i++)
{
n_t_array[i] = name_of_sender[i];
s_t_array[i] = shipping_type[i];
i_t_array[i] = id[i];
w_t_array[i] = weight[i];
}
delete [] name_of_sender;
delete [] shipping_type;
delete [] id;
delete [] weight;
name_of_sender = n_t_array;
shipping_type = s_t_array;
id = i_t_array;
weight = w_t_array;
n_t_array = NULL;
s_t_array = NULL;
i_t_array = NULL;
w_t_array = NULL;
}
/*if(test_percentage >= 62)
{
int new_array_size = ((100 * number_of_deliveries) / 64);
array_size = new_array_size;
cout << "INSIDE GROW ARRAY/JUST AFTER CALCULATING NEW SIZE/PRINT NEW ARRAY SIZE: " << new_array_size << endl;
cout << "INSIDE GROW ARRAY/JUST AFTER CALCULATING NEW SIZE/PRINT ARRAY SIZE: " << array_size << endl;
string *n_t_array = new string[array_size];
string *s_t_array = new string[array_size];
string *i_t_array = new string[array_size];
double *w_t_array = new double[array_size];
for(int i = 0; i < number_of_deliveries; i++)
{
n_t_array[i] = name_of_sender[i];
s_t_array[i] = shipping_type[i];
i_t_array[i] = id[i];
w_t_array[i] = weight[i];
}
delete [] name_of_sender;
delete [] shipping_type;
delete [] id;
delete [] weight;
name_of_sender = n_t_array;
shipping_type = s_t_array;
id = i_t_array;
weight = w_t_array;
n_t_array = NULL;
s_t_array = NULL;
i_t_array = NULL;
w_t_array = NULL;
}*/
}
As you can see, I've tried calculating the new array size and hard coding it. I don't know where I am going wrong with this and what is leading to the seg fault. The arrays start off as size three (#define SIZE 3).