Question
Modify project 8 so that it uses qsort function to sort the array of protein powders instead of selection sort function. Your program should include
Modify project 8 so that it uses qsort function to sort the array of protein powders instead of selection sort function. Your program should include a comparison function that compares protein struct for qsort function. This program will be graded based on whether comparison function and qsort function call are implemented correctly.
Project 8 below
#include
#include
//struct prototype
struct protein
{
char brand[100];
int unitsInStock;
int unitsSold;
double avgCustomerReview;
int numberOfReviews;
};
int compare(const void x, const void y);
//void selection_sort(struct protein a[], int n);
int main(int argc, char *argv[])
{
struct protein proteins[100];
FILE *fp=fopen(argv[1],"r");
FILE *fpw=fopen("protein.txt.rcd","w");
int n=0;
int j;
if(fp==NULL)
{
printf("Error while open the file");
exit(1);
}
else
{
int inp =0;
while(inp!=-1)//checking for invalid numbers
{
inp = fscanf(fp,"%d %d %lf %d %[^ ]s ",&proteins[n].unitsInStock,&proteins[n].unitsSold,&proteins[n].avgCustomerReview,&proteins[n].numberOfReviews,proteins[n].brand);
printf("%s ",proteins[n].brand);
n++;
}
}
//selection_sort(proteins, n);//sorting by average customer review
qsort(protein, protein[100], sizeof(struct proteins), compare);
printf("In sorted order:");
int i;
for (i = 0; i < n-5; i++)//displaying top 5
{
if( (proteins[i].numberOfReviews >= 50) && (proteins[i].unitsInStock > 0))//printing proteins with 50 or more review and currently in stock
{
fprintf(fpw," %d \t%d \t%.2lf \t%d \t%s ",proteins[i].unitsInStock,proteins[i].unitsSold,proteins[i].avgCustomerReview,proteins[i].numberOfReviews,proteins[i].brand);
}
}
printf(" ");
fclose(fpw);
fclose(fp);
return 0;
}
//void selection_sort(struct protein a[], int n)
/*int qsort(protein_powder, count)
{
int i, largest = 0; //temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (a[i].avgCustomerReview < a[largest].avgCustomerReview)
largest = i;
if (largest < n - 1)
{
struct protein temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
selection_sort(a, n - 1);
}*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started