I declare a struct and allocate memory.
Now I call a sort routine and confirm the sorting by following with printf statements. However, when I print out the sorted data, it is not sorted.
Any help would be appreciated.
Code:
typedef struct data
{
char date[days_of_data][9], symbol[9];
float price[days_of_data];
int vol[days_of_data];
int index[days_of_data];
float risk;
} _data ;
daily_data = calloc(num_etfs + 1, sizeof(*daily_data));
for(i = 0; i < num_etfs + 1; i++)
daily_data[i] = *(_data*)calloc(1, sizeof(_data));
Now I call a sort routine and confirm the sorting by following with printf statements. However, when I print out the sorted data, it is not sorted.
Code:
sort (daily_data, num);
void sort(structdata *sorted, int num)
{
int i, j = 0;
int swapped = true;
struct data tmp;
while (swapped)
{
swapped = false;
j++;
for (i = 1; i < num - j; i++)
{
if (sorted[i].risk > sorted[i + 1].risk)
{
// printf("%f %f\n", sorted[i+1].risk, sorted[i].risk);
tmp = sorted[i];
// printf("%f %f\n", tmp.risk, sorted[i].risk);
sorted[i] = sorted[i + 1];
// printf("%f %f\n", sorted[i+1].risk, sorted[i].risk);
sorted[i + 1] = tmp;
// printf("%f %f\n", sorted[i+1].risk, sorted[i].risk);
swapped = true;
}
}
}
for (i = 1; i < num_etfs; i++)
printf("%s %f\n", sorted[i].symbol, sorted[i].risk );
};
Any help would be appreciated.
Last edited by a moderator: