Question: please can any one help me to find out why this code does not run #include #include #include struct Name { char lastname[30]; char firstname[30];

please can any one help me to find out why this code does not run

#include

#include

#include

struct Name

{

char lastname[30];

char firstname[30];

};

int compfirst(const void *p,const void *q)

{

struct Name *a= (struct Name*)p;

struct Name *b= (struct Name*)q;

return strcmp(a->firstname,b->firstname);

}

void swap(char **str1,char **str2)

{

char *temp=*str1;

*str1=*str2;

*str2=temp;

}

int sortfirst(struct Name *a[],int n) //selection sort to sort name by firstname

{

int i,j,min_ind;

for(i=0;i

{

min_ind=i;

for(j=i+1;j

{

if(strcmp(a[min_ind]->firstname,a[j]->firstname)>0)

min_ind=j;

}

char tmp[30];

strcpy(tmp,a[i]->lastname);

strcpy(a[i]->lastname,a[min_ind]->lastname);

strcpy(a[min_ind]->lastname,tmp);

strcpy(tmp,a[i]->firstname);

strcpy(a[i]->firstname,a[min_ind]->firstname);

strcpy(a[min_ind]->firstname,tmp);

}

}

int sortlast(struct Name *a[],int n) //selction sort name by lastname

{

int i,j,min_ind;

for(i=0;i

{

min_ind=i;

for(j=i+1;j

{

if(strcmp(a[min_ind]->lastname,a[j]->lastname)>0)

min_ind=j;

}

char tmp[30];

strcpy(tmp,a[i]->lastname);

strcpy(a[i]->lastname,a[min_ind]->lastname);

strcpy(a[min_ind]->lastname,tmp);

strcpy(tmp,a[i]->firstname);

strcpy(a[i]->firstname,a[min_ind]->firstname);

strcpy(a[min_ind]->firstname,tmp);

}

}

int main()

{

FILE *f;

f=fopen("data.txt","r");

char fname[30],lname[30],c;

int count=0;

Name* nm[20];

//read name from file

printf(" Names in File : ");

while(fscanf(f,"%s %c %s",lname,&c,fname)==3) // read three argument from file lastname (comma)(,) and firstname;

{

printf("%s,%s ",lname,fname);

nm[count]=(struct Name*)malloc(sizeof(struct Name));

strcpy(nm[count]->lastname,lname);

strcpy(nm[count]->firstname,fname);

count++;

}

printf(" Name Sorted by Last Name ");

sortlast(nm,count);

for(int i=0;i

printf("%s %s ",nm[i]->lastname,nm[i]->firstname);

printf(" Name Sorted by First Name ");

sortfirst(nm,count);

for(int i=0;i

printf("%s,%s ",nm[i]->firstname,nm[i]->lastname);

// Read data from user

// also can save two different array and then copy structure element

printf(" Read from user and save in array of structure: ");

printf(" Enter Total Names : ");

scanf("%d",&count);

for(int i=0;i

{

printf(" Firstname : ");

scanf("%s",fname);

printf(" LastName : ");

scanf("%s",lname);

nm[i]=(struct Name*)malloc(sizeof(struct Name));

strcpy(nm[i]->lastname,lname);

strcpy(nm[i]->firstname,fname);

}

printf(" Name Sorted by Last Name ");

sortlast(nm,count);

for(int i=0;i

printf("%s %s ",nm[i]->lastname,nm[i]->firstname);

printf(" Name Sorted by First Name ");

sortfirst(nm,count);

for(int i=0;i

printf("%s,%s ",nm[i]->firstname,nm[i]->lastname);

return 0;

?}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!