A local vitamin store gathered some data in a file for protein powders. The stor
ID: 3738440 • Letter: A
Question
A local vitamin store gathered some data in a file for protein powders. The store manager would like to be able to recommend product to customers based on customer reviews. The file has the following format (the first line is not included in the file).
InStock unitsSold Avg. Customer Review #of Reviews Brand
0 790 4.5 150 Optimum Nutrition
25 555 3.8 53 BSN
…
The numbers represents the number of units in stock at the drugstore, and units sold for the past six months, the average customer review, and the number of reviews, followed by the brand name.
Write a program that read in the data from a file and sort the protein powders by average customer review. The output file contains the list of top 5 recommended protein powders (top 5 by average customer review with 50 or more reviews, and currently in stock). The list of recommended protein powders should be written the same file name as the input file name with added extension of .rcd. For example, if the original file name is protein.txt, the output file name is then protein.txt.rcd.
1. Input file name should be obtained from the command line. For example, if the file name is protein.txt, you will run the program as:
./a.out protein.txt
2. Read the data using fscanf function. To read the brand name (last item in a line), use
"%[^ ] " as the conversion specifier for fscanf function.
3. Define a structure protein to store the brand (string), unitsInStock (integer), and unitsSold (integer), and avg customer review (double), number of reviews (integer). Assume the brand is no more than 100 characters.
4. Build an array of protein structures. Assume that there are no more than 100 protein powders in the file.
5. Modify the selection_sort function to sort an array of protein powders in descending order, sorted by avg. customer review. The function should have the following prototype:
void selection_sort(struct protein protein_powders[], int n);
6. The output file should include the list of protein powders for recommendation (average customer review of 4 or more with 50 or more reviews, currently in stock) in the same format as the input file but in sorted order by average customer review.
Text File
0 70 4.5 150 Optimum Nutrition
25 146 3.8 50 BSN
14 59 4.1 255 Muscle Pharm
0 87 4.3 459 Body Fortress
13 132 3.9 78 Isopure
20 89 4.0 17 Pure Protein
4 15 4.4 1023 Orgain
19 58 3.3 389 EAS
8 45 3.5 284 Muscle Milk
2 34 3.8 134 Vega
11 19 4.2 132 Protein Powders
12 8 5.0 2 Protein Powder
3 45 3.7 365 Everyday Value
Selection_sort file
/*selection_sort.c, project 5, Program Design
*/
#include
#define N 10
void selection_sort(int a[], int n);
int main(void)
{
int i;
int a[N];
printf("Enter %d numbers to be sorted: ", N);
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
selection_sort(a, N);
printf("In sorted order:");
for (i = 0; i < N; i++)
printf(" %d", a[i]);
printf(" ");
return 0;
}
void selection_sort(int a[], int n)
{
int i, largest = 0, temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (a[i] > a[largest])
largest = i;
if (largest < n - 1) {
temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
selection_sort(a, n - 1);
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct protien {
char brand[100];
int unitsInStock;
int unitsSold;
double avgCustomerReview;
int numberOfReviews;
};
void selection_sort(struct protien a[], int n);
int main(void)
{
struct protien protiens[100];
FILE *fp=fopen("protien.txt","r");
FILE *fpw=fopen("protienOut.txt","w");
int n=0;
if(fp==NULL)
{
printf("Error while open the file");
exit(1);
}
else{
int inp =0;
while(inp!=-1)
{
inp = fscanf(fp,"%d %d %lf %d %[^ ]s ",&protiens[n].unitsInStock,&protiens[n].unitsSold,&protiens[n].avgCustomerReview,&protiens[n].numberOfReviews,protiens[n].brand);
//fscanf(fp,"",protiens[i].brand);
printf("%s ",protiens[n].brand);
n++;
}
}
selection_sort(protiens, n);
printf("In sorted order:");
int i;
for (i = 0; i < n-1; i++)
{
if(protiens[i].avgCustomerReview>=4){
fprintf(fpw,"%d %d %.2lf %d %s ",protiens[i].unitsInStock,protiens[i].unitsSold,protiens[i].avgCustomerReview,protiens[i].numberOfReviews,protiens[i].brand);
fprintf(fpw," ");
}
}
fclose(fpw);
fclose(fp);
return 0;
}
void selection_sort(struct protien a[], int n)
{
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 protien temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
selection_sort(a, n - 1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.