Member Avatar for rQQt

Hello
I'm writing a little selection sort program in C. Unfortunately the algorithm doesn't work correctly. After the selection sort some float numbers in the array are rounded down to whole ints.
Here's the example:

Code:
  enter input filename:data
// input numbers
 5.000000
 7.000000
 -48.400000
 8.800000
 8.400000
 1.500000
 8.400000
 9.700000
 35.000000
 64.800000

OUTPUT after SELECTIONSORT
//soted output numbers
-48.400000
1.500000
5.000000
7.000000
8.000000
8.000000
8.000000
9.000000
35.000000
64.800000

So the problem is any decimal place on some numbers is discarded and the numbers appears as ints instead of floats.

Here are the snippets from my code:

int main()
{

    double data [20000];
    int dataCnt;

     readFileIntoFloatArray(inputFilename,data,&dataCnt);
    selectionSort(data,dataCnt);



    int j;
    printf("\n\n OUTPUT after SELECTIONSORT");
    for (j=0; j<dataCnt; j++)
    {
        printf("\n %f \n",data[j]);
    }

    return 0;
}


void readFileIntoFloatArray(char filename[], double *dataArray,int *dataCnt)
{
    int cnt=0;
    int i=0;
    FILE *fp;
    fp = fopen(filename, "r");

    while (!feof(fp))
    {

        fscanf(fp, "%lf", &dataArray[i]);   // lf <<<<<<<
        printf(" \n  %lf \n",dataArray[i]);
        i++;
        cnt++;
    }
    fclose(fp);
    *dataCnt=cnt;

}




void selectionSort(double *data, int count)
{
    int i, j, min, temp;
    for (i = 0; i < count - 1; i++)
    {

        min = i;
        for (j = i+1; j < count; j++)
        {
            if (data[j] < data[min])
            {
                min = j;

            }
        }

        temp = data[i];
        data[i] = data[min];
        data[min] = temp;
    }
}

Any help with this would be much appreciated.
Thanks
R.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.