# Thread: Reading a text file and sorting a column of numbers

1. ## 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.

2. You have three nested for loops there. Aren't you suppose to print after you have sorted, instead of while.

3. I was trying to use this code as an example from my text book

Code:
```#include <stdio.h>
#define N 6
void swap(float *a, float *b)
{
float tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
int main()
{
float a[N]={45, 67, 12, 34, 25, 39};
int i, j;
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("%f ", a[i]);
printf("\n");
}
return 0;
}
```

4. Seems like a bad source to learn from. The sort doesn't even work. If you copied that source directly from your text, (and I do mean exactly) then be aware that is not a functional sort and you should check online at the book's companion site for corrected code. Or at least find a working bubble sort online to learn from.

5. I won't disagree with you there, but unfortunately it's not an actual "textbook" just material that one of the professors wrote and there is no companion for it. I guess part of the problem is that the code was written for a given row of numbers and I'm trying to sort a column of numbers from a text file. I just ran across something on the internet that seems to make more sense. Maybe it will work.

6. ok. I'm getting closer! I was able to get the program to sort 1 column of numbers. Now I have to sort 2 columns of numbers in relation to the first column. In other words I have to sort the numbers below with respect to x.
 x y 2.05 293.21 1.60 299.16 1.37 182.14 0.76 163.41 0.99 200.43

I have the following code that sorts one column.

Code:
```#include <stdio.h>
#define N 35
int main()
{
int i, j, c, d;
FILE *fp;
float a[N];
float swap;
fp=fopen("Data.txt", "r");
{
for(i=0; i<N; i++)
fscanf(fp,"%f", &a[i]);
}
for(c=0; c<(N-1); c++)
{
for(d=0; d<(N-c-1); d++)
{
if(a[d]>a[d+1])
{
swap=a[d];
a[d]=a[d+1];
a[d+1]=swap;
}
}
}
for(c=0; c<N; c++)
printf("%8.2f \n", a[c]);

fclose(fp);
return 0;
}```
I could copy this part:
Code:
```    swap=a[d];
a[d]=a[d+1];
a[d+1]=swap;```
and replace a with b for a second column, but it's going to sort that column by itself, not with respect to x. Any thoughts on how I can get the numbers in the second column to sort with the numbers in the first column instead of indepently?

7. Here is my modified code, but it is sorting each column independently instead of with respect to x (or the first column).

Code:
```#include <stdio.h>
#define N 35
int main()
{
int i, j, c, d;
FILE *fp;
float a[N];
float b[N];
float swap;
fp=fopen("Data.txt", "r");
{
for(i=0; i<N; i++)
fscanf(fp,"%f %f", &a[i], &b[i]);
}
for(c=0; c<(N-1); c++)
{
for(d=0; d<(N-c-1); d++)
{
if(a[d]>a[d+1])
{
swap=a[d];
a[d]=a[d+1];
a[d+1]=swap;
}
if(b[d]>b[d+1])
{
swap=b[d];
b[d]=b[d+1];
b[d+1]=swap;
}
}
}
for(c=0; c<N; c++)
printf("%8.2f  %8.2f\n", a[c], b[c]);

fclose(fp);
return 0;
}```
Any thoughts?

8. If x column is the key you are sorting by, and y is not being sorted as a secondary key, then whenever you swap an x value, you also need to swap it's corresponding y column value.

If you are sorting y as a secondary key, then you need to add an if statement that tests if x[i] == x[i+i]. If that is true, then you need to have a comparison of only those two x's corresponding y values, and swap if needed FOR THOSE TWO Y values only.

Line 25 if statement should be completely removed from your code (and the swap code immediately below it, as well).

9. Your statement in the first sentence sounds like what I'm trying to do, but I'm just not sure what format to use to swap the y value with the x value.

10. Originally Posted by txmusic
Your statement in the first sentence sounds like what I'm trying to do, but I'm just not sure what format to use to swap the y value with the x value.

No, no.

You have an if statement that tests whether x[i] and x[i+1], need to be swapped.

If they need to be swapped, then IN ADDITION to swapping the x[i] and x[i+1], you need to also swap the y[i] with the y[i+1], value.