load_data is called twice. I know that the first time I call it the proper data is loaded in to data_close_array and average. The data gets replaced the second time I call load_data is called although its passed a pointer to a different array.
Code:
if ( (data_open_array = (float **)calloc(number_of_stocks, sizeof(float*))) == NULL)
printf("no mem data_open_arrays\n");
else
for (i = 0; i < number_of_stocks; i++)
if ( (data_open_array[i] = (float *)calloc(total_back_test_period, sizeof(float))) == NULL)
printf("no mem data_open array\n");
if ( (average = (float **)calloc(number_of_stocks, sizeof(float*))) == NULL)
printf("no mem data_open_arrays\n");
else
for (i = 0; i < number_of_stocks; i++)
if ( (average[i] = (float *)calloc(total_back_test_period, sizeof(float))) == NULL)
printf("no mem data_open array\n");
if ((data_close_array = (float **)calloc(number_of_stocks, sizeof(float*))) == NULL)
printf("no mem data_close_arrays\n");
else
for (i = 0; i < number_of_stocks; i++)
if ((data_close_array[i] = (float *)calloc(total_back_test_period, sizeof(float))) == NULL)
printf("no mem data_close array\n");;
index_open_array = (float **)calloc(number_of_indices, sizeof(float*));
for (i = 0; i < number_of_indices; i++)
index_open_array[i] = (float *)calloc(total_back_test_period, sizeof(float));
index_close_array = (float **)calloc(number_of_indices, sizeof(float*));
for (i = 0; i < number_of_indices; i++)
index_close_array[i] = (float *)calloc(total_back_test_period, sizeof(float));
stock_list = load_data(dir, list, data_open_array,data_close_array, date_list, average);
index_list = load_data(dir , list,index_open_array,index_close_array, date_list, average);
//NO LONGER HAVE DATA IN ARRAYS THAT WERE PRINTED OUT WHILE RUNNING stock_list = load_data
for ( i = 0; i < total_back_test_period; i++)
printf("%s %f %f\n", date_list[i], data_close_array[0][i], average[0][i]);
char** load_data(char *dir_path, char* list, float **open_array,float **close_array, char **date_list, float **average)
{
FILE *FILELIST, *SEC;
char *name, *full_path, *line_of_data, *close_s, **symbol_list, *cmd;
int day = 0, i, j, symbol_number = 0;
float close = 0, open = 0;
close_s = (char *) calloc (30, sizeof(char));
name = (char *) calloc (100, sizeof(char));
full_path = (char *) calloc (150, sizeof(char));
line_of_data = (char *) calloc (200, sizeof(char));
cmd = (char *) calloc (200, sizeof(char));
symbol_list = (char**) calloc(number_of_indices, sizeof(char*));
for (i = 0; i <number_of_indices; i++)
symbol_list[i] = (char *)calloc(10, sizeof(char));
if( (FILELIST = fopen(list,"r")) == NULL)
{
printf( "couldn't open file list\n");
}
while (fscanf(FILELIST, "%s\n", name) != EOF && symbol_number < number_of_indices)
{
{
strcpy(full_path, dir_path); strcat(full_path, name);
if ( (SEC = fopen(full_path, "r")) == NULL)
printf( "couldn't open index file %s\n", full_path);
else
if (PRINT)
printf( "opened index file %s\n", full_path);
day = 0;
while ( (fgets(line_of_data, 200, SEC)) && day < total_back_test_period)
{
close_s = strtok (line_of_data,","); //symbol
if ( day == 0)
{
strcpy(symbol_list[symbol_number],close_s);
// printf("%s\n",symbol_list[symbol_number]);
}
close_s = strtok (NULL, ",");
close_s = strtok (NULL, ",");// date
if (strcmp(close_s,date_list[total_back_test_period - 1 - day]) == 0)
{
if (strcmp(close_s, "<TICKER>") != 0)
close_s = strtok (NULL, ",");
close_s = strtok (NULL, ",");
open = (float)atof(close_s);
for (i = 0; i < 3; i++)
close_s = strtok (NULL, ",");
close = (float)atof(close_s);
close_array[symbol_number][total_back_test_period - 1 - day] = close;
open_array[symbol_number][total_back_test_period - 1 - day] = open;
day++;
}
}
fclose(SEC);
}
symbol_number++;
}
fclose(FILELIST);
// now do averages
for (j = 0; j < symbol_number; j ++)
{ day = total_back_test_period;
while (day > -1 )
{
for (i = 0; i < average_period; i++)
average[j][day] += close_array[j][day - i]/average_period;
day--;
}
}
return (symbol_list);
}