-2

Does anyone know why this sort is not sorting the first element in the struct array? Below is the code for the struct, the sort and some sample input output. I have excluded the code that filled the array because I know from printing the array before sorting it,that it is filled correctly.

struct Record
{
    char *firstName;
    char *lastName;
    char *GPA;
    unsigned int ID;
};

void newList(struct Record * Records, int amount, char* name)
{
    int i;
    int j;
    struct Record tempR;
    FILE *fp;
    unsigned int temp;
    //Order Array
    for(i=0;i<amount;i++)
    {
    for(j=0;j<amount-1;j++)
    {
    if(strcmp(Records[j].firstName,Records[j+1].firstName)<0)
    {
    tempR=Records[j];
    Records[j]=Records[j+1];
    Records[j+1]=tempR;
    }
    }
    }
//Make New Fle with Ordered Array
    fp=fopen(name, "w+");
    for(i=0;i<amount;i++)
    {
    fprintf(fp,"%s, %s, %s, %d, Records[i].firstName,Records[i].lastName,               
    Records[i].GPA,Records[i].ID);
    }
    }

A sample input and output of this code is the following Input:

Yblwtjbvtz,Eoztbzoqnz,2.6,1123268861 
Blmhwgzjdd,Ojwfnlislc,3.1,1712113924 
Gkmkbnotic,Mhzcakkugv,3.3,1966045151 
Zsrwqdwkfo,Nciqixcamr,2.1,212426241 
Vrekafrafk,Ixylzenhlc,2.2,297694159 
Kzkaxpoeqg,Syawkuqbew,3.4,104209687 
Ekdcfsifrw,Apvrwfshqm,1.4,799470314 
Iybmcotvpf,Eqvcorjntu,0.6,1748600414 
Jsfwiydnyt,Rhyaabwfdr,2.2,104800253 
Mfqrukoytp,Urjsjcloau,3.8,1240702350"

Output:

