Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data And Hadoop Fundamentals Tools And Techniques For Data Driven Success

Authors: Mayank Bhushan

2nd Edition

9355516665, 978-9355516664

More Books

Students also viewed these Databases questions

Question

=+3. What could be objectives for training employees?

Answered: 1 week ago

Question

Develop clear policy statements.

Answered: 1 week ago

Question

Draft a business plan.

Answered: 1 week ago

Question

Describe the guidelines for appropriate use of the direct plan.

Answered: 1 week ago