Reading a text file and sorting a column of numbers
I am reading in a text file that has a column with 35 rows of numbers. I am supposed to read the text file and then sort it from smallest number to largest number. My code is sorting the numbers properly, but it is printing them out 8 times. Can anyone see any glaring problems?
Code:
#include <stdio.h>
#define N 35
void swap(float *a, float *b)
{
float tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
int main()
{
int i, j, k;
FILE *fp;
float a[N];
fp=fopen("Data.txt", "r");
for(i=0; i<N; i++)
fscanf(fp,"%f", &a[i]);
for(j=1; j<N; j++)
{
for(i=0; i<N-j; i++)
if(a[i]>a[i+1]) swap(&a[i], &a[i+1]);
for(i=0; i<N; i++) printf("%8.2f \n", a[i]);
}
fclose(fp);
return 0;
My next task is to do the same thing for 2 columns of numbers, but I was trying to get it to work with at least the one column.