Yblwtjbvtz, Eoztbzoqnz, 2.6, 1123268861 
Zsrwqdwkfo, Nciqixcamr, 2.1, 212426241 
Vrekafrafk, Ixylzenhlc, 2.2, 297694159 
Mfqrukoytp, Urjsjcloau, 3.8, 1240702350 
Kzkaxpoeqg, Syawkuqbew, 3.4, 104209687 
Jsfwiydnyt, Rhyaabwfdr, 2.2, 104800253 
Iybmcotvpf, Eqvcorjntu, 0.6, 1748600414 
Gkmkbnotic, Mhzcakkugv, 3.3, 1966045151 
Ekdcfsifrw, Apvrwfshqm, 1.4, 799470314 
Blmhwgzjdd, Ojwfnlislc, 3.1, 1712113924
  • 2
    You need to get that code formatting straightened out. To format code properly: 1) copy the code from the source file 2) paste the code into the question 3) select the code in the question 4) click the code button `{}` or press ctrl-K – user3386109 Mar 12 '16 at 00:44
  • 1
    [Couldn't reproduce](http://melpon.org/wandbox/permlink/mNeSJjV2rGK2dvOt) with some modification that won't affect the logic. Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeCAT Mar 12 '16 at 00:50
  • Obviously you're one pass short, `for(i=0;i – xvan Mar 12 '16 at 01:00
  • @xvan The "Z" entry started on row 4, so it should have replaced the "Y" entry on the third pass. So I agree with MikeCAT that the code shown doesn't match the output. – user3386109 Mar 12 '16 at 01:03
  • strongly suggest: place the bubble sort algorithm in a separate function, possibly named `bubbleSort()` It is best to perform only one clearly defined action within a function – user3629249 Mar 12 '16 at 02:05
  • as a suggestion, the signature of the function: `newList()` could use some clarification. Suggest meaningful names for the passed parameters: suggest: `void newList(struct Record * Records, int numRecords, char* outFileName)` – user3629249 Mar 12 '16 at 02:08
  • in general, copying a struct will fail with the assignment statements being used in the posted code. Suggest using `memcpy()` – user3629249 Mar 12 '16 at 02:15
  • I'm co confused...@MikeCat so you used the code and it ran correctly? That's what it looks like in the link you posted? (That's so weird I am using Putty and I promise the output I posted is what I get everytime I run it) Also @user3386109 what is the difference between my formatting and http://stackoverflow.com/questions/35956217/change-access-modifier-on-demand. Why am I getting minus points and that question got positive points? – Chris Austin Mar 12 '16 at 14:25
  • @ChrisAustin You've got to be kidding me if you don't see the code formatting difference between [this question](http://stackoverflow.com/q/35956217/2792531) and yours. – nhgrif Mar 12 '16 at 14:27
  • @nhgrif do you mean the indentation? – Chris Austin Mar 12 '16 at 14:57
  • Yes, the indentation. You can quickly fix the indenting by highlighting the code the clicking the `{}` button – user3629249 Mar 12 '16 at 17:48
  • @ChrisAustin Yes, I was referring to the lack of indentation. One explanation for the behavior that you describe is that there's a bug in the code that you haven't shown. Which is why debugging questions should always have a [Minimal Complete Verifiable Example](http://stackoverflow.com/help/mcve). – user3386109 Mar 12 '16 at 18:21

2 Answers2

0

here is an example bubble sort algorithm, written in C.

for (size_t c = 0 ; c < ( n - 1 ); c++)
{
    for (size_t d = 0 ; d < n - c - 1; d++)
    {
        if (array[d] > array[d+1]) /* For decreasing order use < */
        {
            swap       = array[d];
            array[d]   = array[d+1];
            array[d+1] = swap;
        }
    }
}

so the posted code should implement the same algorithm, just using different variable/array names

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

the following code implements all the comments, performs appropriate error checking, and performs the desired operations.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Record
{
    char *firstName;
    char *lastName;
    char *GPA;
    unsigned int ID;
};

static struct Record Records[] =
{
    {"Yblwtjbvtz","Eoztbzoqnz","2.6",1123268861},
    {"Blmhwgzjdd","Ojwfnlislc","3.1",1712113924},
    {"Gkmkbnotic","Mhzcakkugv","3.3",1966045151},
    {"Zsrwqdwkfo","Nciqixcamr","2.1",212426241},
    {"Vrekafrafk","Ixylzenhlc","2.2",297694159},
    {"Kzkaxpoeqg","Syawkuqbew","3.4",104209687},
    {"Ekdcfsifrw","Apvrwfshqm","1.4",799470314},
    {"Iybmcotvpf","Eqvcorjntu","0.6",1748600414},
    {"Jsfwiydnyt","Rhyaabwfdr","2.2",104800253},
    {"Mfqrukoytp","Urjsjcloau","3.8",1240702350}
};

//prototypes
void newList(   char* filename );
void printList( char* filename );


int main( void )
{
    newList(   "sorted.txt" );
    printList( "sorted.txt" );
    return 0;
} // end function: main


void newList( char* filename )
{
    struct Record swap;
    size_t numRecords = sizeof( Records)/sizeof( struct Record);

    //Order Array,  ascending on firstName field
    for (size_t c = 0 ; c < ( numRecords - 1 ); c++)
    {
        for (size_t d = 0 ; d < numRecords - c - 1; d++)
        {
            if ( 0 < strcmp(Records[d].firstName,   Records[d+1].firstName) )
            {
                memcpy( &swap,         &Records[d],   sizeof( struct Record ) );
                memcpy( &Records[d],   &Records[d+1], sizeof( struct Record ) );
                memcpy( &Records[d+1], &swap,         sizeof( struct Record ) );
            }
        }
    }

    //Make New File with Ordered Array
    FILE *fp = NULL;
    if( NULL == (fp=fopen(filename, "w") ) )
    { // then, fopen failed
        perror( "fopen sorted.txt for write failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    for(size_t i=0;i<numRecords;i++)
    {
        fwrite( &Records[i], sizeof( struct Record ), 1, fp );
    }
    fclose( fp );
} // end function: newList


void printList( char * filename )
{
    FILE *fp = NULL;
    if( NULL == (fp=fopen(filename, "r") ) )
    { // then, fopen failed
        perror( "fopen sorted.txt for read failed" );
        exit( EXIT_FAILURE );
    }

    struct Record oneRecord;



    while(1 == fread( &oneRecord, sizeof( struct Record ), 1, fp) )
    {
        printf( "%s %s %s %u\n",
                oneRecord.firstName,
                oneRecord.lastName,
                oneRecord.GPA,
                oneRecord.ID );
    }

    fclose( fp );
} // end function: printList

this is the output from the program:

Blmhwgzjdd Ojwfnlislc 3.1 1712113924
Ekdcfsifrw Apvrwfshqm 1.4 799470314
Gkmkbnotic Mhzcakkugv 3.3 1966045151
Iybmcotvpf Eqvcorjntu 0.6 1748600414
Jsfwiydnyt Rhyaabwfdr 2.2 104800253
Kzkaxpoeqg Syawkuqbew 3.4 104209687
Mfqrukoytp Urjsjcloau 3.8 1240702350
Vrekafrafk Ixylzenhlc 2.2 297694159
Yblwtjbvtz Eoztbzoqnz 2.6 1123268861
Zsrwqdwkfo Nciqixcamr 2.1 212426241
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • I implemented this...it's still not working properly. Thank you for the code though!. This must have something to do with Putty. Because the logic is there...no reason this shouldn't work – Chris Austin Mar 12 '16 at 20:56