Search code examples
cfile-iocolumnsorting

How to arrange the data of a .txt file for correspond with each value of the other data in C


My problem is this I have a txt file name file.txt with the data. I need to input this for do some calculations,this how the code run:

Please enter input file name
file.txt
File file.txt
STUDENT NAME STUDENT NO.  SUBJECT A    SUBJECT B    
JOAN             1           70.5           85.0
TANIA            2           49.0           75.0
TOM              3           53.0           54.0
JEFF             4           80.0           49.5
SUSAN            5           89.0           90.0
KATHY            6           99.0           55.0
RAYMOND          7           22.5           75.0

This is the data given in the file. So for example if I arrange Subject_A column in ascending order. Column Name and Subject_B Not arrange corresponding with Subject_A, this how the code continue:

Please enter a command (enter h for help): h
a/1 to obtain all the students that got diploma
b/2 to arrange subject A in ascending order
c/3 to calculate the average and standard deviation
d/4 to save all the above results in an output file
e to exit
2
Subject A in ascending order
STUDENT NAME SUBJECT A    SUBJECT B    
JOAN             22.5        85.0
TANIA            49.0        75.0
TOM              53.0        54.0
JEFF             70.5        49.5
SUSAN            80.0        90.0
KATHY            89.0        55.0
RAYMOND          99.0        75.0

And here is my problem, the first one is 22.5 and corresponds to Raymond ( in the .txt given see above ) and the note B 75 and in the program I get it is Joan with the note of 22.5 and 85 in the b. Because I only order A and the rest of the data is not corresponding. Each student has to correspond with her grade in the subject.

This is my code for order subject A but not for order each row with each student correspondent:

void option_ordering_A(){
    while ( !feof( ptrCf ) ) {
        //printf( "%.1f\n", subject_A );
        a[i]=subject_A;
        fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
        i=i+1;
        } /* end while */
          for (i = 0; i < (SIZE - 1); i++) 
          { 
            for (j = i + 1; j < SIZE; j++) 
            { 
              if (a[j] < a[i]) 
              { 
                temp = a[j]; 
                a[j] = a[i]; 
                a[i] = temp; 
              }
            } 
          }
          /* Show Numbers Sorted */
          rewind( ptrCf );

          printf( "%-10s%-13s%-13s\n", "\nSTUDENT NAME ", "SUBJECT A" , "SUBJECT B");
          fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
          for (i = 0; i < SIZE; i++) 
          { 
            printf( "%-13s%8.1f%12.1f\n", student_name, a[i], subject_B );
            fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
            //printf("%.1f, ", a[i]); 
          } 
          printf("\n\n\n");

}

edit: variable declarations, and functions.

float a [SIZE];
float b [SIZE];
int j;
int i = 0;
float temperature;
char student_name [30];
character arrays typedef [30];
matrices c [SIZE];
int student_No;
double subject_A;
double subject_B;

and the functions part:

//////////////////////FUNTIONS//////////////////////////////////////////////////////////////

/* Read the student's name, student's number, Subject A and Subject B */
void Read_File(){
printf( "%-10s%-13s%-13s%-13s\n", "\nSTUDENT NAME ", "STUDENT NO.", "SUBJECT A" , "SUBJECT B");
fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
}

////////////////////////////////////7//Show File///////////////////////////
void show_File(){
while ( !feof( ptrCf ) ) {
printf( "%-13s%5d%15.1f%15.1f\n", student_name, student_No, subject_A, subject_B );
fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
} /* end while */
}

 

Solution

  • Has it already been said in the comment section, it will be much easer for you to use structs in your code. something like that (you can alter it as you see fit):

    typdef struct Student {
        char student_name[30];
        int student_No;
        double subject_A;
        double subject_B;
    };
    

    make a struct variable: Student s; in the function.

    Then when you call scanf function you call it like that : fscanf(ptrCf, "%s%d%lf%lf", s.student_name, &s.student_No, &s.subject_A, &s.subject_B);

    in function option_ordering_A() you make an array of structs and not just for subject A

    After that, you can sort the array of structs by subject A and all related data will change with it too.