selection sorting issues

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2006
Posts: 1
Reputation: rQQt is an unknown quantity at this point 
Solved Threads: 0
rQQt rQQt is offline Offline
Newbie Poster

selection sorting issues

 
0
  #1
Nov 19th, 2006
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.
Last edited by rQQt; Nov 19th, 2006 at 7:02 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: selection sorting issues

 
0
  #2
Nov 19th, 2006
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1108 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC