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

This homework is to examine arrays, strings and data structures. Part one: Read

ID: 3720716 • Letter: T

Question

This homework is to examine arrays, strings and data structures.

Part one:

Read in a file of names: last name, first name will be the format.

A) Print the list of names sorted by last names, i.e., lastName, firstName

Findler, Michael

Miller, Teri

Pochert, Lois

B) Print the list of names sorted by first names., i.e., firstName lastName

Lois Pochert Michael Findler Teri Miller

C) To get ahead of the game, read these in as two separate arrays.

Part Two:

Final version will require the names to be stored in an array of structs.

Explanation / Answer

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

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<n-1;i++)

{

min_ind=i;

for(j=i+1;j<n;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<n-1;i++)

{

min_ind=i;

for(j=i+1;j<n;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<count;i++)

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

  

  

  

printf(" Name Sorted by First Name ");

sortfirst(nm,count);

for(int i=0;i<count;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<count;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<count;i++)

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

  

  

  

printf(" Name Sorted by First Name ");

sortfirst(nm,count);

for(int i=0;i<count;i++)

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

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote