Menu

[Solved]Modify Supplylistc Program Uses Qsort Function Sort Array Supply Instead Selection Sort Fu Q37138690

Modify the supply_list.c program sothat it uses qsort function to sort the array ofsupply instead of the selection sort function. Your program shouldinclude a comparison function that compares struct supplyfor qsort function.

The program and text file are below.

/////////////////////////////////////

/* supply_list.c */

/* The purpose of this program is to maintain an ordered list ofleftover
school supplies for a school teacher. The user inputs the filename
contatining the list of supplies and the program outputs theordered
to a separate file.
*/

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

// supply structure stores name and color strings, and quantityinteger
struct supply
{
char name[100], color[100];
int quantity;
};

void selection_sort(struct supply list[], int n);

int main()
{
// User inputs filename
char fileName[100];
printf(“Enter the file name with the list of supplies: “);
scanf(“%s”, fileName);
// File is opened to be read
FILE *infile = fopen(fileName, “r”);

if(infile == NULL)
{
printf(“File not foundn”);
return 1;
}

// Array of supply named list
struct supply list[200];
// Reads data from file
int i = 0;

while(1)
{
if(feof(infile))
{
break;
}
fscanf(infile, “%s %d %[^n]n”, list[i].color,&list[i].quantity, list[i].name);
i++;
}

selection_sort(list, i);
// changing output file name
char sortedName[107] = “sorted_”;
int j,k;

for(j=7, k=0; j<strlen(fileName)+7; j++, k++)
{
sortedName[j] = fileName[k];
}

// Opens file for output
FILE *outfile = fopen(sortedName, “w”);

// Checks the status of the output file
if(outfile == NULL)
{
printf(“Output file cannot be opened.n”);
return 1;
}

// Writes data to the sorted ouput file
for(j=0; j<i; j++)
{
fprintf(outfile, “%s %d %sn”, list[j].name, list[j].quantity,list[j].color);
}

// Closes both files
fclose(infile);
fclose(outfile);

return 0;
}

void selection_sort(struct supply list[], int n)
{
struct supply dat;
int i, j, m;

for(i=0; i<n-1; i++)
{
m = i;
for(j=i+1; j<n; j++)
{
int r = strcmp(list[j].name , list[m].name );
if(r<0)
{
dat = list[m];
list[m] = list[j];
list[j] = dat;
}
}
}
}

/////////////////////////////////////

THIS IS THE TEXT FILE

red 6 pencil box
blue 1 pencil box
orange 16 glue sticks
mutli 9 facial tissues
black 8 notebook
blue 20 crayon
purple 8 crayon
pink 5 post-it
white 4 pencil box
purple 2 protractor
pink 15 eraser
red 2 dry-erase marker
red 13 #2 pencil
yellow 8 2-pocket folder
blue 5 post-it
green 3 pairs of kids scissors
multi 7 pack of colored pencils
green 4 2-pocket folder
red 5 notebook
blue 6 dry-erase marker
yellow 4 highlighter
blue 6 2-pocket folder
mutli 10 #2 pencil
red 13 crayon
red 3 rollerball pen
yellow 3 post-it
red 4 pairs of kids scissors
black 5 composition book
white 4 pack of index cards
yellow 8 #2 pencil
white 2 binder
black 5 rollerball pen
white 3 school glue
black 5 dry-erase marker

Expert Answer


Answer to Modify the supply_list.c program so that it uses qsort function to sort the array of supply instead of the selection sor… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *