For this assessment you are to write a C program that reads

For this assessment you are to write a C program that reads names and corresponding ages of people records from a file, and then sorts the records based on the person\'s last name (in descending order). Once the records have been sorted, you must output the sorted records to a different file. You do NOT have to perform error checking on the records You may assume that they are stored in the input file in the appropriate format and alignment as follows (last name, first name age) Ruth, Babe 115 Jordan, Michael 46 Obama, Barack 48 Potter, Harry 16 Favre, Brett 38 Potter, Carrie 40 Hamm, Mia 33 After you have sorted the input records, the output records should be displayed in the output file as follows (with the age first): 115 Ruth, Babe 16 Potter, Harry 40 Potter, Carrie 48 Obama, Barack 46 Jordan, Michael 33 Hamm, Mia 38 Favre, Brett Note: The records were sorted based on last name, not the age! Requirements: The following are the ONLY requirements for the implementation of your solution. Your solution may be implemented in any way (using C) just as long as you have the following: 1. Input file with records stored in the format provided above 2. Output file with the sorted records stored in the format provided above 3. An array of structs, where each cell of the array (which is a struct) contains a record with a last name, first name, and age field, corresponding to the order of the records in the input file

Solution

//pls rate my solution if you like

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100
struct rec
{
   char fname[20];
   char lname[20];
   int age;
}record[MAX];

int main()
{
   //declare in and out as pointer to FILE
   FILE *in;
   FILE *out;
   int i = 0,count;
   char s[10];
   void sort(struct rec rec1[], int size);

   //open file in read mode
   in = fopen(\"input.txt\", \"r\");

   if (!in)
   {
       printf(\"not able to open file for reading\ \");
       return -1;
   }
   while (!feof(in))
   {
       fscanf(in,\"%s%s%s\", record[i].lname, record[i].fname, s);
       record[i].age = atoi(s);
       i++;
   }

   count = i;
   sort(record, count);
   //write record to file
   out = fopen(\"output.txt\", \"w\");

   if (!out)
   {
       printf(\"Not able to open file for writing\ \");
       return -1;
   }
   for (i = 0; i < count; i++)
   {
       fprintf(out, \"%d\\t%s%s\ \", record[i].age,record[i].lname, record[i].fname);
   }
   fclose(in);
   fclose(out);
}

//sort using bubble sort
void sort(struct rec rec1[], int size)
{
   char s1[20], s2[20];
   int ag,i,j,cmp;
   for (i = 0; i < size; i++)
   {
       for (j = 0; j < size - 1; j++)
       {
           cmp = strcmp(rec1[j].lname, rec1[j+1].lname);

           if (cmp<0)
           {
               strcpy(s1, rec1[j + 1].lname);
               strcpy(s2, rec1[j + 1].fname);
               ag = rec1[j + 1].age;
               strcpy(rec1[j + 1].lname,rec1[j].lname);
               strcpy(rec1[j + 1].fname,rec1[j].fname);
               rec1[j + 1].age = rec1[j].age;
               strcpy(rec1[j].lname,s1);
               strcpy(rec1[j].fname, s2);
               rec1[j].age = ag;
           }
       }
   }
}

-----------------------------------------------------------------------------------------------

output.txt file content

115   Ruth,Babe
16   Potter,Harry
40   Potter,Carrie
48   Obama,Barak
46   Jordan,Michael
33   Hamm,Mia
38   Favre,Brett

 For this assessment you are to write a C program that reads names and corresponding ages of people records from a file, and then sorts the records based on the
 For this assessment you are to write a C program that reads names and corresponding ages of people records from a file, and then sorts the records based on the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site