x.Hm need help on bubble sorting the list on \"group cost or group name\" either
ID: 3614802 • Letter: X
Question
x.Hm need help on bubble sorting the list on "group cost or group name" either one is fine. Please no "break" statement. The function I need help is the last function it works for sorting only "group num" and I don't understand why004 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 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 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));
if(startgroup == NULL)
{
printf(" Memory allocation was unsuccesfull");
return 0;
}
while(fscanf(groupfile,"%d", &(startgroup->groupnum)) != EOF)
{
fscanf(groupfile, "%d", &(startgroup->groupsize));
fscanf(groupfile,"%f", &(startgroup->groupcost));
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));
startgroup->linkorgan = NULL;
if(gpnewnode == NULL)
gpnewnode = startgroup;
else
{
tempnode = gpnewnode;
while(tempnode->linkorgan != NULL)
tempnode = tempnode->linkorgan;
tempnode->linkorgan = startgroup;
}
Explanation / Answer
The only line you should need to change is the following: if(ascend->groupcost > qsort->groupcost) You may have tried the following unsuccessful attempt: if(ascend->groupname > qsort->groupname) That doesn't work because it compares the addresses of the firstcharacters of the names. Instead, the names themselves should be compared, and that's wherefunction strcmp comes in if(strcmp(ascend->groupname, qsort->groupname) >0)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.