hi,

i have a very simple problem with my program and just need a push in the right direction and will be much appreciative of your help.

i have a bucket sort program which works fine. well i say fine, its fine when it is sorting the numbers initialised at the start of the program but when i want it to read in a file of numbers, its not playing ball. i can't see what the problem is. i'm using dev c++.

here is my full code;

/*Bucket sort */

#include<stdio.h>
#include<conio.h>
#define N 5000
/*Defining the strcuture for each node in the list*/
typedef struct node
{
float value;
struct node *link;
} node;

int main(void)
{

/*An array of structures,pointers to the structure*/

node counter[10], *n2,*n1;
char buff[BUFSIZ];
//float a[N];
     FILE *in;
     in=fopen("rand.txt","r");
float a[10] = {0.79,20.13,10.16,0.64,0.64,0.20,0.89,0.53,0.71,0.42};
float fa[10],temp;
int j,k=0;
float n,x;
system("cls");

/*Initializing the elements of the counter arrray to zero*/
int i=0;
//     while(fgets(buff,BUFSIZ,in)!=NULL){
  //         a[i]=atof(buff);
	//       i++;
     //}
for(i=0;i<10;i++)
{
counter[i].value = 0;
counter[i].link  = 0;
}

/*Redcuing the value equal to index of an array*/

for(i=0;i<10;i++)
{
//n = a[i];
//j = n * 100;
//j = j/10;

/*Moving the values in to the appropriate bucket*/

/*If there are no elements in the bucket*/

if(counter[j] . value ==0 && counter[j] . link == 0)
counter[j] . value = a[i];

else
{
/*If there is only one element at that index*/
if(counter[j].link==0 && counter[j] .value != 0)
{
counter[j].link=(node *) malloc(sizeof(node));
n2 = counter[j].link;
n2 -> link = 0;
n2 -> value =a[i];
continue;
}
/*If there is already an node in the list at this index*/
n2 = counter[j].link ;
while(n2 -> link !=0 )
{
n2 = n2 -> link;
}
n2 -> link =(node *) malloc(sizeof(node));
n2 = n2 -> link;
n2 -> link=0;
n2 -> value = a[i];
}
}

/*Sorting of all the buckets in order*/

printf("The sorted values after merging all buckets in order are: ");

for(i=0;i<10;i++)
{
/*No nodes at that index*/
if(counter[i] . link ==0 && counter[i] . value == 0)
continue;
else
{
n1 = &counter[i];
n2 = &counter[i] ;

/*If there is more than one node at this Index*/

if(n2 -> link != 0)
{
while(n1!=0)
{
while(n2!= 0)
{
if(n1 -> value   >  n2 -> value)
{
temp =n1 -> value;
n1 -> value =n2 -> value;
n2 -> value =temp;
}
n2 = n2 -> link;
}
n2 = n1 -> link;
n1 = n1 -> link;
}
n1 = &counter[i];
for(; n1!=0; k++)
{
fa[k] = n1 -> value;
n1 = n1 -> link;
}

}

/*If there is only one node at this Index*/

else
{
fa[k] = counter[i].value;
k=k+1;
}
}
}
for(i=0;i<10;i++)
printf("\n%f",fa[i]);
getch();
}

this is the working program where it is using the array initialised in the program. below is what i am doing to make it read in the file of numbers

node counter[10], *n2,*n1;
char buff[BUFSIZ];
float a[N];
     FILE *in;
     in=fopen("rand.txt","r");
//float a[10] = {0.79,20.13,10.16,0.64,0.64,0.20,0.89,0.53,0.71,0.42};
float fa[10],temp;
int j,k=0;
float n,x;
system("cls");

/*Initializing the elements of the counter arrray to zero*/
int i=0;
     while(fgets(buff,BUFSIZ,in)!=NULL){
         a[i]=atof(buff);
	       i++;
     }

but its not working for some reason.

also the external file i am readin in has these exact numbers in it

91.175598
68.562500
96.664803
29.593500
43.156502
34.241001
59.178200
85.542702
81.852798
23.628799

please someone just push me in the right direction with this. sorry if the code is messy.

sorry guys, just realised i wasn't including the stdlib.h library while using atof!!!

This question has already been answered. Start a new discussion instead.