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

-I need to write a program in C to store a list of names (the last name first) a

ID: 3684749 • Letter: #

Question

-I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names.

- Could you please fix this program for me!

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

void data_sort(char name[100],int age[100],int size){
    int i = 0;
    while (i < size){
        int j = i+1;
        while (j < size){
          
            if (strcmp(name[i],name[j]) > 0){
                int temp = age[i];
                age[i] = age[j];
                age[j] = temp;
            }
            j += 1;
        }
        i += 1;
    }

   i = 0;
    while (i < size){
        printf("%s %d ", name[i],age[i]);
        i += 1;
    }
}

int main(int argc, char *argv[])
{
    /*declare variable*/
    FILE *inp;
    int status;
    char names[100][41];
    char temp_first[41];
    int age[100];
    int i=0;
   
    /*if there is no file*/
    if (argc <= 2)
    {
        printf("EE233 spring 2016 P5: chaymae");
    exit (0);
    }
   
    else if (argc>=2)
    {
       inp = fopen (argv[1], "r");
        if (inp == NULL)
        {
            printf("%s ","NO SUCH FILE EXIST");
            exit(0);
        }
        else
        {
            status = fscanf(inp,"%s %s %d",name,temp_first,age[i]);
          
            while (status!= EOF)
            {
                for (i=0; i< 100; ++1)
            {
                    strcat(name[i]," ");
                    strcat(name[i],temp_first);
                    ++i;
                     status = fscanf(inp,"%s %s %d",name[i],temp_first,age[i]);
                     data_sort(name[i],age[i]);
                }
               
                  
        }
    }
    return 0;
}

Explanation / Answer


#include <stdio.h>
#include <stdlib.h>   // malloc()
#include <string.h>   // strcmp()

struct list {
   char name[31];
   int age;
   struct list *next; // pointer to next data item in list
} prsn;

struct list *top;     // establish start of list

// proto types
struct list *sort_store(struct list *new, struct list *top);
void display(struct list *start);

int main()
{
   struct list *person, *sort_store();
  
   printf(" Enter a number of names and ages (q to exit loop) ");
   for (;;)
   {
       person = (struct list *) malloc(sizeof(prsn));
       if (!person)
       {
           puts(" Out of memory!");
           break;
       }
       printf(" Enter name : ");
       // use fgets() to allow for names with spaces
       fgets( person->name, sizeof(person->name), stdin);
       // remove frigging fgets() trailing ' '
       if (person->name[strlen(person->name)-1] == ' ')
           person->name[strlen(person->name)-1] = '';   
       // mimic an AND situation      
       if (strlen(person->name) == 1)
       {    
           if (person->name[0] == 'q') break;
       }
       printf("Enter age : ");
       int e;
       e = scanf("%d",&person->age);
       // trap non-numeric entry
       if (e == 0)
           person->age = 0;
       // flush the input stream in case of bad input
       fflush(stdin);
       // store data and update top of list      
       top = sort_store(person,top);
   }
   // display the sorted list from the top
   display(top);
  
   getchar(); // wait
   return 0;
}

//
// insert new data to the list in sorted order
//
struct list *sort_store(struct list *new, struct list *top)
{
   static struct list *last = NULL;
   struct list *old, *start;
  
   start = top;
   if (!last)
   {
       new->next = NULL;
       last      = new;
       return (new);
   }
   old = NULL;
   while (top)
   {
       // sort by name in ascending order
       if (strcmp(top->name, new->name) < 0)
       {
           old = top;
           top = top->next;
       }
       else
       {
           if (old)
           {
               old->next = new;
               new->next = top;
               return (start);
           }
           new->next = top;
           return (new);
       }
   }
   last->next = new;
   new->next = NULL;
   last       = new;
   return (start);
}

//
// walk through the linked list and display the content
//
void display(struct list *start)
{
   while (start)
   {
       printf(" Name = %-30s Age = %2d", start->name, start->age);
       start = start->next;
   }
   printf(" ");
}