Thread: Sorting in Alphabetical order

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    Angry Sorting in Alphabetical order

    Guys I need some help to finish this file in sorting. The names need to be sorted in ascending order by the last name.Help

    /* GIVEN A TEXT FILE WITH THE APPROPRIATE CONTENTS, READ THE INFORMATION
    IN THE FOLLOWTING STRUCTURE (FIGURE); THEN, SORT THE NAMES IN ALPHABETICAL
    ORDER ACCORDING TO THE LAST NAME, AND PRINT THE RESULTS IN ANOTHER FILE.
    INPUT FILE: IN_INFO.TXT
    OUTPUT FILE: OUT_INFO.TXT
    __

    */

    // *********** Preprocessor Directives ******************************
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdarg.h>

    typedef struct
    {
    char x[70];
    float g[4];
    } GRADES;
    // ************************************************** ****************


    // *********** Main Function ****************************************
    int main(void)
    {
    char temp[70];
    char temporary[70];
    int count, i, j;
    FILE *fp1;
    FILE *fp2;
    FILE *fp3;
    GRADES **p;
    GRADES **tempString;

    p = (GRADES **) calloc(15, sizeof(GRADES *));

    fp1 = fopen("in_info.txt", "r");
    if (fp1==NULL)
    {
    printf("Error: can't open input file\n");
    exit(1);
    }


    for (count = 0; count < 15; count++)
    { p[count] = (GRADES *) malloc(sizeof(GRADES));
    }

    for (i = 0; i < 15; i++)
    {
    fscanf(fp1, "%s", p[i]->x);
    fscanf(fp1, "%s", temp);
    strcat(p[i]->x, " ");
    strcat(p[i]->x, temp);

    for(j = 0; j < 4; j++)
    {
    fscanf(fp1, "%s", temp);
    if (temp[0] == 'W')
    {
    p[i]->g[j] = 87.0;
    }
    else {
    p[i]->g[j] = atof(temp);
    }
    }
    }
    fclose(fp1);

    fp2 = fopen("out_info.txt", "w")

    for (i = 0; i < 15; i++)
    {
    fprintf(fp2, "%s ", p[i]->x)
    for (j = 0; j < 4;j++)
    {
    if(p[i]->g[j] == 87.0)
    {
    fprintf(fp2, "%c ", 'W');
    }
    else
    }
    fprintf(fp2, "%3.1f ", p[i]->g[j]);
    }
    fprintf(fp2, "\n");
    }

    fclose(fp2);

    fp3 = fopen("out_info2.txt", "w");

    for (i = 0; i < 15; i++)
    {
    for(j = i + 1; j < 15; j++)
    {
    if(strcmp (p[i], p[j]) > 0)
    {
    tempString = p[i];
    p[i] = p[j];
    p[j] = tempString;
    }
    }
    fprintf(fp3, "%s", p[i]);
    }


    return 0;

    }
    Last edited by Andre Santiago; 12-13-2002 at 09:04 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Please use code tags. That's making my eyes bleed. Also you probably could have just looked at the thread about four down from this with the same exact topic...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-14-2008, 03:52 PM
  2. Putting a word in alphabetical order.
    By Brewer in forum C Programming
    Replies: 12
    Last Post: 12-16-2006, 05:11 PM
  3. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 01:07 PM
  4. Replies: 30
    Last Post: 10-29-2005, 11:09 PM
  5. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM