Question
#include #include /* Structure to store students data */ struct Product { char *name; double unit_price; double sale_vol; double total_sale; }; struct Product *readSalesData(int *totalData);
#include #include
/* 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
i++; } fclose(write); }
void selection_sort(struct Product a[], int n) { int i, largest = 0; if (n == 1) return; for (i = 1; i
if (a[i].sale_vol > a[largest].sale_vol) largest = i; } if (largest
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); }
For Mac gcc terminal.(Pravesh Kumar)
Name your program product2.e) so it takes 2. Extra credit (20 points) 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[l, 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.38Step 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