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

a. Name your program hw14b.c b. Use the same input file as for hw14a.in, except

ID: 3625610 • Letter: A

Question

a. Name your program hw14b.c

b. Use the same input file as for hw14a.in, except remove the ages. In this way,

we are just sorting an array of strings. Call this input file hw14b.in.

c. Read the input file using fgets() into an array of strings. Assume we will

alphabetize no more than 80 names, and that names have no more than 40

characters. Count the number of lines you read in until the null pointer is reached.

d. Copy the addresses of the names to an array of pointers.

e. Implement the bubble sort algorithm, sorting an array of pointers. Let n be the

number of names in the list. Then the function proto-type will be:

void bubble_sort_str(char *list[], int n);

f. Then, create the file hw14b.out in alphabetical order, as in:

Boomer, Baby

DuMond, Kristin

Lady, Old

Larson, Lois

...

Explanation / Answer

    #include <stdio.h>
    #include <conio.h>
     #include<string.h>
     #include<stdlib.h>

     void bubble_sort_str(char *list[], int n)
     {
         char *tmp;   
         int i = 0;
         int j = 0;
         for(i = 0; i < n; ++i)
         {
         for(j = i + 1; j < n; ++j)
         {
         if(strcmp(list[i], list[j]) > 0)
            {
             tmp = list[i];
             list[i] = list[j];
             list[j]=tmp;
            }
         }
        }
       
    }
    
    
    
    int main()
    {
     char name[20];
     char name1[20];
      int p;
    char c[60];
    char out[40];
    FILE *f1;
    char *list[20];
    static int op=0;
    FILE *f2;
    f2 = fopen("hw14a.out","w");
    f1=fopen("hw14a.in","r");
    while(!feof(f1))
    {
    fscanf(f1,"%s %s %d ", name,name1,&p);
    sprintf(out,"%s %s",name,name1);
    list[op] = (char *)calloc(strlen(out)+1, sizeof(char));
    strcpy(list[op],out);
    op++;               
    }
    bubble_sort_str(list,op);
    for(int i=0; i<op; i++)
    fprintf(f2, "%s ",list[i]);
    fclose(f1);
    fclose(f2);
    getch();
   
   
    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