[Solved] Output in another file [closed]


The first issue is that the temp, temp2, and temp3 are int, but you are assigning double values to them, and then re-assigning these values back to double variables. You should make these double.

You might want to consider using qsort() to sort these objects.

#include <stdlib.h> // for qsort

int compare_products(const void *p1, const void *p2)
{
    const struct product *product1 = p1;
    const struct product *product2 = p2;

    // swap product1 and product2 around to reverse sort order
    return (int)(product1->sale_volume - product2->sale_volume);
}

int main()
{
     // ...

     qsort(products, line_amount, sizeof(struct product), compare_products);

     for(i = 0; i < line_amount; i++){
         fprintf(pFile, "%s \t  %lf \t %lf \t %lf\n", products[i].name, products[i].unit_price, products[i].tot_pounds_sold, products[i].sale_volume);
     }

     // ...    
}

Since you must use the selection_sort function, it can be made quite a bit simpler due to struct objects being assignable.

void selection_sort(struct product products[], int n)
{
    int i, largest = 0;

    if (n == 1) {
        return;
    }

    for (i = 0; i < n; i++) {
        if (products[i].sale_volume > products[largest].sale_volume) {
            largest = i;
        }
    }

    struct product temp = products[n - 1];
    products[n - 1] = products[largest];
    products[largest] = temp;

    selection_sort(products, n - 1);
}

4

solved Output in another file [closed]