1. (100 points) The daily sales of a grocery store are saved in the file sale.tx
ID: 3684764 • Letter: 1
Question
1. (100 points) 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 sale.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: string double o name o unit price o total number of pounds sold double o sale volume double You can assume the sale.txt as the following format for each product: product name (one single word), unit price, total numbers of pounds sold. apple, 2.5, 421.4Explanation / Answer
Part 1 : product.c
#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("sale.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_products.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);
}
Part 2 : product2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.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 find_product(struct Product products[], int n, char *name);
/*
* Entry point
*/
int main(int argc, char* argv[])
{
struct Product *products;
int totalData = 0;
products = readSalesData(&totalData);
printf("Total: %d ", totalData);
//sorting by sales volume
selection_sort(products, totalData);
// searching and printg
find_product(products, totalData, argv[1]);
return 0;
}
struct Product* readSalesData(int *totalData)
{
struct Product *products;
FILE *read;
read = fopen("sale.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 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);
}
void find_product(struct Product products[], int n, char *name){
int i;
for(i=0; i<n; i++){
if(strcmp(products[i].name, name) == 0){
printf("Name: %s ", name);
printf("Unit price: %.2lf ", products[i].unit_price);
printf("Number of products sold: %.2lf ", products[i].total_sale);
printf("Sales Volume: %.2lf ", products[i].sale_vol);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.