Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

x.Hmelp on the last function which is bubble sort the cost of joining the group.

ID: 3614792 • Letter: X

Question

x.Hmelp on the last function which is bubble sort the cost of joining the group. (from lowest to greatest) Please I can't figure out where the error is

004 132 10.00 Oozeball
132 24 2.00 Bed Races
12 32 10.00 Joint Council of Engineering Organizations
130 43 15.00 Freshman Leaders on Campus
133 41 5.00 Society of Hispanic Professional Engineers
135 18 4.00 National Society of Black Engineers





#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXGROUP 20
#define MINCOUNT 12
#define MAXCOUNT 20
#define ZERO 0

struct activities
{
        int groupnum;
        char *groupname;
        float meettime;
        char mthalfday;
        char meetday;
        int meetweek;
        char nextdate[6];
        char buildingloc[4];
        char meetroom[5];
        char purpose;
        char memtype;
        int groupsize;
        float groupcost;
        //enum national enumtype;
        //union local unionlevel;
        float meetlength;
        struct activities *linkorgan;
};

void printallinput(struct activities *newnode);
void searchgroupnum(struct activities *newnode);
void searchname(struct activities *newnode, char *strings, int mbytes, int read);
void bubblesortgroupname(struct activities *newnode);
int main(void)
{
        FILE *groupfile;
        struct activities *startgroup;
        struct activities *gpnewnode;
        struct activities *tempnode;
        int iindex = 0, jindex, kindex, count;
        int intro, index, len;
        int choose, xindex, xcount;
        char *my_groupfile, *stringss;
        int nbytes = 60, gbytes = 60;
        int bytes_read, reads;
        int i=1;
        gpnewnode = NULL;


                groupfile = fopen("xfil.dat", "r");
                if(groupfile == NULL)
                {
                        printf(" Error opening File! ");
                        return 0;
                }

                startgroup = (struct activities *)malloc(sizeof(struct activities));                                    //allocate memory for first group
                if(startgroup == NULL)
                {
                        printf(" Memory allocation was unsuccesfull");
                        return 0;
                }

                while(fscanf(groupfile,"%d", &(startgroup->groupnum)) != EOF)                                           //get group number
                {
                        fscanf(groupfile, "%d", &(startgroup->groupsize));
                        fscanf(groupfile,"%f", &(startgroup->groupcost));                                               //get cost

                        my_groupfile = (char *) malloc (nbytes + 1);
                        bytes_read = getline (&my_groupfile, &nbytes, groupfile);

                        startgroup->groupname = (char *)malloc(bytes_read);
                        strncpy(startgroup->groupname, my_groupfile, (bytes_read-1));                                 //copy group names from temp pointer

                        startgroup->linkorgan = NULL;
                        if(gpnewnode == NULL)
                                gpnewnode = startgroup;
                        else
          &nb

Explanation / Answer

voidbubblesortgroupname(struct activities *newnode)
{

        struct activities*first,*ptr,temp,*l,*r;
        int swap;
        while(1)
        {
               swap=0;
               first = newnode;
               while(first->linkorgan != NULL)
               {
                if(first -> groupcost <first->linkorgan->groupcost)
                 {
                 /* save next pointers */
                  l=first->linkorgan;
                  r=first->linkorgan->linkorgan;
                  
                  /* swap d data */
                  temp = *first;
                  *first = *(first->linkorgan);
                  *(first->linkorgan) = temp;
                  /* store next pointer */
                  first->linkorgan = l;
                  first->linkorgan->linkorgan = r;
                  swap=1;
                 }
               first = first->linkorgan;
               }
               if(swap==0)
               break;
        }

     
}