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

The daily sales of a grocery store are saved in the file sale.txt. Each line con

ID: 3683732 • Letter: T

Question

The daily sales of a grocery store are saved in the file sale.txt. Each line contains the record for a product name, price per pound in dollars, and total number of pounds sold. Write a program that reads file sales.txt, store the data in an array of product structures, and sort the products by sale volume. Output the sorted products, including sale volumes in a text file called sorted_products.txt. A product structure should have the following attributes:

o name string

o unit price double

o total number of pounds sold double

o sale volume double

You can assume the sales.txt as the following format for each product: product name (one single word), unit price, total numbers of pounds sold.

apple, 2.5, 421.4

1. Name your program product.c.

2. The program should be built around an array of structures, with each structure containing information of a product’s name, unit price, total numbers of pounds sold, sale volume (unit price * total numbers of pounds sold). Assume that there are no more than 1000 products in the file.

3. Use fscanf and fprintf to read and write data.

4. Modify the selection_sort function provided to sort an array of product struct. The boxes should be sorted by sale volume in ascending order. The function should have the following prototype:

void selection_sort(struct product products[], int n);

5. Output the sorted products, including sale volumes, in a text file called sorted_products.txt, in the following format.

#name unit price ($) units (pound) sold sale volume ($)

celery 2.300000 99.450000 228.735000

blueberry 3.500000 87.300000 305.550000

potato 0.600000 823.650000 494.190000 …

(2) Modify the program (Name your program product2.c) so it takes command line argument for the product name and the program displays unit price, total numbers of pounds sold, and sale volume of the product. The program should include the following function:

void find_product(struct product products[], int n, char *name);

The find_player function should find the product by name, print the product’s unit price, total numbers of pounds sold, and sale volume with two decimal digits.

Example run:

./a.out grape

Output:

Name: grape

Unit price: 2.25

Number of pounds sold: 393.50

Sale volume: 885.38

Sales.txt

orange 1.2 506.21

apple 1.25 944.23

grape 2.25 393.5

cherry 4.25 234.56

avocado 4.76 107.2

celery 2.3 99.45

broccoli 2.1 498.3

cucumber 1.8 367.3

onion 4.3 234.2

blueberry 3.5 87.3

tomato 2.9 873.9

spinach 3.9 267.3

lettuce 5.2 882.4

salmon 10.99 342.4

catfish 6.99 258.3

potato 0.6 823.65

Selection_sort.c

/*selection_sort.c */
#include <stdio.h>

#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>

/*
Structure to store students data
*/
struct Product
{
    char *name;
    double unit_price;
    double sale_vol;
    double total_sale;
};

struct Product* readSalesData(int *totalData);
void selection_sort(struct Product a[], int n);
void writeSalesData(struct Product products[], int totalData);

/*
* Entry point
*/
int main()
{
    struct Product *products;
    int totalData = 0;
    products = readSalesData(&totalData);
  
    printf("Total: %d ", totalData);
    //sorting by sales volume
    selection_sort(products, totalData);
  
    //writing to file
    writeSalesData(products, totalData);
  
    return 0;
}


struct Product* readSalesData(int *totalData)
{
    struct Product *products;
  
    FILE *read;
  
    read = fopen("sales.txt", "r");
  
    int i=0;
    products = (struct Product *)malloc(1000*sizeof(struct Product*));
    while(!feof(read))
     {
      
       products[i].name = (char *)malloc(20*sizeof(char));
       fscanf(read,"%s %lf %lf", products[i].name, &products[i].unit_price, &products[i].total_sale);
       products[i].sale_vol = products[i].unit_price * products[i].total_sale;
      
       i++;
       (*totalData)++;
    }
    printf("i= %d ",i);
   fclose(read);
   return products;
}

void writeSalesData(struct Product products[], int totalData)
{
  
    FILE *write;
  
    write = fopen("sorted_sales.txt", "w");
  
    int i=0;
    while(i<totalData)
     {
       fprintf(write,"%s %lf %lf %lf ", products[i].name,products[i].unit_price, products[i].total_sale, products[i].sale_vol);
      
       i++;
    }
   fclose(write);
}

void selection_sort(struct Product a[], int n){
   int i, largest = 0;
   if (n == 1)
       return;
   for (i = 1; i < n; i++){

   if (a[i].sale_vol > a[largest].sale_vol)  
       largest = i;
   }
      if (largest < n - 1) {

       double temp = a[n-1].sale_vol;

       a[n-1].sale_vol = a[largest].sale_vol;

       a[largest].sale_vol = temp;
      
       double temp_unit_price = a[n-1].unit_price;
       a[n-1].unit_price = a[largest].unit_price;
       a[largest].unit_price = temp_unit_price;
      
       double temp_total_sale = a[n-1].total_sale;
       a[n-1].total_sale = a[largest].total_sale;
       a[largest].total_sale = temp_total_sale;
      
       char *temp_name = a[n-1].name;
       a[n-1].name = a[largest].name;
       a[largest].name = temp_name;
   }
   selection_sort(a, n - 1);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote