How to sorting by the density

i got problem with my program,i have tried to solve this problem but didnt get solution for along time… please help me. the problem comes from sorting, i want to make sort by density this my code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>

void Input_Data(int item[], int jum);
void Show_Data(int item[], int jum);
void Sort_Density(int *no, float *weight, float *profit, int *pieces, double *density, int c);

typedef struct
{
    int b, no;
    float w, p;
    double d;
    char note[15];
}item;
item item1[100], item2[100];

float knapsack;
int bnyk, k[100], n[100];
double o[100];
float l[100], m[100];

int main()
{

    int X[100];


    printf("capacity : "); scanf("%f", &knapsack);
    printf("sum of variation : "); scanf("%d", &bnyk);
    printf("\n");
    Input_Data(X,bnyk);
    Show_Data(X,bnyk);
    int i, j, c;


    float totwd, totpd;
    c=0, totwd=0, totpd=0;
    for(i=0; i<bnyk; i++)
    for(j=0; j<item1[i].b; j++)
    {
        k[c]=item1[i].no;
        l[c]=item1[i].w;
        m[c]=item1[i].p;
        n[c]=item1[i].b;
        o[c]=item1[i].d;
        c++;
    }
    Sort_Density(k,l,m,n,o,c);
    for(i=0; i<c; i++)
    {
        item2[i].no=k[i];
        item2[i].w=l[i];
        item2[i].p=m[i];
        item2[i].b=n[i];
        item2[i].d=o[i];
    }
    printf("SORT DATA BY DENSITY\n");
    printf("Objek     weight     profit     note      density \n");
    printf("--------------------------------------------------\n");
    for(i=0; i<c; i++)
    {
        printf("%d  %10f %10f %10s %10f\n", item2[i].no, item2[i].w, item2[i].p, item2[i].note, item2[i].d);
    }



}
void Input_Data(int item[], int jum)
{
    int i;
    for(i=0; i<jum; i++)
    {
        printf("w%d : ", i+1); scanf("%f", &item1[i].w);
        printf("p%d : ", i+1); scanf("%f", &item1[i].p);
        printf("tot%d : ", i+1); scanf("%d", &item1[i].b);
        item1[i].no=i+1;
    }
}

void Show_Data(int item[], int jum)
{
    int i;
    printf("\n");
    printf("Objek(i)    weight(w)     Profit(p)    pieces(b)    Density(d)\n");
    printf("-------------------------------------------------------------\n");
    for(i=0; i<jum; i++)
    {
        item1[i].d=item1[i].p/item1[i].w;
        printf("%2d %12f %12f %12d %12f\n", item1[i].no, item1[i].w, item1[i].p, item1[i].b, item1[i].d);
    }
}

void Sort_Density(int *no, float *weight, float *profit, int *pieces, double *density, int c)
{
    int i, j, temp;
    for(i=0; i<c-1; i++)
        for(j=i; j<c; j++)
        {
            if(density[i]<density[j])
            {
                temp=no[i];
                no[i]=no[j];
                no[j]=temp;
                temp=weight[i];
                weight[i]=weight[j];
                weight[j]=temp;
                temp=profit[i];
                profit[i]=profit[j];
                profit[j]=temp;
                temp=pieces[i];
                pieces[i]=pieces[j];
                pieces[j]=temp;
            }
        }
}

when i input this

and the result this

so, why does my sorting by density didnt work? please help me…

1 Like

the error is that in ur sort function u have not sorted the density array…ur corrected code…[LINK][1]…hope this helps…:slight_smile:

EDIT: also change the data-type of ur temp variable…if u take floating point inputs then it may cause errors…!!!
[1]: http://ideone.com/TIKdN5

1 Like

+1 for your question description

thank you… you saved me :smiley:

1 Like

glad could help…:slight_smile: