| | |
selection sorting issues
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 1
Reputation:
Solved Threads: 0
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:
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:
Any help with this would be much appreciated.
Thanks
R.
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.
![]() |
Similar Threads
- Selection Sort in java (Java)
- date (C++)
- Vectors, Functions, and Sorting... oh my! (C++)
- Sorting a selection (Visual Basic 4 / 5 / 6)
- Need Help with ArrayList sorting (Java)
Other Threads in the C Forum
- Previous Thread: Help with structures
- Next Thread: about VC+MySQL problem:)
Views: 1108 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






