Like Tree1Likes
  • 1 Post By Subsonics

Reading a text file and sorting a column of numbers

This is a discussion on Reading a text file and sorting a column of numbers within the C Programming forums, part of the General Programming Boards category; I am reading in a text file that has a column with 35 rows of numbers. I am supposed to ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    13

    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. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You have three nested for loops there. Aren't you suppose to print after you have sorted, instead of while.
    MK27 likes this.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    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. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    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. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    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. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    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. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    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. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    13
    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. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by txmusic View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a column from a .dat file
    By ca2004 in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2011, 10:33 AM
  2. Replies: 2
    Last Post: 11-25-2011, 05:42 PM
  3. Reading numbers from a text file
    By wolfindark in forum C++ Programming
    Replies: 12
    Last Post: 03-24-2007, 01:57 PM
  4. Reading / input column of numbers into an array
    By boyfarrell in forum C Programming
    Replies: 6
    Last Post: 08-28-2005, 10:24 AM
  5. Writing to a column in a text file
    By olland in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 05:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21